Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer explicit port to config port #28

Merged
merged 2 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2021 Fraunhofer IOSB, eine rechtlich nicht selbstaendige
* Einrichtung der Fraunhofer-Gesellschaft zur Foerderung der angewandten
* Forschung e.V.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -26,7 +26,7 @@ public interface AssetAdministrationShellServiceManager {

/**
* Boot up an AAS service by environment and port

*
* @param aasModelPath AAS Environment for the AAS service
* @param port AAS service's exposed HTTP port for communication
* with this extension
Expand All @@ -37,22 +37,34 @@ public interface AssetAdministrationShellServiceManager {

/**
* Boot up an AAS service by environment and config

*
* @param aasModelPath AAS Environment for the AAS service
* @param configPath AAS service config
* @return The URL of the new service
* @throws IOException If the URL creation fails
*/
URL startService(Path aasModelPath, Path configPath) throws IOException;

/**
* Boot up an AAS service by environment and config
*
* @param aasModelPath AAS Environment for the AAS service
* @param configPath AAS service config
* @param port AAS service's exposed HTTP port for communication
* with this extension
* @return The URL of the new service
* @throws IOException If the URL creation fails
*/
URL startService(Path aasModelPath, Path configPath, int port) throws IOException;

/**
* Stop all running AAS services that were started by this manager
*/
void stopServices();

/**
* Stop an AAS service by URL

*
* @param aasServiceUrl AAS service to be stopped
*/
void stopService(URL aasServiceUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2021 Fraunhofer IOSB, eine rechtlich nicht selbstaendige
* Einrichtung der Fraunhofer-Gesellschaft zur Foerderung der angewandten
* Forschung e.V.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand All @@ -15,17 +15,6 @@
*/
package de.fraunhofer.iosb.app.aas;

import static java.lang.String.format;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.edc.spi.EdcException;

import de.fraunhofer.iosb.app.Logger;
import de.fraunhofer.iosb.ilt.faaast.service.Service;
import de.fraunhofer.iosb.ilt.faaast.service.config.CoreConfig;
Expand All @@ -35,6 +24,17 @@
import de.fraunhofer.iosb.ilt.faaast.service.persistence.memory.PersistenceInMemoryConfig;
import de.fraunhofer.iosb.ilt.faaast.service.starter.util.AASEnvironmentHelper;
import de.fraunhofer.iosb.ilt.faaast.service.starter.util.ServiceConfigHelper;
import org.eclipse.edc.spi.EdcException;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import static java.lang.String.format;

/**
* Manages internally created FA³ST instances.
Expand All @@ -45,7 +45,7 @@ public class FaaastServiceManager implements AssetAdministrationShellServiceMana
private static final String LOCALHOST_URL = "http://localhost:";

private final Logger logger;
private Map<URL, Service> faaastServiceRepository;
private final Map<URL, Service> faaastServiceRepository;

public FaaastServiceManager() {
logger = Logger.getInstance();
Expand All @@ -55,6 +55,11 @@ public FaaastServiceManager() {
@Override
public URL startService(Path aasModelPath, int port) throws IOException {
Objects.requireNonNull(aasModelPath);
if (!isValidPort(port)) {
var errorMessage = format("Port is not valid: (%s).", port);
logger.error(errorMessage);
throw new EdcException(errorMessage);
}
logger.debug(format("Booting up FA³ST service using AAS model path (%s) and service port (%s).", aasModelPath,
port));

Expand All @@ -75,61 +80,87 @@ public URL startService(Path aasModelPath, int port) throws IOException {
service.start();
logger.debug("Booted up FA³ST service.");

faaastServiceRepository.put(new URL(LOCALHOST_URL + String.valueOf(port)), service);
faaastServiceRepository.put(new URL(LOCALHOST_URL + port), service);

} catch (Exception faaastServiceException) {
throw new EdcException(FAAAST_SERVICE_EXCEPTION_MESSAGE, faaastServiceException);
}

return new URL(LOCALHOST_URL + String.valueOf(port));
return new URL(LOCALHOST_URL + port);
}

@Override
public URL startService(Path aasModelPath, Path configPath) throws IOException {
Objects.requireNonNull(aasModelPath);
Objects.requireNonNull(configPath);
logger.debug(format("Booting up FA³ST service using AAS model path (%s) and FA³ST config path (%s).",
aasModelPath, configPath));
return startService(aasModelPath, configPath, -1);
}

@Override
public URL startService(Path aasModelPath, Path configPath, int port) throws IOException {
if (isValidPort(port)) {
logger.debug(format("Booting up FA³ST service using AAS model path (%s), FA³ST config path (%s) and service port (%s).",
aasModelPath, configPath, port));
} else {
logger.debug(format("Booting up FA³ST service using AAS model path (%s) and FA³ST config path (%s).",
aasModelPath, configPath));
}

var localFaaastServicePort = 0;
try {
var aasEnvironment = AASEnvironmentHelper.fromFile(aasModelPath.toFile());

var serviceConfig = ServiceConfigHelper.load(configPath.toFile());
if (serviceConfig.getEndpoints().isEmpty()) {
throw new IllegalArgumentException(
"No HTTP endpoint has been defined in this configuration. Not booting up this FA³ST service.");
}
var isEndpointsNull = (serviceConfig.getEndpoints() == null); // Remove auto generated httpEndpoint later...
ServiceConfigHelper.autoComplete(serviceConfig);

for (var endpointConfig : serviceConfig.getEndpoints()) {
if (endpointConfig instanceof HttpEndpointConfig) {
localFaaastServicePort = ((HttpEndpointConfig) endpointConfig).getPort();
if (isValidPort(port)) {
if (isEndpointsNull) {
serviceConfig.setEndpoints(List.of(new HttpEndpointConfig.Builder().port(port).build()));
} else {
var endpoints = serviceConfig.getEndpoints();
endpoints.add(new HttpEndpointConfig.Builder().port(port).build());
serviceConfig.setEndpoints(endpoints);
}
localFaaastServicePort = port;

} else {
for (var endpointConfig : serviceConfig.getEndpoints()) {
if (endpointConfig instanceof HttpEndpointConfig) {
localFaaastServicePort = ((HttpEndpointConfig) endpointConfig).getPort();
}
}
}

// If localFaaastServicePort is unchanged, no valid HTTP Endpoint was found/created.
if (localFaaastServicePort == 0) {
throw new IllegalArgumentException(
"No HTTP endpoint has been defined in this configuration. Not booting up this FA³ST service.");
}

var service = new Service(aasEnvironment, serviceConfig);
service.start();
logger.debug("Booted up FA³ST service.");

faaastServiceRepository.put(new URL(LOCALHOST_URL + String.valueOf(localFaaastServicePort)), service);
faaastServiceRepository.put(new URL(LOCALHOST_URL + localFaaastServicePort), service);

} catch (Exception faaastServiceException) {
} catch (
Exception faaastServiceException) {
throw new EdcException(FAAAST_SERVICE_EXCEPTION_MESSAGE, faaastServiceException);
}
return new URL(LOCALHOST_URL + String.valueOf(localFaaastServicePort));
return new URL(LOCALHOST_URL + localFaaastServicePort);
}

@Override
public void stopServices() {
logger.debug("Shutting down all internally started FA³ST services...");
faaastServiceRepository.values().forEach(service -> service.stop());
faaastServiceRepository.values().forEach(Service::stop);
}

@Override
public void stopService(URL aasServiceUrl) {
Objects.requireNonNull(aasServiceUrl);
logger.debug(format("Shutting down FA³ST service with URL %s...", aasServiceUrl.toString()));
logger.debug(format("Shutting down FA³ST service with URL %s...", aasServiceUrl));
var serviceToStop = faaastServiceRepository.get(aasServiceUrl);
if (Objects.nonNull(serviceToStop)) {
serviceToStop.stop();
Expand All @@ -139,4 +170,8 @@ public void stopService(URL aasServiceUrl) {
}
}

private boolean isValidPort(int port) {
return port < 65536 && port > 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public URL startService(Path aasModelPath, int aasServicePort, Path aasConfigPat
logger.log(format(
"Booting up AAS service given AAS model path (%s)\n and service config path (%s)...",
aasModelPath, aasConfigPath));
return aasServiceManager.startService(aasModelPath, aasConfigPath);
return aasServiceManager.startService(aasModelPath, aasConfigPath, aasServicePort);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) 2021 Fraunhofer IOSB, eine rechtlich nicht selbstaendige
* Einrichtung der Fraunhofer-Gesellschaft zur Foerderung der angewandten
* Forschung e.V.
*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
Expand Down Expand Up @@ -68,26 +68,24 @@ public void startServiceFalsePathTest() throws IOException {
}

@Test
public void startServiceFalsePortTest() throws IOException {
try {
faaastServiceManager.startService(Path.of("./src/test/resources/aasEnvironment.json"), -800);
fail("EdcException should have been thrown");
} catch (EdcException expected) {
}
public void startServiceOverwritePortTest() throws IOException {
Path testPath = Path.of("./src/test/resources/aasEnvironment.json");
// Fa³st config path irrelevant, configHelper creates new config with port 8080
var response = faaastServiceManager.startService(testPath, testPath, 12345);
assertEquals(12345, response.getPort());
}

@Test
public void startServiceFalsePortConfigPathTest() throws IOException {
public void startServiceFalsePortTest() throws IOException {
try {
faaastServiceManager.startService(Path.of("./src/test/resources/aasEnvironment.json"),
Path.of("./src/test/resources/aasEnvironment.json"));
faaastServiceManager.startService(Path.of("./src/test/resources/aasEnvironment.json"), -800);
fail("EdcException should have been thrown");
} catch (EdcException expected) {
}
}

@Test
public void stopServicesEmptyRepositoryTest() throws IOException {
public void stopServicesEmptyRepositoryTest() {
try {
faaastServiceManager.stopServices();
} catch (Exception failed) {
Expand All @@ -96,12 +94,11 @@ public void stopServicesEmptyRepositoryTest() throws IOException {
}

@Test
public void stopServiceEmptyRepositoryTest() throws IOException {
public void stopServiceEmptyRepositoryTest() {
try {
faaastServiceManager.stopService(new URL("http://does-not-exist.com:1234/aas"));
fail("This operation should fail");
} catch (IllegalArgumentException expectedException) {
return;
} catch (IllegalArgumentException ignored) {
} catch (Exception unexpectedException) {
fail();
}
Expand Down