From 58d3e270c5c110360ae0459f0a2cff61ec04a6ff Mon Sep 17 00:00:00 2001 From: "Benjamin, Max (mb388a)" Date: Wed, 13 Feb 2019 10:06:03 -0500 Subject: add new functionality to aai client Remove only the first occurrence of parentUri from uriTemplate. add createIfNotExists to transaction clients moved classes to generic GraphInventory location removed unused interfaces from graphinventory client refactored all graph inventory clients Change-Id: I2b946674ff92c44d33fff2f327e96655ed5c0195 Issue-ID: SO-1501 Signed-off-by: Benjamin, Max (mb388a) --- .../java/org/onap/so/client/aai/AAIClient.java | 21 +- .../java/org/onap/so/client/aai/AAIDSLQuery.java | 83 ------ .../org/onap/so/client/aai/AAIDSLQueryClient.java | 42 +++ .../java/org/onap/so/client/aai/AAIObjectType.java | 7 +- .../org/onap/so/client/aai/AAIQueryClient.java | 56 +--- .../org/onap/so/client/aai/AAIResourcesClient.java | 283 ++------------------- .../so/client/aai/AAISingleTransactionClient.java | 184 +++----------- .../org/onap/so/client/aai/AAISubgraphType.java | 38 --- .../onap/so/client/aai/AAITransactionalClient.java | 169 +++--------- .../org/onap/so/client/aai/entities/DSLNode.java | 75 ------ .../onap/so/client/aai/entities/DSLNodeKey.java | 69 ----- .../org/onap/so/client/aai/entities/DSLQuery.java | 47 ---- .../so/client/aai/entities/DSLQueryBuilder.java | 111 -------- .../java/org/onap/so/client/aai/entities/__.java | 59 ----- .../so/client/aai/entities/uri/AAISimpleUri.java | 4 + .../so/client/aai/entities/uri/AAIUriFactory.java | 5 + .../org/onap/so/client/graphinventory/Format.java | 1 + .../graphinventory/GraphInventoryClient.java | 8 +- .../graphinventory/GraphInventoryQueryClient.java | 73 ++++++ .../GraphInventoryResourcesClient.java | 199 +++++++++++++-- .../GraphInventorySingleTransactionClient.java | 87 ------- .../graphinventory/GraphInventorySubgraphType.java | 38 +++ .../GraphInventoryTransactionClient.java | 240 +++++++++++++++++ .../GraphInventoryTransactionalClient.java | 94 ------- .../client/graphinventory/TransactionBuilder.java | 10 + .../so/client/graphinventory/entities/DSLNode.java | 76 ++++++ .../client/graphinventory/entities/DSLNodeKey.java | 71 ++++++ .../client/graphinventory/entities/DSLQuery.java | 47 ++++ .../graphinventory/entities/DSLQueryBuilder.java | 113 ++++++++ .../onap/so/client/graphinventory/entities/__.java | 59 +++++ .../graphinventory/entities/uri/SimpleUri.java | 7 + 31 files changed, 1100 insertions(+), 1276 deletions(-) delete mode 100644 common/src/main/java/org/onap/so/client/aai/AAIDSLQuery.java create mode 100644 common/src/main/java/org/onap/so/client/aai/AAIDSLQueryClient.java delete mode 100644 common/src/main/java/org/onap/so/client/aai/AAISubgraphType.java delete mode 100644 common/src/main/java/org/onap/so/client/aai/entities/DSLNode.java delete mode 100644 common/src/main/java/org/onap/so/client/aai/entities/DSLNodeKey.java delete mode 100644 common/src/main/java/org/onap/so/client/aai/entities/DSLQuery.java delete mode 100644 common/src/main/java/org/onap/so/client/aai/entities/DSLQueryBuilder.java delete mode 100644 common/src/main/java/org/onap/so/client/aai/entities/__.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryQueryClient.java delete mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySingleTransactionClient.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySubgraphType.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionClient.java delete mode 100644 common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionalClient.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/TransactionBuilder.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQuery.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java create mode 100644 common/src/main/java/org/onap/so/client/graphinventory/entities/__.java (limited to 'common/src/main/java/org/onap') diff --git a/common/src/main/java/org/onap/so/client/aai/AAIClient.java b/common/src/main/java/org/onap/so/client/aai/AAIClient.java index be553420ac..21bbc51f89 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAIClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAIClient.java @@ -27,20 +27,24 @@ import javax.ws.rs.core.UriBuilder; import org.onap.so.client.RestClient; import org.onap.so.client.graphinventory.GraphInventoryClient; +import org.onap.so.client.graphinventory.GraphInventoryVersion; import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; import org.onap.so.client.graphinventory.exceptions.GraphInventoryUriComputationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class AAIClient extends GraphInventoryClient { +public class AAIClient extends GraphInventoryClient { private static final String AAI_ROOT = "/aai"; protected static Logger logger = LoggerFactory.getLogger(AAIClient.class); protected AAIVersion version; - public AAIClient() { + protected AAIClient() { + super(AAIProperties.class); + } + + protected AAIClient(AAIVersion version) { super(AAIProperties.class); } - @Override protected URI constructPath(GraphInventoryUri uri) { @@ -48,7 +52,7 @@ public abstract class AAIClient extends GraphInventoryClient { } @Override - protected RestClient createClient(GraphInventoryUri uri) { + public RestClient createClient(GraphInventoryUri uri) { try { return new AAIRestClient(getRestProperties(), constructPath(uri)); } catch (GraphInventoryUriComputationException | NotFoundException e) { @@ -57,11 +61,18 @@ public abstract class AAIClient extends GraphInventoryClient { } } - protected AAIVersion getVersion() { + @Override + public AAIVersion getVersion() { if (version == null) { return this.getRestProperties().getDefaultVersion(); } else { return this.version; } } + + + @Override + public String getGraphDBName() { + return "A&AI"; + } } diff --git a/common/src/main/java/org/onap/so/client/aai/AAIDSLQuery.java b/common/src/main/java/org/onap/so/client/aai/AAIDSLQuery.java deleted file mode 100644 index 52bae20ff3..0000000000 --- a/common/src/main/java/org/onap/so/client/aai/AAIDSLQuery.java +++ /dev/null @@ -1,83 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.so.client.aai; - -import java.util.Optional; - -import org.onap.so.client.RestClient; -import org.onap.so.client.aai.entities.DSLQuery; -import org.onap.so.client.aai.entities.uri.AAIUriFactory; -import org.onap.so.client.graphinventory.Format; -import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; - -public class AAIDSLQuery extends AAIClient { - - private Optional depth = Optional.empty(); - private boolean nodesOnly = false; - private Optional subgraph = Optional.empty(); - - public AAIDSLQuery() { - super(); - } - - public AAIDSLQuery(AAIVersion version) { - super(); - this.version = version; - } - - public String query(Format format, DSLQuery query) { - return this.createClient(AAIUriFactory.createResourceUri(AAIObjectType.DSL).queryParam("format", format.toString())) - .put(query, String.class); - } - - public AAIDSLQuery depth (String depth) { - this.depth = Optional.of(depth); - return this; - } - public AAIDSLQuery nodesOnly() { - this.nodesOnly = true; - return this; - } - public AAIDSLQuery subgraph(AAISubgraphType type){ - - subgraph = Optional.of(type); - - return this; - } - - protected GraphInventoryUri setupQueryParams(GraphInventoryUri uri) { - GraphInventoryUri clone = uri.clone(); - if (this.depth.isPresent()) { - clone.queryParam("depth", depth.get()); - } - if (this.nodesOnly) { - clone.queryParam("nodesOnly", ""); - } - if (this.subgraph.isPresent()) { - clone.queryParam("subgraph", this.subgraph.get().toString()); - } - return clone; - } - @Override - protected RestClient createClient(GraphInventoryUri uri) { - return super.createClient(setupQueryParams(uri)); - } -} diff --git a/common/src/main/java/org/onap/so/client/aai/AAIDSLQueryClient.java b/common/src/main/java/org/onap/so/client/aai/AAIDSLQueryClient.java new file mode 100644 index 0000000000..e9b58b469d --- /dev/null +++ b/common/src/main/java/org/onap/so/client/aai/AAIDSLQueryClient.java @@ -0,0 +1,42 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.aai; + +import org.onap.so.client.aai.entities.uri.AAIUriFactory; +import org.onap.so.client.graphinventory.GraphInventoryQueryClient; +import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; + +public class AAIDSLQueryClient extends GraphInventoryQueryClient { + + public AAIDSLQueryClient() { + super(new AAIClient()); + } + + public AAIDSLQueryClient(AAIVersion version) { + super(new AAIClient(version)); + } + + @Override + protected GraphInventoryUri getQueryUri() { + return AAIUriFactory.createResourceUri(AAIObjectType.DSL); + } + +} diff --git a/common/src/main/java/org/onap/so/client/aai/AAIObjectType.java b/common/src/main/java/org/onap/so/client/aai/AAIObjectType.java index 14d7f43911..21e36cde6c 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAIObjectType.java +++ b/common/src/main/java/org/onap/so/client/aai/AAIObjectType.java @@ -26,8 +26,7 @@ import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Set; - -import javax.annotation.Priority; +import java.util.regex.Pattern; import org.onap.aai.annotations.Metadata; import org.onap.aai.domain.yang.AggregateRoute; @@ -61,6 +60,7 @@ import org.onap.aai.domain.yang.RouteTableReference; import org.onap.aai.domain.yang.ServiceInstance; import org.onap.aai.domain.yang.ServiceSubscription; import org.onap.aai.domain.yang.SpPartner; +import org.onap.aai.domain.yang.SriovPf; import org.onap.aai.domain.yang.Subnet; import org.onap.aai.domain.yang.Tenant; import org.onap.aai.domain.yang.TunnelXconnect; @@ -119,6 +119,7 @@ public class AAIObjectType implements GraphInventoryObjectType, Serializable { public static final AAIObjectType MODEL_VER = new AAIObjectType(AAINamespaceConstants.SERVICE_DESIGN_AND_CREATION + "/models/model/{model-invariant-id}", ModelVer.class); public static final AAIObjectType TUNNEL_XCONNECT = new AAIObjectType(AAIObjectType.ALLOTTED_RESOURCE.uriTemplate(), TunnelXconnect.class); public static final AAIObjectType P_INTERFACE = new AAIObjectType(AAIObjectType.PSERVER.uriTemplate(), PInterface.class); + public static final AAIObjectType SRIOV_PF = new AAIObjectType(AAIObjectType.P_INTERFACE.uriTemplate(), SriovPf.class); public static final AAIObjectType PHYSICAL_LINK = new AAIObjectType(AAINamespaceConstants.NETWORK, PhysicalLink.class); public static final AAIObjectType INSTANCE_GROUP = new AAIObjectType(AAINamespaceConstants.NETWORK, InstanceGroup.class); public static final AAIObjectType COLLECTION = new AAIObjectType(AAINamespaceConstants.NETWORK, Collection.class); @@ -218,6 +219,6 @@ public class AAIObjectType implements GraphInventoryObjectType, Serializable { } protected String removeParentUri(Class aaiObjectClass, String parentUri) { - return aaiObjectClass.getAnnotation(Metadata.class).uriTemplate().replace(parentUri, ""); + return aaiObjectClass.getAnnotation(Metadata.class).uriTemplate().replaceFirst(Pattern.quote(parentUri), ""); } } diff --git a/common/src/main/java/org/onap/so/client/aai/AAIQueryClient.java b/common/src/main/java/org/onap/so/client/aai/AAIQueryClient.java index 184b4e5251..c3523e94c2 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAIQueryClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAIQueryClient.java @@ -20,64 +20,28 @@ package org.onap.so.client.aai; -import java.util.Optional; - -import org.onap.so.client.RestClient; -import org.onap.so.client.aai.entities.CustomQuery; import org.onap.so.client.aai.entities.uri.AAIUriFactory; -import org.onap.so.client.graphinventory.Format; +import org.onap.so.client.graphinventory.GraphInventoryQueryClient; import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; -public class AAIQueryClient extends AAIClient { +public class AAIQueryClient extends GraphInventoryQueryClient { - private Optional depth = Optional.empty(); - private boolean nodesOnly = false; - private Optional subgraph = Optional.empty(); - public AAIQueryClient() { - super(); + super(new AAIClient()); } public AAIQueryClient(AAIVersion version) { - super(); - this.version = version; - } - - public String query(Format format, CustomQuery query) { - return this.createClient(AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY).queryParam("format", format.toString())) - .put(query, String.class); - } - - public AAIQueryClient depth (String depth) { - this.depth = Optional.of(depth); - return this; + super(new AAIClient(version)); } - public AAIQueryClient nodesOnly() { - this.nodesOnly = true; - return this; - } - public AAIQueryClient subgraph(AAISubgraphType type){ - - subgraph = Optional.of(type); - return this; + @Override + protected GraphInventoryUri getQueryUri() { + return AAIUriFactory.createResourceUri(AAIObjectType.CUSTOM_QUERY); } - protected GraphInventoryUri setupQueryParams(GraphInventoryUri uri) { - GraphInventoryUri clone = uri.clone(); - if (this.depth.isPresent()) { - clone.queryParam("depth", depth.get()); - } - if (this.nodesOnly) { - clone.queryParam("nodesOnly", ""); - } - if (this.subgraph.isPresent()) { - clone.queryParam("subgraph", this.subgraph.get().toString()); - } - return clone; - } @Override - protected RestClient createClient(GraphInventoryUri uri) { - return super.createClient(setupQueryParams(uri)); + protected GraphInventoryUri setupQueryParams(GraphInventoryUri uri) { + return super.setupQueryParams(uri); } + } diff --git a/common/src/main/java/org/onap/so/client/aai/AAIResourcesClient.java b/common/src/main/java/org/onap/so/client/aai/AAIResourcesClient.java index 288ac9bc7f..ee1736feeb 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAIResourcesClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAIResourcesClient.java @@ -20,291 +20,58 @@ package org.onap.so.client.aai; -import java.lang.reflect.InvocationTargetException; -import java.util.Map; import java.util.Optional; -import javax.ws.rs.NotFoundException; -import javax.ws.rs.core.GenericType; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.Status; - import org.onap.aai.domain.yang.Relationship; -import org.onap.so.client.RestClient; -import org.onap.so.client.RestProperties; import org.onap.so.client.aai.entities.AAIEdgeLabel; import org.onap.so.client.aai.entities.AAIResultWrapper; import org.onap.so.client.aai.entities.uri.AAIResourceUri; -import org.onap.so.client.aai.entities.uri.AAIUri; import org.onap.so.client.graphinventory.GraphInventoryResourcesClient; -import org.onap.so.client.graphinventory.entities.uri.Depth; +import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel; +import org.onap.so.client.graphinventory.entities.uri.GraphInventoryResourceUri; -public class AAIResourcesClient extends AAIClient implements GraphInventoryResourcesClient { - - public AAIResourcesClient() { - super(); - } - - public AAIResourcesClient(AAIVersion version) { - super(); - this.version = version; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#create(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) - */ - @Override - public void create(AAIResourceUri uri, Object obj) { - RestClient aaiRC = this.createClient(uri); - aaiRC.put(obj); - return; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#createEmpty(org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public void createEmpty(AAIResourceUri uri) { - RestClient aaiRC = this.createClient(uri); - aaiRC.put(""); - return; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#exists(org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public boolean exists(AAIResourceUri uri) { - AAIUri forceMinimal = this.addParams(Optional.of(Depth.ZERO), true, uri); - try { - RestClient aaiRC = this.createClient(forceMinimal); - - return aaiRC.get().getStatus() == Status.OK.getStatusCode(); - } catch (NotFoundException e) { - return false; - } - } +public class AAIResourcesClient extends GraphInventoryResourcesClient { - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public void connect(AAIResourceUri uriA, AAIResourceUri uriB) { - AAIResourceUri uriAClone = uriA.clone(); - RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); - aaiRC.put(this.buildRelationship(uriB)); - return; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.AAIEdgeLabel) - */ - @Override - public void connect(AAIResourceUri uriA, AAIResourceUri uriB, AAIEdgeLabel label) { - AAIResourceUri uriAClone = uriA.clone(); - RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); - aaiRC.put(this.buildRelationship(uriB, label)); - return; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public void disconnect(AAIResourceUri uriA, AAIResourceUri uriB) { - AAIResourceUri uriAClone = uriA.clone(); - RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); - aaiRC.delete(this.buildRelationship(uriB)); - return; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#delete(org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public void delete(AAIResourceUri uri) { - AAIResourceUri clone = uri.clone(); - RestClient aaiRC = this.createClient(clone); - Map result = aaiRC.get(new GenericType>(){}) - .orElseThrow(() -> new NotFoundException(clone.build() + " does not exist in A&AI")); - String resourceVersion = (String) result.get("resource-version"); - aaiRC = this.createClient(clone.resourceVersion(resourceVersion)); - aaiRC.delete(); - return; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#update(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) - */ - @Override - public void update(AAIResourceUri uri, Object obj) { - RestClient aaiRC = this.createClient(uri); - aaiRC.patch(obj); - return; - } + private AAIClient aaiClient; - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#get(java.lang.Class, org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public Optional get(Class clazz, AAIResourceUri uri) { - try { - return this.createClient(uri).get(clazz); - } catch (NotFoundException e) { - if (this.getRestProperties().mapNotFoundToEmpty()) { - return Optional.empty(); - } else { - throw e; - } - } - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#getFullResponse(org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public Response getFullResponse(AAIResourceUri uri) { - try { - return this.createClient(uri).get(); - } catch (NotFoundException e) { - if (this.getRestProperties().mapNotFoundToEmpty()) { - return e.getResponse(); - } else { - throw e; - } - } + public AAIResourcesClient() { + super(new AAIClient()); + aaiClient = (AAIClient) super.client; } - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#get(javax.ws.rs.core.GenericType, org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public Optional get(GenericType resultClass, AAIResourceUri uri) { - try { - return this.createClient(uri).get(resultClass); - } catch (NotFoundException e) { - if (this.getRestProperties().mapNotFoundToEmpty()) { - return Optional.empty(); - } else { - throw e; - } - } + public AAIResourcesClient(AAIVersion version) { + super(new AAIClient(version)); + aaiClient = (AAIClient) super.client; } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#get(org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ + @Override - public AAIResultWrapper get(AAIResourceUri uri) { - String json; - try { - json = this.createClient(uri).get(String.class).orElse(null); - } catch (NotFoundException e) { - if (this.getRestProperties().mapNotFoundToEmpty()) { - json = null; - } else { - throw e; - } - } + public AAIResultWrapper createWrapper(String json) { return new AAIResultWrapper(json); } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#get(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Class) - */ - @Override - public AAIResultWrapper get(AAIResourceUri uri, Class c) { - String json; - try { - json = this.createClient(uri).get(String.class) - .orElseThrow(() -> createException(c, uri.build() + " not found in A&AI", Optional.empty())); - } catch (NotFoundException e) { - throw createException(c, "could not construct uri for use with A&AI", Optional.of(e)); - } - return new AAIResultWrapper(json); - } - - private RuntimeException createException(Class c, String message, Optional t) { - RuntimeException e; - try { - if (t.isPresent()) { - e = c.getConstructor(String.class, Throwable.class).newInstance(message, t.get()); - } else { - e = c.getConstructor(String.class).newInstance(message); - } - } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException - | NoSuchMethodException | SecurityException e1) { - throw new IllegalArgumentException("could not create instance for " + c.getName()); - } - - return e; - } - - protected Relationship buildRelationship(AAIResourceUri uri) { - return buildRelationship(uri, Optional.empty()); - } - - protected Relationship buildRelationship(AAIResourceUri uri, AAIEdgeLabel label) { - return buildRelationship(uri, Optional.of(label)); - } - protected Relationship buildRelationship(AAIResourceUri uri, Optional label) { - final Relationship result = new Relationship(); - result.setRelatedLink(uri.build().toString()); - if (label.isPresent()) { - result.setRelationshipLabel(label.get().toString()); - } - return result; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#createIfNotExists(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.Optional) - */ @Override - public AAIResourcesClient createIfNotExists(AAIResourceUri uri, Optional obj) { - if(!this.exists(uri)){ - if (obj.isPresent()) { - this.create(uri, obj.get()); - } else { - this.createEmpty(uri); - } - - } - return this; + public AAITransactionalClient beginTransaction() { + return new AAITransactionalClient(this, aaiClient); } - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#beginTransaction() - */ @Override - public AAITransactionalClient beginTransaction() { - return new AAITransactionalClient(this.getVersion()); + public AAISingleTransactionClient beginSingleTransaction() { + return new AAISingleTransactionClient(this, aaiClient); } - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#beginSingleTransaction() - */ @Override - public AAISingleTransactionClient beginSingleTransaction() { - return new AAISingleTransactionClient(this.getVersion()); + protected Relationship buildRelationship(GraphInventoryResourceUri uri) { + return super.buildRelationship(uri, Optional.empty()); } - private AAIUri addParams(Optional depth, boolean nodesOnly, AAIUri uri) { - AAIUri clone = uri.clone(); - if (depth.isPresent()) { - clone.depth(depth.get()); - } - if (nodesOnly) { - clone.nodesOnly(nodesOnly); - } - - return clone; + @Override + protected Relationship buildRelationship(GraphInventoryResourceUri uri, GraphInventoryEdgeLabel label) { + return super.buildRelationship(uri, Optional.of(label)); } - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryResourcesClient#getRestProperties() - */ + @Override - public T getRestProperties() { - return super.getRestProperties(); + protected Relationship buildRelationship(GraphInventoryResourceUri uri, Optional label) { + return super.buildRelationship(uri, label); } + } diff --git a/common/src/main/java/org/onap/so/client/aai/AAISingleTransactionClient.java b/common/src/main/java/org/onap/so/client/aai/AAISingleTransactionClient.java index ba65ac3f15..ee15e10e01 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAISingleTransactionClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAISingleTransactionClient.java @@ -22,170 +22,45 @@ package org.onap.so.client.aai; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.Map.Entry; import java.util.Optional; -import javax.ws.rs.NotFoundException; import javax.ws.rs.core.GenericType; -import org.onap.aai.domain.yang.Relationship; import org.onap.so.client.RestClient; import org.onap.so.client.aai.entities.AAIEdgeLabel; import org.onap.so.client.aai.entities.AAIError; -import org.onap.so.client.aai.entities.bulkprocess.Transactions; import org.onap.so.client.aai.entities.singletransaction.OperationBodyRequest; import org.onap.so.client.aai.entities.singletransaction.OperationBodyResponse; import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest; import org.onap.so.client.aai.entities.singletransaction.SingleTransactionResponse; import org.onap.so.client.aai.entities.uri.AAIResourceUri; -import org.onap.so.client.aai.entities.uri.AAIUri; import org.onap.so.client.aai.entities.uri.AAIUriFactory; import org.onap.so.client.graphinventory.GraphInventoryPatchConverter; -import org.onap.so.client.graphinventory.GraphInventorySingleTransactionClient; +import org.onap.so.client.graphinventory.GraphInventoryTransactionClient; import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; -public class AAISingleTransactionClient extends AAIClient implements GraphInventorySingleTransactionClient { +public class AAISingleTransactionClient extends GraphInventoryTransactionClient { private final SingleTransactionRequest request; - private final AAIVersion version; - private int actionCount = 0; - - private final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter(); - - protected AAISingleTransactionClient(AAIVersion version) { + private AAIResourcesClient resourcesClient; + private AAIClient aaiClient; + protected AAISingleTransactionClient(AAIResourcesClient resourcesClient, AAIClient aaiClient) { super(); - this.version = version; + this.resourcesClient = resourcesClient; + this.aaiClient = aaiClient; this.request = new SingleTransactionRequest(); } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#create(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) - */ - @Override - public AAISingleTransactionClient create(AAIResourceUri uri, Object obj) { - request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uri.build().toString()).withBody(obj)); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#createEmpty(org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public AAISingleTransactionClient createEmpty(AAIResourceUri uri) { - request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uri.build().toString()).withBody(new HashMap())); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public AAISingleTransactionClient connect(AAIResourceUri uriA, AAIResourceUri uriB) { - AAIResourceUri uriAClone = uriA.clone(); - request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uriAClone.relationshipAPI().build().toString()).withBody(this.buildRelationship(uriB))); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List) - */ - @Override - public AAISingleTransactionClient connect(AAIResourceUri uriA, List uris) { - for (AAIResourceUri uri : uris) { - this.connect(uriA, uri); - } - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.AAIEdgeLabel) - */ - @Override - public AAISingleTransactionClient connect(AAIResourceUri uriA, AAIResourceUri uriB, AAIEdgeLabel label) { - AAIResourceUri uriAClone = uriA.clone(); - RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); - aaiRC.put(this.buildRelationship(uriB, label)); - return this; - } - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List, org.onap.so.client.aai.entities.AAIEdgeLabel) - */ - @Override - public AAISingleTransactionClient connect(AAIResourceUri uriA, List uris, AAIEdgeLabel label) { - for (AAIResourceUri uri : uris) { - this.connect(uriA, uri, label); - } - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public AAISingleTransactionClient disconnect(AAIResourceUri uriA, AAIResourceUri uriB) { - AAIResourceUri uriAClone = uriA.clone(); - request.getOperations().add(new OperationBodyRequest().withAction("delete").withUri(uriAClone.relationshipAPI().build().toString()).withBody(this.buildRelationship(uriB))); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List) - */ - @Override - public AAISingleTransactionClient disconnect(AAIResourceUri uriA, List uris) { - for (AAIResourceUri uri : uris) { - this.disconnect(uriA, uri); - } - return this; - } - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#delete(org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public AAISingleTransactionClient delete(AAIResourceUri uri) { - AAIResourcesClient client = new AAIResourcesClient(); - AAIResourceUri clone = uri.clone(); - Map result = client.get(new GenericType>(){}, clone) - .orElseThrow(() -> new NotFoundException(clone.build() + " does not exist in A&AI")); - String resourceVersion = (String) result.get("resource-version"); - request.getOperations().add(new OperationBodyRequest().withAction("delete").withUri(clone.resourceVersion(resourceVersion).build().toString()).withBody("")); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionClient#update(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) - */ - @Override - public AAISingleTransactionClient update(AAIResourceUri uri, Object obj) { - - final String payload = getPatchConverter().convertPatchFormat(obj); - request.getOperations().add(new OperationBodyRequest().withAction("patch").withUri(uri.build().toString()).withBody(payload)); - incrementActionAmount(); - return this; - } - - private void incrementActionAmount() { - actionCount++; - } /* (non-Javadoc) * @see org.onap.so.client.aai.GraphInventoryTransactionClient#execute() */ @Override public void execute() throws BulkProcessFailed { - RestClient client = this.createClient(AAIUriFactory.createResourceUri(AAIObjectType.SINGLE_TRANSACTION)); + RestClient client = aaiClient.createClient(AAIUriFactory.createResourceUri(AAIObjectType.SINGLE_TRANSACTION)); try { SingleTransactionResponse response = client.post(this.request, SingleTransactionResponse.class); if (response != null) { @@ -227,32 +102,43 @@ public class AAISingleTransactionClient extends AAIClient implements GraphInvent return Optional.empty(); } } + - private Relationship buildRelationship(AAIResourceUri uri) { - return buildRelationship(uri, Optional.empty()); + protected SingleTransactionRequest getRequest() { + return this.request; } - - private Relationship buildRelationship(AAIResourceUri uri, AAIEdgeLabel label) { - return buildRelationship(uri, Optional.of(label)); + + @Override + public void put(String uri, Object body) { + request.getOperations().add(new OperationBodyRequest().withAction("put").withUri(uri).withBody(body)); } - private Relationship buildRelationship(AAIResourceUri uri, Optional label) { - final Relationship result = new Relationship(); - result.setRelatedLink(uri.build().toString()); - if (label.isPresent()) { - result.setRelationshipLabel(label.toString()); - } - return result; + + @Override + public void delete(String uri, Object body) { + request.getOperations().add(new OperationBodyRequest().withAction("delete").withUri(uri).withBody(body)); + } + + @Override + public void patch(String uri, Object body) { + request.getOperations().add(new OperationBodyRequest().withAction("patch").withUri(uri).withBody(body)); } @Override - protected AAIVersion getVersion() { - return this.version; + protected Optional get(GenericType genericType, AAIResourceUri clone) { + return resourcesClient.get(genericType, clone); } - protected SingleTransactionRequest getRequest() { - return this.request; + @Override + protected boolean exists(AAIResourceUri uri) { + return resourcesClient.exists(uri); } + @Override + protected String getGraphDBName() { + return aaiClient.getGraphDBName(); + } + + @Override protected GraphInventoryPatchConverter getPatchConverter() { return this.patchConverter; } diff --git a/common/src/main/java/org/onap/so/client/aai/AAISubgraphType.java b/common/src/main/java/org/onap/so/client/aai/AAISubgraphType.java deleted file mode 100644 index e9beb143fd..0000000000 --- a/common/src/main/java/org/onap/so/client/aai/AAISubgraphType.java +++ /dev/null @@ -1,38 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.so.client.aai; - - -public enum AAISubgraphType { - STAR("star"), - PRUNE("prune"); - - private final String name; - - private AAISubgraphType(String name) { - this.name = name; - } - - @Override - public String toString() { - return name; - } -} diff --git a/common/src/main/java/org/onap/so/client/aai/AAITransactionalClient.java b/common/src/main/java/org/onap/so/client/aai/AAITransactionalClient.java index dd4cb2f591..474ae89ff9 100644 --- a/common/src/main/java/org/onap/so/client/aai/AAITransactionalClient.java +++ b/common/src/main/java/org/onap/so/client/aai/AAITransactionalClient.java @@ -22,13 +22,11 @@ package org.onap.so.client.aai; import java.io.IOException; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Optional; -import javax.ws.rs.NotFoundException; import javax.ws.rs.core.GenericType; import javax.ws.rs.core.Response; @@ -42,7 +40,7 @@ import org.onap.so.client.aai.entities.bulkprocess.Transactions; import org.onap.so.client.aai.entities.uri.AAIResourceUri; import org.onap.so.client.aai.entities.uri.AAIUriFactory; import org.onap.so.client.graphinventory.GraphInventoryPatchConverter; -import org.onap.so.client.graphinventory.GraphInventoryTransactionalClient; +import org.onap.so.client.graphinventory.GraphInventoryTransactionClient; import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed; import org.onap.so.jsonpath.JsonPathUtil; @@ -50,18 +48,17 @@ import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.common.base.Joiner; -public class AAITransactionalClient extends AAIClient implements GraphInventoryTransactionalClient { +public class AAITransactionalClient extends GraphInventoryTransactionClient { private final Transactions transactions; private Transaction currentTransaction; - private final AAIVersion version; - private int actionCount = 0; - private final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter(); - - protected AAITransactionalClient(AAIVersion version) { + private AAIResourcesClient resourcesClient; + private AAIClient aaiClient; + protected AAITransactionalClient(AAIResourcesClient resourcesClient, AAIClient aaiClient) { super(); - this.version = version; + this.resourcesClient = resourcesClient; + this.aaiClient = aaiClient; this.transactions = new Transactions(); startTransaction(); } @@ -75,132 +72,17 @@ public class AAITransactionalClient extends AAIClient implements GraphInventoryT /* (non-Javadoc) * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#beginNewTransaction() */ - @Override public AAITransactionalClient beginNewTransaction() { startTransaction(); return this; } - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#create(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) - */ - @Override - public AAITransactionalClient create(AAIResourceUri uri, Object obj) { - currentTransaction.getPut().add(new OperationBody().withUri(uri.build().toString()).withBody(obj)); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#createEmpty(org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public AAITransactionalClient createEmpty(AAIResourceUri uri) { - currentTransaction.getPut().add(new OperationBody().withUri(uri.build().toString()).withBody(new HashMap())); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public AAITransactionalClient connect(AAIResourceUri uriA, AAIResourceUri uriB) { - AAIResourceUri uriAClone = uriA.clone(); - currentTransaction.getPut().add(new OperationBody().withUri(uriAClone.relationshipAPI().build().toString()).withBody(this.buildRelationship(uriB))); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List) - */ - @Override - public AAITransactionalClient connect(AAIResourceUri uriA, List uris) { - for (AAIResourceUri uri : uris) { - this.connect(uriA, uri); - } - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.AAIEdgeLabel) - */ - @Override - public AAITransactionalClient connect(AAIResourceUri uriA, AAIResourceUri uriB, AAIEdgeLabel label) { - AAIResourceUri uriAClone = uriA.clone(); - RestClient aaiRC = this.createClient(uriAClone.relationshipAPI()); - aaiRC.put(this.buildRelationship(uriB, label)); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#connect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List, org.onap.so.client.aai.entities.AAIEdgeLabel) - */ - @Override - public AAITransactionalClient connect(AAIResourceUri uriA, List uris, AAIEdgeLabel label) { - for (AAIResourceUri uri : uris) { - this.connect(uriA, uri, label); - } - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public AAITransactionalClient disconnect(AAIResourceUri uriA, AAIResourceUri uriB) { - AAIResourceUri uriAClone = uriA.clone(); - currentTransaction.getDelete().add(new OperationBody().withUri(uriAClone.relationshipAPI().build().toString()).withBody(this.buildRelationship(uriB))); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#disconnect(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.util.List) - */ - @Override - public AAITransactionalClient disconnect(AAIResourceUri uriA, List uris) { - for (AAIResourceUri uri : uris) { - this.disconnect(uriA, uri); - } - return this; - } - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#delete(org.onap.so.client.aai.entities.uri.AAIResourceUri) - */ - @Override - public AAITransactionalClient delete(AAIResourceUri uri) { - AAIResourcesClient client = new AAIResourcesClient(); - AAIResourceUri clone = uri.clone(); - Map result = client.get(new GenericType>(){}, clone) - .orElseThrow(() -> new NotFoundException(clone.build() + " does not exist in A&AI")); - String resourceVersion = (String) result.get("resource-version"); - currentTransaction.getDelete().add(new OperationBody().withUri(clone.resourceVersion(resourceVersion).build().toString()).withBody("")); - incrementActionAmount(); - return this; - } - - /* (non-Javadoc) - * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#update(org.onap.so.client.aai.entities.uri.AAIResourceUri, java.lang.Object) - */ - @Override - public AAITransactionalClient update(AAIResourceUri uri, Object obj) { - final String payload = getPatchConverter().convertPatchFormat(obj); - currentTransaction.getPatch().add(new OperationBody().withUri(uri.build().toString()).withBody(payload)); - incrementActionAmount(); - return this; - } - - private void incrementActionAmount() { - actionCount++; - } /* (non-Javadoc) * @see org.onap.so.client.aai.GraphInventoryTransactionalClient#execute() */ @Override public void execute() throws BulkProcessFailed { - RestClient client = this.createClient(AAIUriFactory.createResourceUri(AAIObjectType.BULK_PROCESS)); + RestClient client = aaiClient.createClient(AAIUriFactory.createResourceUri(AAIObjectType.BULK_PROCESS)); try { Response response = client.put(this.transactions); if (response.hasEntity()) { @@ -271,16 +153,43 @@ public class AAITransactionalClient extends AAIClient implements GraphInventoryT } return result; } + + protected Transactions getTransactions() { + return this.transactions; + } + + @Override + public void put(String uri, Object body) { + currentTransaction.getPut().add(new OperationBody().withUri(uri).withBody(body)); + } + + @Override + public void delete(String uri, Object body) { + currentTransaction.getDelete().add(new OperationBody().withUri(uri).withBody(body)); + + } @Override - protected AAIVersion getVersion() { - return this.version; + public void patch(String uri, Object body) { + currentTransaction.getPatch().add(new OperationBody().withUri(uri).withBody(body)); + } + + @Override + protected Optional get(GenericType genericType, AAIResourceUri clone) { + return resourcesClient.get(genericType, clone); } - protected Transactions getTransactions() { - return this.transactions; + @Override + protected boolean exists(AAIResourceUri uri) { + return resourcesClient.exists(uri); + } + + @Override + protected String getGraphDBName() { + return aaiClient.getGraphDBName(); } + @Override protected GraphInventoryPatchConverter getPatchConverter() { return this.patchConverter; } diff --git a/common/src/main/java/org/onap/so/client/aai/entities/DSLNode.java b/common/src/main/java/org/onap/so/client/aai/entities/DSLNode.java deleted file mode 100644 index f94c28c5b7..0000000000 --- a/common/src/main/java/org/onap/so/client/aai/entities/DSLNode.java +++ /dev/null @@ -1,75 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.so.client.aai.entities; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.onap.so.client.graphinventory.GraphInventoryObjectName; - -public class DSLNode implements QueryStep { - - private final String nodeName; - private final List nodeKeys; - private final StringBuilder query = new StringBuilder(); - private boolean output = false; - - public DSLNode() { - this.nodeName = ""; - this.nodeKeys = new ArrayList<>(); - - } - public DSLNode(GraphInventoryObjectName name) { - this.nodeName = name.typeName(); - this.nodeKeys = new ArrayList<>(); - query.append(nodeName); - } - public DSLNode(GraphInventoryObjectName name, DSLNodeKey... key) { - this.nodeName = name.typeName(); - this.nodeKeys = Arrays.asList(key); - query.append(nodeName); - } - - public DSLNode output() { - this.output = true; - - return this; - } - - public DSLNode and(DSLNodeKey... key) { - this.nodeKeys.addAll(Arrays.asList(key)); - - return this; - } - - @Override - public String build() { - if (output) { - query.append("*"); - } - for (DSLNodeKey key : nodeKeys) { - query.append(key.build()); - } - - return query.toString(); - } -} diff --git a/common/src/main/java/org/onap/so/client/aai/entities/DSLNodeKey.java b/common/src/main/java/org/onap/so/client/aai/entities/DSLNodeKey.java deleted file mode 100644 index a9795d1cc3..0000000000 --- a/common/src/main/java/org/onap/so/client/aai/entities/DSLNodeKey.java +++ /dev/null @@ -1,69 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.so.client.aai.entities; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import com.google.common.base.Joiner; - - -public class DSLNodeKey implements QueryStep { - - private boolean not = false; - private final StringBuilder query = new StringBuilder(); - private final String keyName; - private final List values; - public DSLNodeKey(String keyName, String... value) { - - this.keyName = keyName; - this.values = Arrays.asList(value); - } - - public DSLNodeKey not() { - - this.not = true; - return this; - } - - @Override - public String build() { - - if (not) { - query.append(" !"); - } - query.append("('").append(keyName).append("', "); - List temp = new ArrayList<>(); - for (String item : values) { - if (item.equals("null")) { - temp.add(String.format("' %s '", item)); - } else if (item.equals("")){ - temp.add("' '"); - } else { - temp.add(String.format("'%s'", item)); - } - } - query.append(Joiner.on(", ").join(temp)).append(")"); - - return query.toString(); - } -} diff --git a/common/src/main/java/org/onap/so/client/aai/entities/DSLQuery.java b/common/src/main/java/org/onap/so/client/aai/entities/DSLQuery.java deleted file mode 100644 index 0f4095177a..0000000000 --- a/common/src/main/java/org/onap/so/client/aai/entities/DSLQuery.java +++ /dev/null @@ -1,47 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.so.client.aai.entities; - -import com.fasterxml.jackson.annotation.JsonInclude; - -@JsonInclude(JsonInclude.Include.NON_NULL) -public class DSLQuery { - - private String dsl; - - public DSLQuery() { - - } - - public DSLQuery(String dsl) { - this.dsl = dsl; - } - - public String getDsl() { - return dsl; - } - - public void setDsl(String dsl) { - this.dsl = dsl; - } - - -} diff --git a/common/src/main/java/org/onap/so/client/aai/entities/DSLQueryBuilder.java b/common/src/main/java/org/onap/so/client/aai/entities/DSLQueryBuilder.java deleted file mode 100644 index 9f1dbeda8f..0000000000 --- a/common/src/main/java/org/onap/so/client/aai/entities/DSLQueryBuilder.java +++ /dev/null @@ -1,111 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.so.client.aai.entities; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.stream.Collectors; - -import com.google.common.base.Joiner; - - -public class DSLQueryBuilder implements QueryStep { - - private List steps = new ArrayList<>(); - - - public DSLQueryBuilder() { - - } - public DSLQueryBuilder(DSLNode node) { - steps.add(node); - } - - public DSLQueryBuilder node(DSLNode node) { - steps.add(node); - - return (DSLQueryBuilder) this; - } - public DSLQueryBuilder output() { - if (steps.get(steps.size() -1) instanceof DSLNode) { - ((DSLNode)steps.get(steps.size() -1)).output(); - } - return this; - } - - public DSLQueryBuilder union(final DSLQueryBuilder... union) { - - List> unions = Arrays.asList(union); - steps.add(() -> { - StringBuilder query = new StringBuilder(); - - query.append("> [ ").append( - Joiner.on(", ").join( - unions.stream().map(item -> item.build()).collect(Collectors.toList()))) - .append(" ]"); - return query.toString(); - }); - - return (DSLQueryBuilder) this; - } - - public DSLQueryBuilder where(DSLQueryBuilder where) { - - steps.add(() -> { - StringBuilder query = new StringBuilder(); - query.append(where.build()).append(")"); - String result = query.toString(); - if (!result.startsWith(">")) { - result = "> " + result; - } - return "(" + result; - }); - return this; - } - - public DSLQueryBuilder to(DSLQueryBuilder to) { - steps.add(() -> { - StringBuilder query = new StringBuilder(); - - query.append("> ").append(to.build()); - return query.toString(); - }); - return this; - } - - public String limit(int limit) { - return compile() + " LIMIT " + limit; - } - - @Override - public String build() { - return compile(); - } - - private String compile() { - return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())); - } - - protected QueryStep getFirst() { - return steps.get(0); - } -} diff --git a/common/src/main/java/org/onap/so/client/aai/entities/__.java b/common/src/main/java/org/onap/so/client/aai/entities/__.java deleted file mode 100644 index 16d6f9b27e..0000000000 --- a/common/src/main/java/org/onap/so/client/aai/entities/__.java +++ /dev/null @@ -1,59 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * ONAP - SO - * ================================================================================ - * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. - * ================================================================================ - * 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 - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.so.client.aai.entities; - -import org.onap.so.client.graphinventory.GraphInventoryObjectName; - -public class __ { - - protected __() { - - } - - public static DSLQueryBuilder identity() { - return new DSLQueryBuilder<>(); - } - public static DSLQueryBuilder start(DSLNode node) { - return new DSLQueryBuilder<>(node); - } - public static DSLQueryBuilder node(GraphInventoryObjectName name) { - - return __.start(new DSLNode(name)); - } - - public static DSLQueryBuilder node(GraphInventoryObjectName name, DSLNodeKey... key) { - return __.start(new DSLNode(name, key)); - } - - public static DSLNodeKey key(String keyName, String... value) { - return new DSLNodeKey(keyName, value); - } - - public static DSLQueryBuilder union(final DSLQueryBuilder... traversal) { - - return __.identity().union(traversal); - } - -public static DSLQueryBuilder where(DSLQueryBuilder traversal) { - - return __.identity().where(traversal); - } -} diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/AAISimpleUri.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/AAISimpleUri.java index 76413c2594..7cb37d9c3a 100644 --- a/common/src/main/java/org/onap/so/client/aai/entities/uri/AAISimpleUri.java +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/AAISimpleUri.java @@ -59,6 +59,10 @@ public class AAISimpleUri extends SimpleUri implements AAIResourceUri { super(parentUri, childType, childValues); } + protected AAISimpleUri(AAIResourceUri parentUri, AAIObjectPlurals childType) { + super(parentUri, childType); + } + @Override public AAISimpleUri relationshipAPI() { return (AAISimpleUri) super.relationshipAPI(); diff --git a/common/src/main/java/org/onap/so/client/aai/entities/uri/AAIUriFactory.java b/common/src/main/java/org/onap/so/client/aai/entities/uri/AAIUriFactory.java index 77c61089a4..ac0aba3c36 100644 --- a/common/src/main/java/org/onap/so/client/aai/entities/uri/AAIUriFactory.java +++ b/common/src/main/java/org/onap/so/client/aai/entities/uri/AAIUriFactory.java @@ -85,6 +85,11 @@ public class AAIUriFactory { return new AAISimpleUri(parentUri, childType, childValues); } + public static AAIResourceUri createResourceFromParentURI(AAIResourceUri parentUri, AAIObjectPlurals childType) { + + return new AAISimpleUri(parentUri, childType); + } + /** * Creates a uri for a plural type e.g. /cloud-infrastructure/pservers * diff --git a/common/src/main/java/org/onap/so/client/graphinventory/Format.java b/common/src/main/java/org/onap/so/client/graphinventory/Format.java index 5cbf1320c1..0a3e0b498c 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/Format.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/Format.java @@ -23,6 +23,7 @@ package org.onap.so.client.graphinventory; public enum Format { RESOURCE("resource"), + RESOURCE_AND_URL("resource_and_url"), SIMPLE("simple"), RAW("raw"), CONSOLE("console"), diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryClient.java index 0d10c21886..30e91dce03 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryClient.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryClient.java @@ -37,12 +37,16 @@ public abstract class GraphInventoryClient { } protected abstract URI constructPath(GraphInventoryUri uri); - protected abstract RestClient createClient(GraphInventoryUri uri); + public abstract RestClient createClient(GraphInventoryUri uri); - protected T getRestProperties() { + public T getRestProperties() { if (props == null) { throw new IllegalStateException("No RestProperty implementation found on classpath"); } return (T)props; } + + public abstract GraphInventoryVersion getVersion(); + + public abstract String getGraphDBName(); } diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryQueryClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryQueryClient.java new file mode 100644 index 0000000000..aa4842fe2a --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryQueryClient.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.graphinventory; + +import java.util.Optional; + +import org.onap.so.client.aai.entities.CustomQuery; +import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; + +public abstract class GraphInventoryQueryClient { + + private Optional depth = Optional.empty(); + private boolean nodesOnly = false; + private Optional subgraph = Optional.empty(); + private GraphInventoryClient client; + + public GraphInventoryQueryClient(GraphInventoryClient client) { + this.client = client; + } + + protected abstract GraphInventoryUri getQueryUri(); + + public String query(Format format, CustomQuery query) { + return client.createClient(setupQueryParams(getQueryUri().queryParam("format", format.toString()))).put(query, String.class); + } + + public S depth (String depth) { + this.depth = Optional.of(depth); + return (S) this; + } + public S nodesOnly() { + this.nodesOnly = true; + return (S) this; + } + public S subgraph(GraphInventorySubgraphType type){ + + subgraph = Optional.of(type); + + return (S) this; + } + + protected GraphInventoryUri setupQueryParams(GraphInventoryUri uri) { + GraphInventoryUri clone = uri.clone(); + if (this.depth.isPresent()) { + clone.queryParam("depth", depth.get()); + } + if (this.nodesOnly) { + clone.queryParam("nodesOnly", ""); + } + if (this.subgraph.isPresent()) { + clone.queryParam("subgraph", this.subgraph.get().toString()); + } + return clone; + } +} 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 7fbe286b98..39d2d01da9 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 @@ -1,17 +1,30 @@ package org.onap.so.client.graphinventory; +import java.lang.reflect.InvocationTargetException; +import java.util.Map; import java.util.Optional; +import javax.ws.rs.NotFoundException; import javax.ws.rs.core.GenericType; import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.Status; +import org.onap.aai.domain.yang.Relationship; +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.Depth; +import org.onap.so.client.graphinventory.entities.uri.GraphInventoryResourceUri; import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; -public interface GraphInventoryResourcesClient { +public abstract class GraphInventoryResourcesClient { + protected GraphInventoryClient client; + + protected GraphInventoryResourcesClient(GraphInventoryClient client) { + this.client = client; + } /** * creates a new object in GraphInventory * @@ -19,7 +32,10 @@ public interface GraphInventoryResourcesClient result = giRC.get(new GenericType>(){}) + .orElseThrow(() -> new NotFoundException(clone.build() + " does not exist in " + client.getGraphDBName())); + String resourceVersion = (String) result.get("resource-version"); + giRC = client.createClient(clone.resourceVersion(resourceVersion)); + giRC.delete(); + } /** * @param obj - can be any object which will marshal into a valid GraphInventory payload * @param uri * @return */ - void update(Uri uri, Object obj); + public void update(Uri uri, Object obj) { + RestClient giRC = client.createClient(uri); + giRC.patch(obj); + } /** * Retrieves an object from GraphInventory and unmarshalls it into the Class specified @@ -85,14 +136,34 @@ public interface GraphInventoryResourcesClient Optional get(Class clazz, Uri uri); + public Optional get(Class clazz, Uri uri) { + try { + return client.createClient(uri).get(clazz); + } catch (NotFoundException e) { + if (this.getRestProperties().mapNotFoundToEmpty()) { + return Optional.empty(); + } else { + throw e; + } + } + } /** * Retrieves an object from GraphInventory and returns complete response * @param uri * @return */ - Response getFullResponse(Uri uri); + public Response getFullResponse(Uri uri) { + try { + return client.createClient(uri).get(); + } catch (NotFoundException e) { + if (this.getRestProperties().mapNotFoundToEmpty()) { + return e.getResponse(); + } else { + throw e; + } + } + } /** * Retrieves an object from GraphInventory and automatically unmarshalls it into a Map or List @@ -100,15 +171,36 @@ public interface GraphInventoryResourcesClient Optional get(GenericType resultClass, Uri uri); - + public Optional get(GenericType resultClass, Uri uri) { + try { + return client.createClient(uri).get(resultClass); + } 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 * * @param uri * @return */ - Wrapper get(Uri uri); + public Wrapper get(Uri uri) { + String json; + try { + json = client.createClient(uri).get(String.class).orElse(null); + } catch (NotFoundException e) { + if (this.getRestProperties().mapNotFoundToEmpty()) { + json = null; + } else { + throw e; + } + } + return this.createWrapper(json); + } /** * Retrieves an object from GraphInventory wrapped in a helper class which offer additional features @@ -117,7 +209,33 @@ public interface GraphInventoryResourcesClient c); + public Wrapper get(Uri uri, Class c) { + String json; + try { + json = client.createClient(uri).get(String.class) + .orElseThrow(() -> createException(c, uri.build() + " not found in " + client.getGraphDBName(), Optional.empty())); + } catch (NotFoundException e) { + throw createException(c, "could not construct uri for use with " + client.getGraphDBName(), Optional.of(e)); + } + + return this.createWrapper(json); + } + + private RuntimeException createException(Class c, String message, Optional t) { + RuntimeException e; + try { + if (t.isPresent()) { + e = c.getConstructor(String.class, Throwable.class).newInstance(message, t.get()); + } else { + e = c.getConstructor(String.class).newInstance(message); + } + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException + | NoSuchMethodException | SecurityException e1) { + throw new IllegalArgumentException("could not create instance for " + c.getName()); + } + + return e; + } /** * Will automatically create the object if it does not exist @@ -126,22 +244,63 @@ public interface GraphInventoryResourcesClient obj); - + public Self createIfNotExists(Uri uri, Optional obj) { + if(!this.exists(uri)){ + if (obj.isPresent()) { + this.create(uri, obj.get()); + } else { + this.createEmpty(uri); + } + + } + return (Self)this; + } + protected Relationship buildRelationship(GraphInventoryResourceUri uri) { + return buildRelationship(uri, Optional.empty()); + } + + protected Relationship buildRelationship(GraphInventoryResourceUri uri, GraphInventoryEdgeLabel label) { + return buildRelationship(uri, Optional.of(label)); + } + protected Relationship buildRelationship(GraphInventoryResourceUri uri, Optional label) { + final Relationship result = new Relationship(); + result.setRelatedLink(uri.build().toString()); + if (label.isPresent()) { + result.setRelationshipLabel(label.get().toString()); + } + return result; + } + + public abstract Wrapper createWrapper(String json); + /** * Starts a transaction which encloses multiple GraphInventory mutations * * @return */ - TransactionalClient beginTransaction(); + public abstract TransactionalClient beginTransaction(); /** * Starts a transaction groups multiple GraphInventory mutations * * @return */ - SingleTransactionClient beginSingleTransaction(); + public abstract SingleTransactionClient beginSingleTransaction(); - T getRestProperties(); + private GraphInventoryUri addParams(Optional depth, boolean nodesOnly, GraphInventoryUri uri) { + GraphInventoryUri clone = uri.clone(); + if (depth.isPresent()) { + clone.depth(depth.get()); + } + if (nodesOnly) { + clone.nodesOnly(nodesOnly); + } + + return clone; + } + + public T getRestProperties() { + return client.getRestProperties(); + } } \ No newline at end of file diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySingleTransactionClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySingleTransactionClient.java deleted file mode 100644 index e1aa2252d9..0000000000 --- a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySingleTransactionClient.java +++ /dev/null @@ -1,87 +0,0 @@ -package org.onap.so.client.graphinventory; - -import java.util.List; - -import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel; -import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; -import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed; - -public interface GraphInventorySingleTransactionClient { - - /** - * creates a new object in A&AI - * - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return - */ - Self create(Uri uri, Object obj); - - /** - * creates a new object in A&AI with no payload body - * - * @param uri - * @return - */ - Self createEmpty(Uri uri); - - /** - * Adds a relationship between two objects in A&AI - * @param uriA - * @param uriB - * @return - */ - Self connect(Uri uriA, Uri uriB); - - /** - * relationship between multiple objects in A&AI - connects A to all objects specified in list - * - * @param uriA - * @param uris - * @return - */ - Self connect(Uri uriA, List uris); - - Self connect(Uri uriA, Uri uriB, EdgeLabel label); - - Self connect(Uri uriA, List uris, EdgeLabel label); - - /** - * Removes relationship from two objects in A&AI - * - * @param uriA - * @param uriB - * @return - */ - Self disconnect(Uri uriA, Uri uriB); - - /** - * Removes relationship from multiple objects - disconnects A from all objects specified in list - * @param uriA - * @param uris - * @return - */ - Self disconnect(Uri uriA, List uris); - - /** - * Deletes object from A&AI. Automatically handles resource-version. - * - * @param uri - * @return - */ - Self delete(Uri uri); - - /** - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return - */ - Self update(Uri uri, Object obj); - - /** - * Executes all created transactions in A&AI - * @throws BulkProcessFailed - */ - void execute() throws BulkProcessFailed; - -} \ No newline at end of file diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySubgraphType.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySubgraphType.java new file mode 100644 index 0000000000..4bbbe202cc --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventorySubgraphType.java @@ -0,0 +1,38 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.graphinventory; + + +public enum GraphInventorySubgraphType { + STAR("star"), + PRUNE("prune"); + + private final String name; + + private GraphInventorySubgraphType(String name) { + this.name = name; + } + + @Override + public String toString() { + return name; + } +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionClient.java new file mode 100644 index 0000000000..8d1a945c1b --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionClient.java @@ -0,0 +1,240 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.graphinventory; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import javax.ws.rs.NotFoundException; +import javax.ws.rs.core.GenericType; + +import org.onap.aai.domain.yang.Relationship; +import org.onap.so.client.aai.AAIVersion; +import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest; +import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel; +import org.onap.so.client.graphinventory.entities.uri.GraphInventoryResourceUri; +import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public abstract class GraphInventoryTransactionClient implements TransactionBuilder { + + protected static Logger logger = LoggerFactory.getLogger(GraphInventoryTransactionClient.class); + + protected int actionCount = 0; + + protected final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter(); + + protected GraphInventoryTransactionClient() { + } + + /** + * creates a new object in A&AI + * + * @param obj - can be any object which will marshal into a valid A&AI payload + * @param uri + * @return + */ + public Self create(Uri uri, Object obj) { + this.put(uri.build().toString(), obj); + incrementActionAmount(); + return (Self)this; + } + + /** + * creates a new object in A&AI with no payload body + * + * @param uri + * @return + */ + public Self createEmpty(Uri uri) { + this.put(uri.build().toString(), new HashMap()); + incrementActionAmount(); + return (Self)this; + } + + /** + * Will automatically create the object if it does not exist + * + * @param obj - Optional object which serializes to a valid GraphInventory payload + * @param uri + * @return + */ + public Self createIfNotExists(Uri uri, Optional obj) { + if(!this.exists(uri)){ + if (obj.isPresent()) { + this.create(uri, obj.get()); + incrementActionAmount(); + } else { + this.createEmpty(uri); + incrementActionAmount(); + } + + } + return (Self)this; + } + + /** + * Adds a relationship between two objects in A&AI + * @param uriA + * @param uriB + * @return + */ + public Self connect(Uri uriA, Uri uriB) { + GraphInventoryResourceUri uriAClone = uriA.clone(); + this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB)); + incrementActionAmount(); + return (Self)this; + } + + /** + * relationship between multiple objects in A&AI - connects A to all objects specified in list + * + * @param uriA + * @param uris + * @return + */ + public Self connect(Uri uriA, List uris) { + for (Uri uri : uris) { + this.connect(uriA, uri); + } + return (Self)this; + } + + /** + * relationship between multiple objects in A&AI - connects A to all objects specified in list + * + * @param uriA + * @param uris + * @return + */ + public Self connect(Uri uriA, Uri uriB, EdgeLabel label) { + GraphInventoryResourceUri uriAClone = uriA.clone(); + this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB, label)); + return (Self)this; + } + + /** + * relationship between multiple objects in A&AI - connects A to all objects specified in list + * + * @param uriA + * @param uris + * @return + */ + public Self connect(Uri uriA, List uris, EdgeLabel label) { + for (Uri uri : uris) { + this.connect(uriA, uri, label); + } + return (Self)this; + } + + /** + * Removes relationship from two objects in A&AI + * + * @param uriA + * @param uriB + * @return + */ + public Self disconnect(Uri uriA, Uri uriB) { + GraphInventoryResourceUri uriAClone = uriA.clone(); + this.delete(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB)); + incrementActionAmount(); + return (Self)this; + } + + /** + * Removes relationship from multiple objects - disconnects A from all objects specified in list + * @param uriA + * @param uris + * @return + */ + public Self disconnect(Uri uriA, List uris) { + for (Uri uri : uris) { + this.disconnect(uriA, uri); + } + return (Self)this; + } + /** + * Deletes object from A&AI. Automatically handles resource-version. + * + * @param uri + * @return + */ + public Self delete(Uri uri) { + Map result = this.get(new GenericType>(){}, (Uri)uri.clone()) + .orElseThrow(() -> new NotFoundException(uri.build() + " does not exist in " + this.getGraphDBName())); + String resourceVersion = (String) result.get("resource-version"); + this.delete(uri.resourceVersion(resourceVersion).build().toString(), ""); + incrementActionAmount(); + return (Self)this; + } + + protected abstract Optional get(GenericType genericType, Uri clone); + + protected abstract boolean exists(Uri uri); + + protected abstract String getGraphDBName(); + + /** + * @param obj - can be any object which will marshal into a valid A&AI payload + * @param uri + * @return + */ + public Self update(Uri uri, Object obj) { + + final String payload = getPatchConverter().convertPatchFormat(obj); + this.patch(uri.build().toString(), payload); + incrementActionAmount(); + return (Self)this; + } + + private void incrementActionAmount() { + actionCount++; + } + /** + * Executes all created transactions in A&AI + * @throws BulkProcessFailed + */ + public abstract void execute() throws BulkProcessFailed; + + private Relationship buildRelationship(Uri uri) { + return buildRelationship(uri, Optional.empty()); + } + + private Relationship buildRelationship(Uri uri, EdgeLabel label) { + return buildRelationship(uri, Optional.of(label)); + } + private Relationship buildRelationship(Uri uri, Optional label) { + final Relationship result = new Relationship(); + result.setRelatedLink(uri.build().toString()); + if (label.isPresent()) { + result.setRelationshipLabel(label.toString()); + } + return result; + } + + protected GraphInventoryPatchConverter getPatchConverter() { + return this.patchConverter; + } + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionalClient.java b/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionalClient.java deleted file mode 100644 index a7362c85da..0000000000 --- a/common/src/main/java/org/onap/so/client/graphinventory/GraphInventoryTransactionalClient.java +++ /dev/null @@ -1,94 +0,0 @@ -package org.onap.so.client.graphinventory; - -import java.util.List; - -import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel; -import org.onap.so.client.graphinventory.entities.uri.GraphInventoryUri; -import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed; - -public interface GraphInventoryTransactionalClient { - - /** - * adds an additional transaction and closes the previous transaction - * - * @return Self - */ - Self beginNewTransaction(); - - /** - * creates a new object in A&AI - * - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return - */ - Self create(Uri uri, Object obj); - - /** - * creates a new object in A&AI with no payload body - * - * @param uri - * @return - */ - Self createEmpty(Uri uri); - - /** - * Adds a relationship between two objects in A&AI - * @param uriA - * @param uriB - * @return - */ - Self connect(Uri uriA, Uri uriB); - - /** - * relationship between multiple objects in A&AI - connects A to all objects specified in list - * - * @param uriA - * @param uris - * @return - */ - Self connect(Uri uriA, List uris); - - Self connect(Uri uriA, Uri uriB, EdgeLabel label); - - Self connect(Uri uriA, List uris, EdgeLabel label); - - /** - * Removes relationship from two objects in A&AI - * - * @param uriA - * @param uriB - * @return - */ - Self disconnect(Uri uriA, Uri uriB); - - /** - * Removes relationship from multiple objects - disconnects A from all objects specified in list - * @param uriA - * @param uris - * @return - */ - Self disconnect(Uri uriA, List uris); - - /** - * Deletes object from A&AI. Automatically handles resource-version. - * - * @param uri - * @return - */ - Self delete(Uri uri); - - /** - * @param obj - can be any object which will marshal into a valid A&AI payload - * @param uri - * @return - */ - Self update(Uri uri, Object obj); - - /** - * Executes all created transactions in A&AI - * @throws BulkProcessFailed - */ - void execute() throws BulkProcessFailed; - -} \ No newline at end of file diff --git a/common/src/main/java/org/onap/so/client/graphinventory/TransactionBuilder.java b/common/src/main/java/org/onap/so/client/graphinventory/TransactionBuilder.java new file mode 100644 index 0000000000..2cab4b5654 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/TransactionBuilder.java @@ -0,0 +1,10 @@ +package org.onap.so.client.graphinventory; + +public interface TransactionBuilder { + + + void put(String uri, Object body); + void delete(String uri, Object body); + void patch(String uri,Object body); + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java new file mode 100644 index 0000000000..7da1408f2d --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNode.java @@ -0,0 +1,76 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.graphinventory.entities; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.onap.so.client.aai.entities.QueryStep; +import org.onap.so.client.graphinventory.GraphInventoryObjectName; + +public class DSLNode implements QueryStep { + + private final String nodeName; + private final List nodeKeys; + private final StringBuilder query = new StringBuilder(); + private boolean output = false; + + public DSLNode() { + this.nodeName = ""; + this.nodeKeys = new ArrayList<>(); + + } + public DSLNode(GraphInventoryObjectName name) { + this.nodeName = name.typeName(); + this.nodeKeys = new ArrayList<>(); + query.append(nodeName); + } + public DSLNode(GraphInventoryObjectName name, DSLNodeKey... key) { + this.nodeName = name.typeName(); + this.nodeKeys = Arrays.asList(key); + query.append(nodeName); + } + + public DSLNode output() { + this.output = true; + + return this; + } + + public DSLNode and(DSLNodeKey... key) { + this.nodeKeys.addAll(Arrays.asList(key)); + + return this; + } + + @Override + public String build() { + if (output) { + query.append("*"); + } + for (DSLNodeKey key : nodeKeys) { + query.append(key.build()); + } + + return query.toString(); + } +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java new file mode 100644 index 0000000000..159bfb1c29 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLNodeKey.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.graphinventory.entities; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.onap.so.client.aai.entities.QueryStep; + +import com.google.common.base.Joiner; + + +public class DSLNodeKey implements QueryStep { + + private boolean not = false; + private final StringBuilder query = new StringBuilder(); + private final String keyName; + private final List values; + public DSLNodeKey(String keyName, String... value) { + + this.keyName = keyName; + this.values = Arrays.asList(value); + } + + public DSLNodeKey not() { + + this.not = true; + return this; + } + + @Override + public String build() { + + if (not) { + query.append(" !"); + } + query.append("('").append(keyName).append("', "); + List temp = new ArrayList<>(); + for (String item : values) { + if (item.equals("null")) { + temp.add(String.format("' %s '", item)); + } else if (item.equals("")){ + temp.add("' '"); + } else { + temp.add(String.format("'%s'", item)); + } + } + query.append(Joiner.on(", ").join(temp)).append(")"); + + return query.toString(); + } +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQuery.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQuery.java new file mode 100644 index 0000000000..c8ab015b26 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQuery.java @@ -0,0 +1,47 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.graphinventory.entities; + +import com.fasterxml.jackson.annotation.JsonInclude; + +@JsonInclude(JsonInclude.Include.NON_NULL) +public class DSLQuery { + + private String dsl; + + public DSLQuery() { + + } + + public DSLQuery(String dsl) { + this.dsl = dsl; + } + + public String getDsl() { + return dsl; + } + + public void setDsl(String dsl) { + this.dsl = dsl; + } + + +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java new file mode 100644 index 0000000000..73dccea8e6 --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/DSLQueryBuilder.java @@ -0,0 +1,113 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.graphinventory.entities; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import org.onap.so.client.aai.entities.QueryStep; + +import com.google.common.base.Joiner; + + +public class DSLQueryBuilder implements QueryStep { + + private List steps = new ArrayList<>(); + + + public DSLQueryBuilder() { + + } + public DSLQueryBuilder(DSLNode node) { + steps.add(node); + } + + public DSLQueryBuilder node(DSLNode node) { + steps.add(node); + + return (DSLQueryBuilder) this; + } + public DSLQueryBuilder output() { + if (steps.get(steps.size() -1) instanceof DSLNode) { + ((DSLNode)steps.get(steps.size() -1)).output(); + } + return this; + } + + public DSLQueryBuilder union(final DSLQueryBuilder... union) { + + List> unions = Arrays.asList(union); + steps.add(() -> { + StringBuilder query = new StringBuilder(); + + query.append("> [ ").append( + Joiner.on(", ").join( + unions.stream().map(item -> item.build()).collect(Collectors.toList()))) + .append(" ]"); + return query.toString(); + }); + + return (DSLQueryBuilder) this; + } + + public DSLQueryBuilder where(DSLQueryBuilder where) { + + steps.add(() -> { + StringBuilder query = new StringBuilder(); + query.append(where.build()).append(")"); + String result = query.toString(); + if (!result.startsWith(">")) { + result = "> " + result; + } + return "(" + result; + }); + return this; + } + + public DSLQueryBuilder to(DSLQueryBuilder to) { + steps.add(() -> { + StringBuilder query = new StringBuilder(); + + query.append("> ").append(to.build()); + return query.toString(); + }); + return this; + } + + public String limit(int limit) { + return compile() + " LIMIT " + limit; + } + + @Override + public String build() { + return compile(); + } + + private String compile() { + return Joiner.on(" ").join(steps.stream().map(item -> item.build()).collect(Collectors.toList())); + } + + protected QueryStep getFirst() { + return steps.get(0); + } +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/__.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/__.java new file mode 100644 index 0000000000..184f412adb --- /dev/null +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/__.java @@ -0,0 +1,59 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.so.client.graphinventory.entities; + +import org.onap.so.client.graphinventory.GraphInventoryObjectName; + +public class __ { + + protected __() { + + } + + public static DSLQueryBuilder identity() { + return new DSLQueryBuilder<>(); + } + public static DSLQueryBuilder start(DSLNode node) { + return new DSLQueryBuilder<>(node); + } + public static DSLQueryBuilder node(GraphInventoryObjectName name) { + + return __.start(new DSLNode(name)); + } + + public static DSLQueryBuilder node(GraphInventoryObjectName name, DSLNodeKey... key) { + return __.start(new DSLNode(name, key)); + } + + public static DSLNodeKey key(String keyName, String... value) { + return new DSLNodeKey(keyName, value); + } + + public static DSLQueryBuilder union(final DSLQueryBuilder... traversal) { + + return __.identity().union(traversal); + } + +public static DSLQueryBuilder where(DSLQueryBuilder traversal) { + + return __.identity().where(traversal); + } +} diff --git a/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java b/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java index 93de9139f9..dc4179a86f 100644 --- a/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java +++ b/common/src/main/java/org/onap/so/client/graphinventory/entities/uri/SimpleUri.java @@ -98,6 +98,13 @@ public class SimpleUri implements GraphInventoryResourceUri, Serializable { validateValuesSize(childType.partialUri(), values); } + protected SimpleUri(GraphInventoryResourceUri parentUri, GraphInventoryObjectPlurals childType) { + this.type = null; + this.pluralType = childType; + this.internalURI = UriBuilder.fromUri(parentUri.build()).path(childType.partialUri()); + this.values = new Object[0]; + } + protected void setInternalURI(UriBuilder builder) { this.internalURI = builder; } -- cgit 1.2.3-korg