diff options
author | Benjamin, Max <max.benjamin@att.com> | 2020-04-30 10:41:46 -0400 |
---|---|---|
committer | Benjamin, Max (mb388a) <mb388a@att.com> | 2020-04-30 11:08:10 -0400 |
commit | e7f7527a7ad0397f9f98c739f6f4dfba357bcf3a (patch) | |
tree | 96db50b73b0a7d096683ecb0672c79cf6897ee90 /graph-inventory/aai-client | |
parent | 0fdf096ae4ff4ee4b7a644549bd67d75e4aa82b1 (diff) |
add fluent type builder support to A&AI client
add fluent type builder support to A&AI client
Issue-ID: SO-2856
Signed-off-by: Benjamin, Max (mb388a) <mb388a@att.com>
Change-Id: I2957aedb84a1c6b23979ff2e1c4dfb16b3a646c7
Diffstat (limited to 'graph-inventory/aai-client')
23 files changed, 435 insertions, 23 deletions
diff --git a/graph-inventory/aai-client/pom.xml b/graph-inventory/aai-client/pom.xml index c8b10cce1f..754f1bb1f4 100644 --- a/graph-inventory/aai-client/pom.xml +++ b/graph-inventory/aai-client/pom.xml @@ -7,6 +7,125 @@ <version>1.6.0-SNAPSHOT</version> </parent> <artifactId>aai-client</artifactId> + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.eclipse.m2e</groupId> + <artifactId>lifecycle-mapping</artifactId> + <version>1.0.0</version> + <configuration> + <lifecycleMappingMetadata> + <pluginExecutions> + <pluginExecution> + <pluginExecutionFilter> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <versionRange>[1.0.0,)</versionRange> + <goals> + <goal>unpack</goal> + </goals> + </pluginExecutionFilter> + <action> + <execute /> + </action> + </pluginExecution> + <pluginExecution> + <pluginExecutionFilter> + <groupId>org.onap.so</groupId> + <artifactId>fluent-builder-maven-plugin</artifactId> + <goals> + <goal>generate-builders</goal> + </goals> + <versionRange>[0.0,)</versionRange> + </pluginExecutionFilter> + <action> + <execute /> + </action> + </pluginExecution> + </pluginExecutions> + </lifecycleMappingMetadata> + </configuration> + </plugin> + </plugins> + </pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-dependency-plugin</artifactId> + <executions> + <execution> + <id>unpack</id> + <phase>generate-sources</phase> + <goals> + <goal>unpack</goal> + </goals> + <configuration> + <artifactItems> + <artifactItem> + <groupId>org.onap.aai.traversal</groupId> + <artifactId>aai-traversal</artifactId> + <version>1.6.3</version> + <outputDirectory>${project.build.directory}/antlr</outputDirectory> + <includes>**/*.g4</includes> + </artifactItem> + <artifactItem> + <groupId>org.onap.aai.schema-service</groupId> + <artifactId>aai-schema</artifactId> + <version>1.6.5</version> + <outputDirectory>${project.build.directory}/swagger</outputDirectory> + <includes>**/*.yaml</includes> + </artifactItem> + </artifactItems> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.onap.so</groupId> + <artifactId>fluent-builder-maven-plugin</artifactId> + <version>${project.version}</version> + <executions> + <execution> + <goals> + <goal>generate-builders</goal> + </goals> + <configuration> + <destination>${project.build.directory}/generated-sources</destination> + <destinationClasspath>org.onap.aaiclient.client.generated.fluentbuilders</destinationClasspath> + <builderName>AAIFluentTypeBuilder</builderName> + <swaggerLocation>${project.build.directory}/swagger/onap/aai_swagger_yaml/aai_swagger_v19.yaml</swaggerLocation> + <singularBuilderClass>org.onap.aaiclient.client.aai.entities.uri.AAIFluentSingleType</singularBuilderClass> + <pluralBuilderClass>org.onap.aaiclient.client.aai.entities.uri.AAIFluentPluralType</pluralBuilderClass> + <topLevelBuilderClass>org.onap.aaiclient.client.aai.entities.uri.AAIFluentTopLevelType</topLevelBuilderClass> + <baseBuilderClass>org.onap.aaiclient.client.graphinventory.GraphInventoryFluentTypeBase</baseBuilderClass> + <singularClass>org.onap.aaiclient.client.aai.AAIObjectType</singularClass> + <pluralClass>org.onap.aaiclient.client.aai.AAIObjectPlurals</pluralClass> + <nameClass>org.onap.aaiclient.client.aai.AAIObjectName</nameClass> + </configuration> + </execution> + </executions> + </plugin> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>build-helper-maven-plugin</artifactId> + <executions> + <execution> + <id>add-source</id> + <phase>generate-sources</phase> + <goals> + <goal>add-source</goal> + </goals> + <configuration> + <sources> + <source>${project.build.directory}/generated-sources/fluent/</source> + </sources> + </configuration> + </execution> + </executions> + </plugin> + </plugins> + </build> <dependencies> <dependency> <groupId>org.onap.so</groupId> diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIDSLQueryClient.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIDSLQueryClient.java index 4c45a675b2..238e87392f 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIDSLQueryClient.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIDSLQueryClient.java @@ -21,6 +21,7 @@ package org.onap.aaiclient.client.aai; import org.onap.aaiclient.client.aai.entities.AAIResultWrapper; +import org.onap.aaiclient.client.aai.entities.uri.AAIFluentTypeReverseLookup; import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory; import org.onap.aaiclient.client.graphinventory.GraphInventoryQueryClient; import org.onap.aaiclient.client.graphinventory.entities.DSLQuery; @@ -49,8 +50,8 @@ public class AAIDSLQueryClient } @Override - public AAIObjectType createType(String name) { - return AAIObjectType.fromTypeName(name); + public AAIObjectType createType(String name, String uri) { + return new AAIFluentTypeReverseLookup().fromName(name, uri); } } diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectName.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectName.java new file mode 100644 index 0000000000..180a99e88b --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectName.java @@ -0,0 +1,23 @@ +package org.onap.aaiclient.client.aai; + +import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName; +import com.google.common.base.CaseFormat; + +public class AAIObjectName implements GraphInventoryObjectName { + + private final String name; + + public AAIObjectName(String name) { + this.name = name; + } + + @Override + public String typeName() { + return name; + } + + @Override + public String typeName(CaseFormat format) { + return CaseFormat.LOWER_HYPHEN.to(format, this.name); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectPlurals.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectPlurals.java index fcda3c8ad4..5b68d0c69a 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectPlurals.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectPlurals.java @@ -77,12 +77,18 @@ public class AAIObjectPlurals implements AAIObjectBase, GraphInventoryObjectPlur private final String uriTemplate; private final String partialUri; - private final AAIObjectType type; + private final String name; protected AAIObjectPlurals(AAIObjectType type, String parentUri, String partialUri) { this.uriTemplate = parentUri + partialUri; this.partialUri = partialUri; - this.type = type; + this.name = type.typeName(); + } + + public AAIObjectPlurals(String name, String parentUri, String partialUri) { + this.uriTemplate = parentUri + partialUri; + this.partialUri = partialUri; + this.name = name; } @Override @@ -101,17 +107,27 @@ public class AAIObjectPlurals implements AAIObjectBase, GraphInventoryObjectPlur } @Override - public AAIObjectType getType() { - return this.type; + public String typeName() { + return this.typeName(CaseFormat.LOWER_HYPHEN); } @Override - public String typeName() { - return this.getType().typeName(); + public String typeName(CaseFormat format) { + return CaseFormat.LOWER_HYPHEN.to(format, this.name.replace("default-", "")); } @Override - public String typeName(CaseFormat format) { - return this.getType().typeName(format); + public int hashCode() { + return this.typeName().hashCode(); + } + + @Override + public boolean equals(Object o) { + + if (o instanceof AAIObjectBase) { + return this.typeName().equals(((AAIObjectBase) o).typeName()); + } + + return false; } } diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectType.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectType.java index 8045a220a3..b3402fa221 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectType.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIObjectType.java @@ -74,6 +74,7 @@ import org.onap.aai.domain.yang.VolumeGroup; import org.onap.aai.domain.yang.VpnBinding; import org.onap.aai.domain.yang.Vserver; import org.onap.aai.domain.yang.Zone; +import org.onap.aaiclient.client.aai.entities.uri.AAIFluentTypeReverseLookup; import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectType; import org.onap.so.constants.Defaults; import org.reflections.Reflections; @@ -262,12 +263,16 @@ public class AAIObjectType implements AAIObjectBase, GraphInventoryObjectType, S } protected AAIObjectType(String parentUri, String partialUri, String name) { + this(parentUri, partialUri, name, true); + } + + public AAIObjectType(String parentUri, String partialUri, String name, boolean register) { this.parentUri = parentUri; this.partialUri = partialUri; this.uriTemplate = parentUri + partialUri; this.aaiObjectClass = null; this.name = name; - if (!AAIObjectType.map.containsKey(name)) { + if (register && !AAIObjectType.map.containsKey(name)) { AAIObjectType.map.put(name, this); } } @@ -288,6 +293,11 @@ public class AAIObjectType implements AAIObjectBase, GraphInventoryObjectType, S return this.uriTemplate(); } + public static AAIObjectType fromTypeName(String name, String uri) { + + return new AAIFluentTypeReverseLookup().fromName(name, uri); + } + public static AAIObjectType fromTypeName(String name) { if (map.containsKey(name)) { return map.get(name); @@ -316,6 +326,21 @@ public class AAIObjectType implements AAIObjectBase, GraphInventoryObjectType, S return this.partialUri; } + @Override + public int hashCode() { + return this.typeName().hashCode(); + } + + @Override + public boolean equals(Object o) { + + if (o instanceof AAIObjectBase) { + return this.typeName().equals(((AAIObjectBase) o).typeName()); + } + + return false; + } + protected String removeParentUri(Class<?> aaiObjectClass, String parentUri) { return aaiObjectClass.getAnnotation(Metadata.class).uriTemplate().replaceFirst(Pattern.quote(parentUri), ""); } diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIQueryClient.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIQueryClient.java index 5e8b052146..75d4a6ff56 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIQueryClient.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/AAIQueryClient.java @@ -22,6 +22,7 @@ package org.onap.aaiclient.client.aai; import org.onap.aaiclient.client.aai.entities.AAIResultWrapper; import org.onap.aaiclient.client.aai.entities.CustomQuery; +import org.onap.aaiclient.client.aai.entities.uri.AAIFluentTypeReverseLookup; import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory; import org.onap.aaiclient.client.graphinventory.GraphInventoryQueryClient; import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventoryUri; @@ -53,8 +54,8 @@ public class AAIQueryClient } @Override - public AAIObjectType createType(String name) { - return AAIObjectType.fromTypeName(name); + public AAIObjectType createType(String name, String uri) { + return new AAIFluentTypeReverseLookup().fromName(name, uri); } } diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/Relationships.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/Relationships.java index fb205310fc..b8a80f082c 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/Relationships.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/Relationships.java @@ -24,6 +24,7 @@ import java.util.List; import javax.ws.rs.core.UriBuilder; import org.onap.aaiclient.client.aai.AAIObjectType; import org.onap.aaiclient.client.aai.AAIResourcesClient; +import org.onap.aaiclient.client.aai.entities.uri.AAIFluentTypeReverseLookup; import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri; import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory; import org.onap.aaiclient.client.graphinventory.GraphInventoryObjectName; @@ -68,7 +69,7 @@ public class Relationships extends GraphInventoryRelationships<AAIResultWrapper, } @Override - protected AAIObjectType fromTypeName(String name) { - return AAIObjectType.fromTypeName(name); + protected AAIObjectType fromTypeName(String name, String uri) { + return new AAIFluentTypeReverseLookup().fromName(name, uri); } } diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentPluralType.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentPluralType.java new file mode 100644 index 0000000000..ea81e7b95c --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentPluralType.java @@ -0,0 +1,12 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import org.onap.aaiclient.client.aai.AAIObjectPlurals; +import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentType; + +public interface AAIFluentPluralType extends GraphInventoryFluentType<AAIObjectPlurals> { + + public interface Info extends GraphInventoryFluentType.Info { + + + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentSingleType.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentSingleType.java new file mode 100644 index 0000000000..9310933499 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentSingleType.java @@ -0,0 +1,12 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentType; + +public interface AAIFluentSingleType extends GraphInventoryFluentType<AAIObjectType> { + + public interface Info extends GraphInventoryFluentType.Info { + + + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTopLevelType.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTopLevelType.java new file mode 100644 index 0000000000..46a521d82e --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTopLevelType.java @@ -0,0 +1,11 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentTopLevelType; + +public interface AAIFluentTopLevelType extends GraphInventoryFluentTopLevelType { + + public interface Info extends GraphInventoryFluentTopLevelType.Info { + + + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookup.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookup.java new file mode 100644 index 0000000000..bd9f4c713f --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookup.java @@ -0,0 +1,51 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import java.util.List; +import java.util.Optional; +import java.util.stream.Collectors; +import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.graphinventory.GraphInventoryFluentType; +import org.onap.aaiclient.client.graphinventory.entities.uri.parsers.UriParserSpringImpl; +import com.google.common.base.CaseFormat; + +public class AAIFluentTypeReverseLookup { + + public AAIObjectType fromName(String name, String uri) { + + String className = CaseFormat.LOWER_HYPHEN.to(CaseFormat.UPPER_CAMEL, name); + + uri = uri.replaceFirst(".*?/v\\d+", ""); + try { + Class<? extends GraphInventoryFluentType.Info> clazz = + (Class<? extends GraphInventoryFluentType.Info>) Class + .forName("org.onap.aaiclient.client.generated.fluentbuilders." + className + "$Info"); + + GraphInventoryFluentType.Info type = clazz.newInstance(); + + Optional<String> parentTemplate = findParentPath(type, uri); + if (parentTemplate.isPresent()) { + return new AAIObjectType(parentTemplate.get(), type.getPartialUri(), type.getName(), false); + } else { + // fallback to enum lookup + return AAIObjectType.fromTypeName(name); + } + } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { + } + return AAIObjectType.UNKNOWN; + } + + protected Optional<String> findParentPath(GraphInventoryFluentType.Info type, String uri) { + + List<UriParserSpringImpl> parsers = + type.getPaths().stream().map(path -> new UriParserSpringImpl(path)).collect(Collectors.toList()); + + for (UriParserSpringImpl parser : parsers) { + if (parser.isMatch(uri)) { + String partialUriReplacer = type.getPartialUri().replaceAll("\\{[^}]+\\}", "[^/]+"); + return Optional.of(parser.getTemplate().replaceFirst(partialUriReplacer + "$", "")); + } + } + + return Optional.empty(); + } +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactory.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactory.java index 31b55958d6..6bb2cbd471 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactory.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/aai/entities/uri/AAIUriFactory.java @@ -47,6 +47,10 @@ public class AAIUriFactory { } } + public static AAIResourceUri createResourceUri(AAIFluentSingleType uri) { + return new AAISimpleUri(uri.build(), uri.values()); + } + public static NodesSingleUri createNodesUri(AAIObjectType type, Object... values) { return new NodesSingleUri(type, values); @@ -88,6 +92,12 @@ public class AAIUriFactory { return new AAISimplePluralUri(parentUri, childType); } + public static AAISimplePluralUri createResourceUri(AAIFluentPluralType uri) { + + return new AAISimplePluralUri(uri.build()); + + } + /** * Creates a uri for a plural type e.g. /cloud-infrastructure/pservers * diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryFluentTopLevelType.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryFluentTopLevelType.java new file mode 100644 index 0000000000..9740016d17 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryFluentTopLevelType.java @@ -0,0 +1,5 @@ +package org.onap.aaiclient.client.graphinventory; + +public interface GraphInventoryFluentTopLevelType extends GraphInventoryFluentTypeBase { + +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryFluentType.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryFluentType.java new file mode 100644 index 0000000000..94fa240244 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryFluentType.java @@ -0,0 +1,10 @@ +package org.onap.aaiclient.client.graphinventory; + +public interface GraphInventoryFluentType<T> extends GraphInventoryFluentTypeBase { + + public interface Info extends GraphInventoryFluentTypeBase.Info { + String getName(); + } + + T build(); +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryFluentTypeBase.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryFluentTypeBase.java new file mode 100644 index 0000000000..1a258fcd88 --- /dev/null +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryFluentTypeBase.java @@ -0,0 +1,16 @@ +package org.onap.aaiclient.client.graphinventory; + +import java.util.List; + +public interface GraphInventoryFluentTypeBase { + + public interface Info { + String getPartialUri(); + + List<String> getPaths(); + } + + Object[] values(); + + String uriTemplate(); +} diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryObjectPlurals.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryObjectPlurals.java index 3eb71b21c2..a2d31f80b5 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryObjectPlurals.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryObjectPlurals.java @@ -22,6 +22,4 @@ package org.onap.aaiclient.client.graphinventory; public interface GraphInventoryObjectPlurals extends GraphInventoryObjectBase { - public GraphInventoryObjectType getType(); - } diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java index 8aa03e2920..c749561e5f 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/GraphInventoryQueryClient.java @@ -95,8 +95,8 @@ public abstract class GraphInventoryQueryClient<S, I, Wrapper extends GraphInven if (!entrySet.getKey().equals("url")) { String url = (String) m.get("url"); String stringJson = mapper.writeValueAsString(entrySet.getValue()); - result.add( - new ResourceAndUrl<Wrapper>(url, createType(entrySet.getKey()), createWrapper(stringJson))); + result.add(new ResourceAndUrl<Wrapper>(url, createType(entrySet.getKey(), url), + createWrapper(stringJson))); } } } @@ -106,7 +106,7 @@ public abstract class GraphInventoryQueryClient<S, I, Wrapper extends GraphInven public abstract Wrapper createWrapper(String json); - public abstract Type createType(String name); + public abstract Type createType(String name, String uri); public S depth(String depth) { this.depth = Optional.of(depth); diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryRelationships.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryRelationships.java index 7d960877b0..48feba25df 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryRelationships.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/GraphInventoryRelationships.java @@ -86,8 +86,8 @@ public abstract class GraphInventoryRelationships<Wrapper extends GraphInventory final String relatedTo = (String) relationship.get("related-to"); if (p.test(relatedTo)) { Type type; - type = fromTypeName(relatedTo); final String relatedLink = (String) relationship.get("related-link"); + type = fromTypeName(relatedTo, relatedLink); result.add(createUri(type, relatedLink)); } @@ -116,7 +116,7 @@ public abstract class GraphInventoryRelationships<Wrapper extends GraphInventory protected abstract Uri createUri(Type type, String relatedLink); - protected abstract Type fromTypeName(String name); + protected abstract Type fromTypeName(String name, String uri); protected List<String> getRelatedLinks(Optional<GraphInventoryObjectName> type) { String matcher = ""; diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParser.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParser.java index 24a20240e0..c6a2f96550 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParser.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParser.java @@ -27,4 +27,8 @@ public interface UriParser { public Set<String> getVariables(); public Map<String, String> parse(final String uri); + + public boolean isMatch(final String uri); + + public String getTemplate(); } diff --git a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParserSpringImpl.java b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParserSpringImpl.java index db13ef742a..291e395373 100644 --- a/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParserSpringImpl.java +++ b/graph-inventory/aai-client/src/main/java/org/onap/aaiclient/client/graphinventory/entities/uri/parsers/UriParserSpringImpl.java @@ -32,9 +32,16 @@ import org.springframework.web.util.UriUtils; public class UriParserSpringImpl implements UriParser { private final UriTemplate uriTemplate; + private final String template; public UriParserSpringImpl(final String template) { this.uriTemplate = new UriTemplate(template); + this.template = template; + } + + @Override + public boolean isMatch(final String uri) { + return this.uriTemplate.matches(uri); } @Override @@ -51,6 +58,11 @@ public class UriParserSpringImpl implements UriParser { return Collections.unmodifiableSet(new LinkedHashSet<String>(this.uriTemplate.getVariableNames())); } + @Override + public String getTemplate() { + return this.template; + } + protected Map<String, String> decodeParams(Map<String, String> map) { final Map<String, String> result = new LinkedHashMap<>(); diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIResourcesClientTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIResourcesClientTest.java index 4cb79ef9ad..ca361f46d8 100644 --- a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIResourcesClientTest.java +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/AAIResourcesClientTest.java @@ -49,7 +49,6 @@ import org.mockito.junit.MockitoJUnitRunner; import org.onap.aai.domain.yang.GenericVnf; import org.onap.aai.domain.yang.GenericVnfs; import org.onap.aai.domain.yang.Relationship; -import org.onap.so.client.RestClient; import org.onap.aaiclient.client.aai.entities.AAIEdgeLabel; import org.onap.aaiclient.client.aai.entities.AAIResultWrapper; import org.onap.aaiclient.client.aai.entities.uri.AAIPluralResourceUri; @@ -57,6 +56,7 @@ import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri; import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory; import org.onap.aaiclient.client.defaultproperties.DefaultAAIPropertiesImpl; import org.onap.aaiclient.client.graphinventory.exceptions.GraphInventoryMultipleItemsException; +import org.onap.so.client.RestClient; import com.github.tomakehurst.wiremock.admin.NotFoundException; import com.github.tomakehurst.wiremock.junit.WireMockRule; diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookupTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookupTest.java new file mode 100644 index 0000000000..699eac9768 --- /dev/null +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAIFluentTypeReverseLookupTest.java @@ -0,0 +1,74 @@ +package org.onap.aaiclient.client.aai.entities.uri; + +import static org.junit.Assert.assertEquals; +import org.junit.Test; +import org.onap.aaiclient.client.aai.AAIObjectType; + +public class AAIFluentTypeReverseLookupTest { + + + @Test + public void reverseParseEntryUri() { + String cloudRegion = + "http://localhost:8888/aai/v38/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}"; + String newvce = "/aai/v9/network/newvces/newvce/{vnf-id2}"; + + AAIFluentTypeReverseLookup lookup = new AAIFluentTypeReverseLookup(); + AAIObjectType type = lookup.fromName("cloud-region", cloudRegion); + + assertEquals("cloud-region", type.typeName()); + assertEquals("/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}", type.partialUri()); + assertEquals("/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}", + type.uriTemplate()); + + type = lookup.fromName("newvce", newvce); + + assertEquals("newvce", type.typeName()); + assertEquals("/newvces/newvce/{vnf-id2}", type.partialUri()); + assertEquals("/network/newvces/newvce/{vnf-id2}", type.uriTemplate()); + + type = lookup.fromName("unknown-type-of-something", "/some/endpoint"); + + assertEquals("unknown", type.typeName()); + + } + + @Test + public void reverseParseTest() { + + String pserverParent = + "/aai/v9/cloud-infrastructure/pservers/pserver/{hostname}/lag-interfaces/lag-interface/{interface-name}/l-interfaces/l-interface/{l-interface.interface-name}"; + String cloudRegionParent = + "http://localhost:8888/aai/v38/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}/l-interfaces/l-interface/{interface-name}"; + String newVceParent = "/network/newvces/newvce/{vnf-id2}/l-interfaces/l-interface/{l-interface.interface-name}"; + + + AAIFluentTypeReverseLookup lookup = new AAIFluentTypeReverseLookup(); + + AAIObjectType type = lookup.fromName("l-interface", pserverParent); + + assertEquals("l-interface", type.typeName()); + assertEquals("/l-interfaces/l-interface/{l-interface.interface-name}", type.partialUri()); + assertEquals( + "/cloud-infrastructure/pservers/pserver/{hostname}/lag-interfaces/lag-interface/{interface-name}/l-interfaces/l-interface/{l-interface.interface-name}", + type.uriTemplate()); + + type = lookup.fromName("l-interface", cloudRegionParent); + + assertEquals("l-interface", type.typeName()); + assertEquals("/l-interfaces/l-interface/{l-interface.interface-name}", type.partialUri()); + assertEquals( + "/cloud-infrastructure/cloud-regions/cloud-region/{cloud-owner}/{cloud-region-id}/tenants/tenant/{tenant-id}/vservers/vserver/{vserver-id}/l-interfaces/l-interface/{l-interface.interface-name}", + type.uriTemplate()); + + type = lookup.fromName("l-interface", newVceParent); + + assertEquals("l-interface", type.typeName()); + assertEquals("/l-interfaces/l-interface/{l-interface.interface-name}", type.partialUri()); + assertEquals("/network/newvces/newvce/{vnf-id2}/l-interfaces/l-interface/{l-interface.interface-name}", + type.uriTemplate()); + + + + } +} diff --git a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriTest.java b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriTest.java index 3b11c01436..f8dd1726d3 100644 --- a/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriTest.java +++ b/graph-inventory/aai-client/src/test/java/org/onap/aaiclient/client/aai/entities/uri/AAISimpleUriTest.java @@ -31,6 +31,7 @@ import java.util.Map; import org.junit.Test; import org.onap.aaiclient.client.aai.AAIObjectPlurals; import org.onap.aaiclient.client.aai.AAIObjectType; +import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder; import org.onap.aaiclient.client.graphinventory.entities.uri.Depth; public class AAISimpleUriTest { @@ -130,4 +131,14 @@ public class AAISimpleUriTest { assertEquals(e2.build().toString(), uri.build().toString()); } + + @Test + public void fluentBuilderTest() { + AAIResourceUri uri = AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.cloudInfrastructure() + .cloudRegion("cloud1", "cloud-id").tenant("tenant-id").vserver("vserver-id")); + + assertEquals( + "/cloud-infrastructure/cloud-regions/cloud-region/cloud1/cloud-id/tenants/tenant/tenant-id/vservers/vserver/vserver-id", + uri.build().toString()); + } } |