-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds draft of createAAS Method with tests
- Loading branch information
1 parent
7fffd7b
commit 1627a40
Showing
11 changed files
with
394 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...gateway-core/src/main/java/org/eclipse/digitaltwin/basyx/gateway/core/DefaultGateway.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2024 the Eclipse BaSyx Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
******************************************************************************/ | ||
|
||
package org.eclipse.digitaltwin.basyx.gateway.core; | ||
|
||
import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell; | ||
import org.eclipse.digitaltwin.basyx.gateway.core.exception.BaSyxComponentNotHealthyException; | ||
import org.eclipse.digitaltwin.basyx.gateway.core.feature.Gateway; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public class DefaultGateway implements Gateway { | ||
|
||
private Logger logger = LoggerFactory.getLogger(DefaultGateway.class); | ||
|
||
@Override | ||
public void createAAS(AssetAdministrationShell aas, String aasRepository, String aasRegistry) throws BaSyxComponentNotHealthyException{ | ||
throwExceptionIfIsUnhealthyBaSyxComponent(aasRepository); | ||
if(aasRegistry != null){ | ||
throwExceptionIfIsUnhealthyBaSyxComponent(aasRegistry); | ||
} | ||
} | ||
|
||
private void throwExceptionIfIsUnhealthyBaSyxComponent(String componentURL) { | ||
componentURL = formatURL(componentURL); | ||
|
||
if (isBaSyxComponent(componentURL) && !isHealthy(componentURL)) { | ||
throw new BaSyxComponentNotHealthyException(componentURL + " is not healthy"); | ||
} | ||
} | ||
|
||
private String formatURL(String componentURL) { | ||
try { | ||
URL url = new URL(componentURL); | ||
String protocol = url.getProtocol(); | ||
String host = url.getHost(); | ||
int port = url.getPort(); | ||
if (port == -1) { // no port specified, use default port | ||
port = url.getDefaultPort(); | ||
} | ||
componentURL = protocol + "://" + host + ":" + port; | ||
} catch (Exception e) { | ||
logger.error(e.getMessage()); | ||
} | ||
return componentURL; | ||
} | ||
|
||
private boolean isBaSyxComponent(String componentURL) { | ||
try { | ||
HttpURLConnection connection = getRequest(componentURL, "/shells"); | ||
|
||
String aasMiddleware = connection.getHeaderField("AASMiddleware"); | ||
|
||
return "BaSyx".equals(aasMiddleware); | ||
} catch (Exception e) { | ||
logger.error(e.getMessage()); | ||
return false; | ||
} | ||
} | ||
|
||
private boolean isHealthy(String componentURL){ | ||
try { | ||
HttpURLConnection connection = getRequest(componentURL, "/actuator/health"); | ||
|
||
String body = new String(connection.getInputStream().readAllBytes()); | ||
|
||
return connection.getResponseCode() == 200 && body.equals("{'status':'UP'}"); | ||
|
||
} catch (Exception e) { | ||
logger.error(e.getMessage()); | ||
return false; | ||
} | ||
} | ||
|
||
private static HttpURLConnection getRequest(String componentURL, String path) throws IOException { | ||
URL url = new URL(componentURL + path); | ||
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
connection.connect(); | ||
return connection; | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...gateway-core/src/main/java/org/eclipse/digitaltwin/basyx/gateway/core/GatewayFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.eclipse.digitaltwin.basyx.gateway.core; | ||
|
||
import org.eclipse.digitaltwin.basyx.gateway.core.feature.Gateway; | ||
|
||
public interface GatewayFactory { | ||
public Gateway create(); | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
...g/eclipse/digitaltwin/basyx/gateway/core/exception/BaSyxComponentNotHealthyException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2024 the Eclipse BaSyx Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
******************************************************************************/ | ||
|
||
package org.eclipse.digitaltwin.basyx.gateway.core.exception; | ||
|
||
public class BaSyxComponentNotHealthyException extends RuntimeException{ | ||
|
||
public BaSyxComponentNotHealthyException(String message) { | ||
super(message); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...core/src/main/java/org/eclipse/digitaltwin/basyx/gateway/core/feature/GatewayFeature.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package org.eclipse.digitaltwin.basyx.gateway.core.feature; | ||
|
||
import org.eclipse.digitaltwin.basyx.core.BaSyxFeature; | ||
import org.eclipse.digitaltwin.basyx.gateway.core.GatewayFactory; | ||
|
||
public interface GatewayFeature extends BaSyxFeature<GatewayFactory> { | ||
|
||
} |
125 changes: 125 additions & 0 deletions
125
...y/basyx.gateway-core/src/test/java/org/eclipse/digitaltwin/basyx/gateway/TestGateway.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/******************************************************************************* | ||
* Copyright (C) 2024 the Eclipse BaSyx Authors | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
* a copy of this software and associated documentation files (the | ||
* "Software"), to deal in the Software without restriction, including | ||
* without limitation the rights to use, copy, modify, merge, publish, | ||
* distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so, subject to | ||
* the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
******************************************************************************/ | ||
|
||
package org.eclipse.digitaltwin.basyx.gateway; | ||
|
||
import org.apache.http.HttpStatus; | ||
import org.eclipse.digitaltwin.basyx.gateway.core.DefaultGateway; | ||
import org.eclipse.digitaltwin.basyx.gateway.core.exception.BaSyxComponentNotHealthyException; | ||
import org.eclipse.digitaltwin.basyx.gateway.core.feature.Gateway; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.mockserver.client.MockServerClient; | ||
import org.mockserver.integration.ClientAndServer; | ||
import org.mockserver.verify.VerificationTimes; | ||
|
||
public class TestGateway { | ||
|
||
public static ClientAndServer mockBaSyxAasRepository; | ||
public static ClientAndServer mockAasRegistry; | ||
private Gateway gateway; | ||
|
||
@Before | ||
public void setUp(){ | ||
mockBaSyxAasRepository = ClientAndServer.startClientAndServer(6006); | ||
gateway = new DefaultGateway(); | ||
} | ||
|
||
@After | ||
public void tearDown(){ | ||
mockBaSyxAasRepository.stop(); | ||
} | ||
|
||
@Test | ||
public void testCreateAASWithHealthyBaSyxComponent(){ | ||
new MockServerClient("localhost", 6006) | ||
.when(org.mockserver.model.HttpRequest.request() | ||
.withMethod("GET") | ||
.withPath("/shells")) | ||
.respond(org.mockserver.model.HttpResponse.response() | ||
.withStatusCode(HttpStatus.SC_OK) | ||
.withHeader("AASMiddleware","BaSyx") | ||
.withBody("{}")); | ||
new MockServerClient("localhost", 6006) | ||
.when(org.mockserver.model.HttpRequest.request() | ||
.withMethod("GET") | ||
.withPath("/actuator/health")) | ||
.respond(org.mockserver.model.HttpResponse.response() | ||
.withStatusCode(HttpStatus.SC_OK) | ||
.withBody("{'status':'UP'}")); | ||
gateway.createAAS(null, "http://localhost:6006", null); | ||
verifyCall("/shells",1); | ||
verifyCall("/actuator/health",1); | ||
} | ||
|
||
@Test(expected = BaSyxComponentNotHealthyException.class) | ||
public void testCreateAASWithUnhealthyBaSyxComponent(){ | ||
new MockServerClient("localhost", 6006) | ||
.when(org.mockserver.model.HttpRequest.request() | ||
.withMethod("GET") | ||
.withPath("/shells")) | ||
.respond(org.mockserver.model.HttpResponse.response() | ||
.withStatusCode(HttpStatus.SC_OK) | ||
.withHeader("AASMiddleware","BaSyx") | ||
.withBody("{}")); | ||
new MockServerClient("localhost", 6006) | ||
.when(org.mockserver.model.HttpRequest.request() | ||
.withMethod("GET") | ||
.withPath("/actuator/health")) | ||
.respond(org.mockserver.model.HttpResponse.response() | ||
.withStatusCode(HttpStatus.SC_OK) | ||
.withBody("{'status':'DOWN'}")); | ||
gateway.createAAS(null, "http://localhost:6006", null); | ||
} | ||
|
||
@Test | ||
public void testCreateAASWithNonBaSyxComponent(){ | ||
new MockServerClient("localhost", 6006) | ||
.when(org.mockserver.model.HttpRequest.request() | ||
.withMethod("GET") | ||
.withPath("/shells")) | ||
.respond(org.mockserver.model.HttpResponse.response() | ||
.withStatusCode(HttpStatus.SC_OK) | ||
.withBody("{}")); | ||
new MockServerClient("localhost", 6006) | ||
.when(org.mockserver.model.HttpRequest.request() | ||
.withMethod("GET") | ||
.withPath("/actuator/health")) | ||
.respond(org.mockserver.model.HttpResponse.response() | ||
.withStatusCode(HttpStatus.SC_OK) | ||
.withBody("{'status':'UP'}")); | ||
gateway.createAAS(null, "http://localhost:6006", null); | ||
verifyCall("/shells",1); | ||
verifyCall("/actuator/health",0); | ||
} | ||
|
||
private void verifyCall(String path, int timesCalled) { | ||
new MockServerClient("localhost", 6006).verify(org.mockserver.model.HttpRequest.request().withMethod("GET") | ||
.withPath(path) | ||
, VerificationTimes.exactly(timesCalled)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.