Skip to content

Commit

Permalink
Modify output of ConnectedAasManager to be Connected*Service (eclipse…
Browse files Browse the repository at this point in the history
…-basyx#288)

* Change output from getters to be Connected*Service instead of AAS4j objs

* Refactor parsing logic

* Remove unused methods
  • Loading branch information
mateusmolina-iese authored May 24, 2024
1 parent 5bbf0df commit 17f58ad
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
import org.eclipse.digitaltwin.basyx.aasregistry.client.model.AssetAdministrationShellDescriptor;
import org.eclipse.digitaltwin.basyx.aasrepository.client.ConnectedAasRepository;
import org.eclipse.digitaltwin.basyx.aasrepository.feature.registry.integration.AasDescriptorFactory;
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.api.SubmodelRegistryApi;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.model.SubmodelDescriptor;
import org.eclipse.digitaltwin.basyx.submodelrepository.client.ConnectedSubmodelRepository;
import org.eclipse.digitaltwin.basyx.submodelrepository.feature.registry.integration.SubmodelDescriptorFactory;
import org.eclipse.digitaltwin.basyx.submodelservice.client.ConnectedSubmodelService;

/**
* Client component for executing consolidated Repository and Registry requests
Expand Down Expand Up @@ -87,13 +89,13 @@ public ConnectedAasManager(String aasRegistryBaseUrl, String aasRepositoryBaseUr
}

/**
* Retrieves an AAS in an AAS registry by its identifier.
* Retrieves a ConnectedAasService in an AAS registry by its identifier.
*
* @param identifier
* The identifier of the AAS to retrieve.
* @return The retrieved AAS object.
* @return The retrieved ConnectedAasService object.
*/
public AssetAdministrationShell getAas(String identifier) throws NoValidEndpointFoundException {
public ConnectedAasService getAas(String identifier) throws NoValidEndpointFoundException {
AssetAdministrationShellDescriptor descriptor;

try {
Expand All @@ -105,13 +107,14 @@ public AssetAdministrationShell getAas(String identifier) throws NoValidEndpoint
}

/**
* Retrieves a Submodel in a Submodel registry by its identifier.
* Retrieves a ConnectedSubmodelService in a Submodel registry by its
* identifier.
*
* @param identifier
* The identifier of the submodel to retrieve.
* @return The retrieved Submodel object.
* @return The retrieved ConnectedSubmodelService object.
*/
public Submodel getSubmodel(String identifier) {
public ConnectedSubmodelService getSubmodel(String identifier) {
SubmodelDescriptor descriptor;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.AasDescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.EndpointResolver;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.SubmodelDescriptorResolver;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.parsers.AasRegistryEndpointURIParser;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.parsers.SubmodelRegistryEndpointURIParser;
import org.eclipse.digitaltwin.basyx.aasrepository.feature.registry.integration.AasDescriptorFactory;
import org.eclipse.digitaltwin.basyx.aasrepository.feature.registry.integration.mapper.AttributeMapper;
import org.eclipse.digitaltwin.basyx.http.Aas4JHTTPSerializationExtension;
Expand All @@ -53,28 +51,28 @@ class ConnectedAasManagerHelper {
private ConnectedAasManagerHelper() {
}

public static ObjectMapper buildObjectMapper() {
static ObjectMapper buildObjectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder().serializationInclusion(JsonInclude.Include.NON_NULL);
Aas4JHTTPSerializationExtension extension = new Aas4JHTTPSerializationExtension();
extension.extend(builder);
return builder.build();
}

public static AasDescriptorResolver buildAasDescriptorResolver() {
return new AasDescriptorResolver(new EndpointResolver<>(new AasRegistryEndpointURIParser()));
static AasDescriptorResolver buildAasDescriptorResolver() {
return new AasDescriptorResolver(new EndpointResolver());
}

public static SubmodelDescriptorResolver buildSubmodelDescriptorResolver() {
return new SubmodelDescriptorResolver(new EndpointResolver<>(new SubmodelRegistryEndpointURIParser()));
static SubmodelDescriptorResolver buildSubmodelDescriptorResolver() {
return new SubmodelDescriptorResolver(new EndpointResolver());
}

public static AasDescriptorFactory buildAasDescriptorFactory(String aasRepositoryBaseUrl) {
static AasDescriptorFactory buildAasDescriptorFactory(String aasRepositoryBaseUrl) {
AttributeMapper attributeMapper = new AttributeMapper(objectMapper);

return new AasDescriptorFactory(null, aasRepositoryBaseUrl, attributeMapper);
}

public static SubmodelDescriptorFactory buildSmDescriptorFactory(String aasRepositoryBaseUrl) {
static SubmodelDescriptorFactory buildSmDescriptorFactory(String aasRepositoryBaseUrl) {
org.eclipse.digitaltwin.basyx.submodelrepository.feature.registry.integration.mapper.AttributeMapper attributeMapperSm = new org.eclipse.digitaltwin.basyx.submodelrepository.feature.registry.integration.mapper.AttributeMapper(
objectMapper);
return new SubmodelDescriptorFactory(null, aasRepositoryBaseUrl, attributeMapperSm);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@

package org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers;

import org.eclipse.digitaltwin.aas4j.v3.model.AssetAdministrationShell;
import java.net.URI;
import java.util.Optional;

import org.eclipse.digitaltwin.basyx.aasregistry.client.model.AssetAdministrationShellDescriptor;
import org.eclipse.digitaltwin.basyx.aasregistry.client.model.Endpoint;
import org.eclipse.digitaltwin.basyx.aasservice.client.ConnectedAasService;
Expand All @@ -38,18 +40,30 @@
*/
public class AasDescriptorResolver {

private final EndpointResolver<Endpoint> endpointResolver;
private final EndpointResolver endpointResolver;

public AasDescriptorResolver(EndpointResolver<Endpoint> endpointResolver) {
public AasDescriptorResolver(EndpointResolver endpointResolver) {
this.endpointResolver = endpointResolver;
}

public AssetAdministrationShell resolveAasDescriptor(AssetAdministrationShellDescriptor aasDescriptor) {
String endpoint = endpointResolver.resolveFirst(aasDescriptor.getEndpoints());

ConnectedAasService aasService = new ConnectedAasService(endpoint);
public ConnectedAasService resolveAasDescriptor(AssetAdministrationShellDescriptor aasDescriptor) {
String endpoint = endpointResolver.resolveFirst(aasDescriptor.getEndpoints(), AasDescriptorResolver::parseEndpoint);

return aasService.getAAS();
return new ConnectedAasService(endpoint);
}

private static Optional<URI> parseEndpoint(Endpoint endpoint) {
try {
if (endpoint == null || endpoint.getProtocolInformation() == null || endpoint.getProtocolInformation().getHref() == null)
return Optional.empty();

String baseHref = endpoint.getProtocolInformation().getHref();
// TODO not working: String queryString = "?" + endpoint.toUrlQueryString();
String queryString = "";
URI uri = new URI(baseHref + queryString);
return Optional.of(uri);
} catch (Exception e) {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,31 @@
import java.util.Optional;

import org.eclipse.digitaltwin.basyx.aasenvironment.client.exceptions.NoValidEndpointFoundException;
import org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.parsers.URIParser;

/**
* Resolves a list of endpoints based on find first working strategy
*
* @author mateusmolina
*
*/
public class EndpointResolver<T> {
public class EndpointResolver {

private final URIParser<T> parser;

private int timeout = 3000;

public EndpointResolver(URIParser<T> parser) {
this.parser = parser;
public EndpointResolver() {
}

public EndpointResolver(URIParser<T> parser, int timeout) {
this(parser);
public EndpointResolver(int timeout) {
this.timeout = timeout;
}

public String resolveFirst(List<T> endpoints) {
List<URI> uris = endpoints.stream().map(parser::parse).flatMap(Optional::stream).toList();
public <T> String resolveFirst(List<T> endpoints, URIParser<T> uriParser) {
List<URI> uris = endpoints.stream().map(uriParser::parse).flatMap(Optional::stream).toList();
return findFirstWorkingURI(uris).orElseThrow(() -> new NoValidEndpointFoundException(endpoints.toString())).toString();
}

public List<String> resolveAll(List<T> endpoints) {
return endpoints.stream().map(parser::parse).flatMap(Optional::stream).filter(this::isURIWorking).map(URI::toString).toList();
public <T> List<String> resolveAll(List<T> endpoints, URIParser<T> uriParser) {
return endpoints.stream().map(uriParser::parse).flatMap(Optional::stream).filter(this::isURIWorking).map(URI::toString).toList();
}

private Optional<URI> findFirstWorkingURI(List<URI> uris) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,9 @@

package org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers;

import org.eclipse.digitaltwin.aas4j.v3.model.Key;
import org.eclipse.digitaltwin.aas4j.v3.model.KeyTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.Reference;
import org.eclipse.digitaltwin.aas4j.v3.model.ReferenceTypes;
import org.eclipse.digitaltwin.aas4j.v3.model.Submodel;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultKey;
import org.eclipse.digitaltwin.aas4j.v3.model.impl.DefaultReference;
import java.net.URI;
import java.util.Optional;

import org.eclipse.digitaltwin.basyx.submodelregistry.client.model.Endpoint;
import org.eclipse.digitaltwin.basyx.submodelregistry.client.model.SubmodelDescriptor;
import org.eclipse.digitaltwin.basyx.submodelservice.client.ConnectedSubmodelService;
Expand All @@ -44,25 +40,30 @@
*/
public class SubmodelDescriptorResolver {

private final EndpointResolver<Endpoint> endpointResolver;
private final EndpointResolver endpointResolver;

public SubmodelDescriptorResolver(EndpointResolver<Endpoint> endpointResolver) {
public SubmodelDescriptorResolver(EndpointResolver endpointResolver) {
this.endpointResolver = endpointResolver;
}

public Submodel resolveSubmodelDescriptor(SubmodelDescriptor smDescriptor) {
String endpoint = endpointResolver.resolveFirst(smDescriptor.getEndpoints());

ConnectedSubmodelService smService = new ConnectedSubmodelService(endpoint);
public ConnectedSubmodelService resolveSubmodelDescriptor(SubmodelDescriptor smDescriptor) {
String endpoint = endpointResolver.resolveFirst(smDescriptor.getEndpoints(), SubmodelDescriptorResolver::parseEndpoint);

return smService.getSubmodel();
return new ConnectedSubmodelService(endpoint);
}

public Reference deriveReferenceFromSubmodelDescriptor(SubmodelDescriptor smDescriptor) {
return new DefaultReference.Builder().type(ReferenceTypes.EXTERNAL_REFERENCE).keys(generateKeyFromId(smDescriptor.getId())).build();
}
private static Optional<URI> parseEndpoint(Endpoint endpoint) {
try {
if (endpoint == null || endpoint.getProtocolInformation() == null || endpoint.getProtocolInformation().getHref() == null)
return Optional.empty();

private static Key generateKeyFromId(String smId) {
return new DefaultKey.Builder().type(KeyTypes.SUBMODEL).value(smId).build();
String baseHref = endpoint.getProtocolInformation().getHref();
// TODO not working: String queryString = "?" + endpoint.toUrlQueryString();
String queryString = "";
URI uri = new URI(baseHref + queryString);
return Optional.of(uri);
} catch (Exception e) {
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* SPDX-License-Identifier: MIT
******************************************************************************/

package org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers.parsers;
package org.eclipse.digitaltwin.basyx.aasenvironment.client.resolvers;

import java.net.URI;
import java.util.Optional;
Expand All @@ -34,6 +34,7 @@
* @author mateusmolina
*
*/
public interface URIParser<T> {
@FunctionalInterface
interface URIParser<T> {
public Optional<URI> parse(T object);
}

This file was deleted.

This file was deleted.

Loading

0 comments on commit 17f58ad

Please sign in to comment.