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;
27 import javax.ws.rs.NotFoundException;
28 import javax.ws.rs.core.GenericType;
29 import org.onap.aai.domain.yang.Relationship;
30 import org.onap.so.client.aai.AAIVersion;
31 import org.onap.so.client.aai.entities.singletransaction.SingleTransactionRequest;
32 import org.onap.so.client.graphinventory.entities.GraphInventoryEdgeLabel;
33 import org.onap.so.client.graphinventory.entities.uri.GraphInventoryResourceUri;
34 import org.onap.so.client.graphinventory.exceptions.BulkProcessFailed;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public abstract class GraphInventoryTransactionClient<Self, Uri extends GraphInventoryResourceUri, EdgeLabel extends GraphInventoryEdgeLabel>
39 implements TransactionBuilder {
41 protected static Logger logger = LoggerFactory.getLogger(GraphInventoryTransactionClient.class);
43 protected int actionCount = 0;
45 protected final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter();
47 protected GraphInventoryTransactionClient() {}
50 * creates a new object in A&AI
52 * @param obj - can be any object which will marshal into a valid A&AI payload
56 public Self create(Uri uri, Object obj) {
57 this.put(uri.build().toString(), obj);
58 incrementActionAmount();
63 * creates a new object in A&AI with no payload body
68 public Self createEmpty(Uri uri) {
69 this.put(uri.build().toString(), new HashMap<String, String>());
70 incrementActionAmount();
75 * Will automatically create the object if it does not exist
77 * @param obj - Optional object which serializes to a valid GraphInventory payload
81 public Self createIfNotExists(Uri uri, Optional<Object> obj) {
82 if (!this.exists(uri)) {
83 if (obj.isPresent()) {
84 this.create(uri, obj.get());
86 this.createEmpty(uri);
94 * Adds a relationship between two objects in A&AI
100 public Self connect(Uri uriA, Uri uriB) {
101 GraphInventoryResourceUri uriAClone = uriA.clone();
102 this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
103 incrementActionAmount();
108 * relationship between multiple objects in A&AI - connects A to all objects specified in list
114 public Self connect(Uri uriA, List<Uri> uris) {
115 for (Uri uri : uris) {
116 this.connect(uriA, uri);
122 * relationship between multiple objects in A&AI - connects A to all objects specified in list
128 public Self connect(Uri uriA, Uri uriB, EdgeLabel label) {
129 GraphInventoryResourceUri uriAClone = uriA.clone();
130 this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB, label));
135 * relationship between multiple objects in A&AI - connects A to all objects specified in list
141 public Self connect(Uri uriA, List<Uri> uris, EdgeLabel label) {
142 for (Uri uri : uris) {
143 this.connect(uriA, uri, label);
149 * Removes relationship from two objects in A&AI
155 public Self disconnect(Uri uriA, Uri uriB) {
156 GraphInventoryResourceUri uriAClone = uriA.clone();
157 this.delete(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
158 incrementActionAmount();
163 * 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);
177 * Deletes object from A&AI. Automatically handles resource-version.
182 public Self delete(Uri uri) {
183 Map<String, Object> result = this.get(new GenericType<Map<String, Object>>() {}, (Uri) uri.clone())
184 .orElseThrow(() -> new NotFoundException(uri.build() + " does not exist in " + this.getGraphDBName()));
185 String resourceVersion = (String) result.get("resource-version");
186 this.delete(uri.resourceVersion(resourceVersion).build().toString(), "");
187 incrementActionAmount();
191 protected abstract <T> Optional<T> get(GenericType<T> genericType, Uri clone);
193 protected abstract boolean exists(Uri uri);
195 protected abstract String getGraphDBName();
198 * @param obj - can be any object which will marshal into a valid A&AI payload
202 public Self update(Uri uri, Object obj) {
204 final String payload = getPatchConverter().convertPatchFormat(obj);
205 this.patch(uri.build().toString(), payload);
206 incrementActionAmount();
210 private void incrementActionAmount() {
215 * Executes all created transactions in A&AI
217 * @throws BulkProcessFailed
219 public abstract void execute() throws BulkProcessFailed;
221 private Relationship buildRelationship(Uri uri) {
222 return buildRelationship(uri, Optional.empty());
225 private Relationship buildRelationship(Uri uri, EdgeLabel label) {
226 return buildRelationship(uri, Optional.of(label));
229 private Relationship buildRelationship(Uri uri, Optional<EdgeLabel> label) {
230 final Relationship result = new Relationship();
231 result.setRelatedLink(uri.build().toString());
232 if (label.isPresent()) {
233 result.setRelationshipLabel(label.toString());
238 protected GraphInventoryPatchConverter getPatchConverter() {
239 return this.patchConverter;