2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.client.graphinventory;
23 import java.util.HashMap;
24 import java.util.List;
26 import java.util.Optional;
28 import javax.ws.rs.NotFoundException;
29 import javax.ws.rs.core.GenericType;
31 import org.onap.aai.domain.yang.Relationship;
32 import org.onap.so.client.aai.AAIVersion;
33 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest;
34 import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel;
35 import org.onap.so.client.graphinventory.entities.uri.GraphInventoryResourceUri;
36 import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public abstract class GraphInventoryTransactionClient<Self, Uri extends GraphInventoryResourceUri, EdgeLabel extends GraphInventoryEdgeLabel> implements TransactionBuilder {
42 protected static Logger logger = LoggerFactory.getLogger(GraphInventoryTransactionClient.class);
44 protected int actionCount = 0;
46 protected final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter();
48 protected GraphInventoryTransactionClient() {
52 * creates a new object in A&AI
54 * @param obj - can be any object which will marshal into a valid A&AI payload
58 public Self create(Uri uri, Object obj) {
59 this.put(uri.build().toString(), obj);
60 incrementActionAmount();
65 * creates a new object in A&AI with no payload body
70 public Self createEmpty(Uri uri) {
71 this.put(uri.build().toString(), new HashMap<String, String>());
72 incrementActionAmount();
77 * Will automatically create the object if it does not exist
79 * @param obj - Optional object which serializes to a valid GraphInventory payload
83 public Self createIfNotExists(Uri uri, Optional<Object> obj) {
84 if(!this.exists(uri)){
85 if (obj.isPresent()) {
86 this.create(uri, obj.get());
87 incrementActionAmount();
89 this.createEmpty(uri);
90 incrementActionAmount();
98 * Adds a relationship between two objects in A&AI
103 public Self connect(Uri uriA, Uri uriB) {
104 GraphInventoryResourceUri uriAClone = uriA.clone();
105 this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
106 incrementActionAmount();
111 * relationship between multiple objects in A&AI - connects A to all objects specified in list
117 public Self connect(Uri uriA, List<Uri> uris) {
118 for (Uri uri : uris) {
119 this.connect(uriA, uri);
125 * relationship between multiple objects in A&AI - connects A to all objects specified in list
131 public Self connect(Uri uriA, Uri uriB, EdgeLabel label) {
132 GraphInventoryResourceUri uriAClone = uriA.clone();
133 this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB, label));
138 * relationship between multiple objects in A&AI - connects A to all objects specified in list
144 public Self connect(Uri uriA, List<Uri> uris, EdgeLabel label) {
145 for (Uri uri : uris) {
146 this.connect(uriA, uri, label);
152 * Removes relationship from two objects in A&AI
158 public Self disconnect(Uri uriA, Uri uriB) {
159 GraphInventoryResourceUri uriAClone = uriA.clone();
160 this.delete(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
161 incrementActionAmount();
166 * Removes relationship from multiple objects - disconnects A from all objects specified in list
171 public Self disconnect(Uri uriA, List<Uri> uris) {
172 for (Uri uri : uris) {
173 this.disconnect(uriA, uri);
178 * Deletes object from A&AI. Automatically handles resource-version.
183 public Self delete(Uri uri) {
184 Map<String, Object> result = this.get(new GenericType<Map<String, Object>>(){}, (Uri)uri.clone())
185 .orElseThrow(() -> new NotFoundException(uri.build() + " does not exist in " + this.getGraphDBName()));
186 String resourceVersion = (String) result.get("resource-version");
187 this.delete(uri.resourceVersion(resourceVersion).build().toString(), "");
188 incrementActionAmount();
192 protected abstract <T> Optional<T> get(GenericType<T> genericType, Uri clone);
194 protected abstract boolean exists(Uri uri);
196 protected abstract String getGraphDBName();
199 * @param obj - can be any object which will marshal into a valid A&AI payload
203 public Self update(Uri uri, Object obj) {
205 final String payload = getPatchConverter().convertPatchFormat(obj);
206 this.patch(uri.build().toString(), payload);
207 incrementActionAmount();
211 private void incrementActionAmount() {
215 * Executes all created transactions in A&AI
216 * @throws BulkProcessFailed
218 public abstract void execute() throws BulkProcessFailed;
220 private Relationship buildRelationship(Uri uri) {
221 return buildRelationship(uri, Optional.empty());
224 private Relationship buildRelationship(Uri uri, EdgeLabel label) {
225 return buildRelationship(uri, Optional.of(label));
227 private Relationship buildRelationship(Uri uri, Optional<EdgeLabel> label) {
228 final Relationship result = new Relationship();
229 result.setRelatedLink(uri.build().toString());
230 if (label.isPresent()) {
231 result.setRelationshipLabel(label.toString());
236 protected GraphInventoryPatchConverter getPatchConverter() {
237 return this.patchConverter;