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());
88 this.createEmpty(uri);
96 * Adds a relationship between two objects in A&AI
101 public Self connect(Uri uriA, Uri uriB) {
102 GraphInventoryResourceUri uriAClone = uriA.clone();
103 this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
104 incrementActionAmount();
109 * relationship between multiple objects in A&AI - connects A to all objects specified in list
115 public Self connect(Uri uriA, List<Uri> uris) {
116 for (Uri uri : uris) {
117 this.connect(uriA, uri);
123 * relationship between multiple objects in A&AI - connects A to all objects specified in list
129 public Self connect(Uri uriA, Uri uriB, EdgeLabel label) {
130 GraphInventoryResourceUri uriAClone = uriA.clone();
131 this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB, label));
136 * relationship between multiple objects in A&AI - connects A to all objects specified in list
142 public Self connect(Uri uriA, List<Uri> uris, EdgeLabel label) {
143 for (Uri uri : uris) {
144 this.connect(uriA, uri, label);
150 * Removes relationship from two objects in A&AI
156 public Self disconnect(Uri uriA, Uri uriB) {
157 GraphInventoryResourceUri uriAClone = uriA.clone();
158 this.delete(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
159 incrementActionAmount();
164 * Removes relationship from multiple objects - disconnects A from all objects specified in list
169 public Self disconnect(Uri uriA, List<Uri> uris) {
170 for (Uri uri : uris) {
171 this.disconnect(uriA, uri);
176 * Deletes object from A&AI. Automatically handles resource-version.
181 public Self delete(Uri uri) {
182 Map<String, Object> result = this.get(new GenericType<Map<String, Object>>(){}, (Uri)uri.clone())
183 .orElseThrow(() -> new NotFoundException(uri.build() + " does not exist in " + this.getGraphDBName()));
184 String resourceVersion = (String) result.get("resource-version");
185 this.delete(uri.resourceVersion(resourceVersion).build().toString(), "");
186 incrementActionAmount();
190 protected abstract <T> Optional<T> get(GenericType<T> genericType, Uri clone);
192 protected abstract boolean exists(Uri uri);
194 protected abstract String getGraphDBName();
197 * @param obj - can be any object which will marshal into a valid A&AI payload
201 public Self update(Uri uri, Object obj) {
203 final String payload = getPatchConverter().convertPatchFormat(obj);
204 this.patch(uri.build().toString(), payload);
205 incrementActionAmount();
209 private void incrementActionAmount() {
213 * Executes all created transactions in A&AI
214 * @throws BulkProcessFailed
216 public abstract void execute() throws BulkProcessFailed;
218 private Relationship buildRelationship(Uri uri) {
219 return buildRelationship(uri, Optional.empty());
222 private Relationship buildRelationship(Uri uri, EdgeLabel label) {
223 return buildRelationship(uri, Optional.of(label));
225 private Relationship buildRelationship(Uri uri, Optional<EdgeLabel> label) {
226 final Relationship result = new Relationship();
227 result.setRelatedLink(uri.build().toString());
228 if (label.isPresent()) {
229 result.setRelationshipLabel(label.toString());
234 protected GraphInventoryPatchConverter getPatchConverter() {
235 return this.patchConverter;