X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=common%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fso%2Fclient%2Fgraphinventory%2FGraphInventoryResourcesClient.java;h=f446e5ac4c3fdce916f83a3a11a7a5ee9da3e7e9;hb=a577f75663e5c8ccddee54337328425c5b2361d2;hp=c7cdb2ff5811042b48edbcfba8fd42302492a483;hpb=7e2f7db5b4007fe470a7ce5794c88d1844021410;p=so.git diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryResourcesClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryResourcesClient.java index c7cdb2ff58..f446e5ac4c 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryResourcesClient.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryResourcesClient.java @@ -21,6 +21,11 @@ package org.onap.so.client.graphinventory; import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.List; import java.util.Map; import java.util.Optional; import javax.ws.rs.NotFoundException; @@ -32,9 +37,13 @@ import org.onap.so.client.RestClient; import org.onap.so.client.RestProperties; import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel; import org.onap.so.client.graphinventory.entities.GraphInventoryResultWrapper; +import org.onap.so.client.graphinventory.entities.uri.GraphInventoryPluralResourceUri; import org.onap.so.client.graphinventory.entities.uri.GraphInventoryResourceUri; +import org.onap.so.client.graphinventory.entities.uri.GraphInventorySingleResourceUri; +import org.onap.so.client.graphinventory.entities.uri.HttpAwareUri; +import org.onap.so.client.graphinventory.exceptions.GraphInventoryMultipleItemsException; -public abstract class GraphInventoryResourcesClient { +public abstract class GraphInventoryResourcesClient, SingleUri extends GraphInventorySingleResourceUri, PluralUri extends GraphInventoryPluralResourceUri, EdgeLabel extends GraphInventoryEdgeLabel, Wrapper extends GraphInventoryResultWrapper, TransactionalClient, SingleTransactionClient> { protected GraphInventoryClient client; @@ -49,7 +58,7 @@ public abstract class GraphInventoryResourcesClient forceMinimal = (Uri) uri.clone(); forceMinimal.format(Format.COUNT); forceMinimal.limit(1); try { @@ -91,8 +100,8 @@ public abstract class GraphInventoryResourcesClient uriAClone = (SingleUri) uriA.clone(); RestClient giRC = client.createClient(uriAClone.relationshipAPI()); giRC.put(this.buildRelationship(uriB)); } @@ -105,8 +114,8 @@ public abstract class GraphInventoryResourcesClient uriAClone = (SingleUri) uriA.clone(); RestClient giRC = client.createClient(uriAClone.relationshipAPI()); giRC.put(this.buildRelationship(uriB, label)); } @@ -118,8 +127,8 @@ public abstract class GraphInventoryResourcesClient uriAClone = (SingleUri) uriA.clone(); RestClient giRC = client.createClient(uriAClone.relationshipAPI()); giRC.delete(this.buildRelationship(uriB)); } @@ -130,8 +139,8 @@ public abstract class GraphInventoryResourcesClient clone = (SingleUri) uri.clone(); RestClient giRC = client.createClient(clone); Map result = giRC.get(new GenericType>() {}).orElseThrow( () -> new NotFoundException(clone.build() + " does not exist in " + client.getGraphDBName())); @@ -145,7 +154,7 @@ public abstract class GraphInventoryResourcesClient Optional getOne(Class pluralClass, Class resultClass, PluralUri uri) { + Optional> result = unwrapPlural(pluralClass, resultClass, uri); + + if (result.isPresent()) { + if (result.get().size() == 1) { + return Optional.of(result.get().get(0)); + } else { + throw new GraphInventoryMultipleItemsException(result.get().size(), uri); + } + } + + return Optional.empty(); + } + + public Optional getFirst(Class pluralClass, Class resultClass, PluralUri uri) { + Optional> result = unwrapPlural(pluralClass, resultClass, uri); + + if (result.isPresent() && !result.get().isEmpty()) { + return Optional.of(result.get().get(0)); + } + + return Optional.empty(); + } + + public Optional getFirstWrapper(Class pluralClass, Class resultClass, PluralUri uri) { + + Optional result = getFirst(pluralClass, resultClass, uri); + if (result.isPresent()) { + return Optional.of(this.createWrapper(result.get())); + } else { + return Optional.empty(); + } + } + + public Optional getOneWrapper(Class pluralClass, Class resultClass, PluralUri uri) { + + Optional result = getOne(pluralClass, resultClass, uri); + if (result.isPresent()) { + return Optional.of(this.createWrapper(result.get())); + } else { + return Optional.empty(); + } + } + + protected Optional> unwrapPlural(Class pluralClass, Class resultClass, PluralUri uri) { + try { + PluralUri clone = (PluralUri) uri.clone().limit(1); + Optional obj = client.createClient(clone).get(pluralClass); + if (obj.isPresent()) { + Optional listMethod = Arrays.stream(obj.get().getClass().getMethods()).filter(method -> { + + Type returnType = method.getGenericReturnType(); + if (returnType instanceof ParameterizedType) { + Type[] types = ((ParameterizedType) returnType).getActualTypeArguments(); + if (types != null && types[0] instanceof Class) { + Class listClass = (Class) types[0]; + return resultClass.equals(listClass); + } + } + + return false; + }).findFirst(); + if (listMethod.isPresent()) { + try { + return Optional.of((List) listMethod.get().invoke(obj.get())); + + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } + } + return Optional.empty(); + + } catch (NotFoundException e) { + if (this.getRestProperties().mapNotFoundToEmpty()) { + return Optional.empty(); + } else { + throw e; + } + } + } + /** * Retrieves an object from GraphInventory wrapped in a helper class which offer additional features * @@ -269,8 +360,8 @@ public abstract class GraphInventoryResourcesClient obj) { - if (!this.exists(uri)) { + public Self createIfNotExists(SingleUri uri, Optional obj) { + if (!this.exists((Uri) uri)) { if (obj.isPresent()) { this.create(uri, obj.get()); } else { @@ -281,17 +372,21 @@ public abstract class GraphInventoryResourcesClient label) { + protected Relationship buildRelationship(SingleUri uri, Optional label) { final Relationship result = new Relationship(); - result.setRelatedLink(uri.build().toString()); + if (uri instanceof HttpAwareUri) { + result.setRelatedLink(((HttpAwareUri) uri).locateAndBuild().toString()); + } else { + result.setRelatedLink(uri.build().toString()); + } if (label.isPresent()) { result.setRelationshipLabel(label.get().toString()); } @@ -300,6 +395,8 @@ public abstract class GraphInventoryResourcesClient