Replaced all tabs with spaces in java and pom.xml
[so.git] / common / src / main / java / org / onap / so / client / graphinventory / GraphInventoryTransactionClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.graphinventory;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
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;
37
38 public abstract class GraphInventoryTransactionClient<Self, Uri extends GraphInventoryResourceUri, EdgeLabel extends GraphInventoryEdgeLabel>
39         implements TransactionBuilder {
40
41     protected static Logger logger = LoggerFactory.getLogger(GraphInventoryTransactionClient.class);
42
43     protected int actionCount = 0;
44
45     protected final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter();
46
47     protected GraphInventoryTransactionClient() {}
48
49     /**
50      * creates a new object in A&AI
51      * 
52      * @param obj - can be any object which will marshal into a valid A&AI payload
53      * @param uri
54      * @return
55      */
56     public Self create(Uri uri, Object obj) {
57         this.put(uri.build().toString(), obj);
58         incrementActionAmount();
59         return (Self) this;
60     }
61
62     /**
63      * creates a new object in A&AI with no payload body
64      * 
65      * @param uri
66      * @return
67      */
68     public Self createEmpty(Uri uri) {
69         this.put(uri.build().toString(), new HashMap<String, String>());
70         incrementActionAmount();
71         return (Self) this;
72     }
73
74     /**
75      * Will automatically create the object if it does not exist
76      * 
77      * @param obj - Optional object which serializes to a valid GraphInventory payload
78      * @param uri
79      * @return
80      */
81     public Self createIfNotExists(Uri uri, Optional<Object> obj) {
82         if (!this.exists(uri)) {
83             if (obj.isPresent()) {
84                 this.create(uri, obj.get());
85             } else {
86                 this.createEmpty(uri);
87             }
88
89         }
90         return (Self) this;
91     }
92
93     /**
94      * Adds a relationship between two objects in A&AI
95      * 
96      * @param uriA
97      * @param uriB
98      * @return
99      */
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();
104         return (Self) this;
105     }
106
107     /**
108      * relationship between multiple objects in A&AI - connects A to all objects specified in list
109      * 
110      * @param uriA
111      * @param uris
112      * @return
113      */
114     public Self connect(Uri uriA, List<Uri> uris) {
115         for (Uri uri : uris) {
116             this.connect(uriA, uri);
117         }
118         return (Self) this;
119     }
120
121     /**
122      * relationship between multiple objects in A&AI - connects A to all objects specified in list
123      * 
124      * @param uriA
125      * @param uris
126      * @return
127      */
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));
131         return (Self) this;
132     }
133
134     /**
135      * relationship between multiple objects in A&AI - connects A to all objects specified in list
136      * 
137      * @param uriA
138      * @param uris
139      * @return
140      */
141     public Self connect(Uri uriA, List<Uri> uris, EdgeLabel label) {
142         for (Uri uri : uris) {
143             this.connect(uriA, uri, label);
144         }
145         return (Self) this;
146     }
147
148     /**
149      * Removes relationship from two objects in A&AI
150      * 
151      * @param uriA
152      * @param uriB
153      * @return
154      */
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();
159         return (Self) this;
160     }
161
162     /**
163      * Removes relationship from multiple objects - disconnects A from all objects specified in list
164      * 
165      * @param uriA
166      * @param uris
167      * @return
168      */
169     public Self disconnect(Uri uriA, List<Uri> uris) {
170         for (Uri uri : uris) {
171             this.disconnect(uriA, uri);
172         }
173         return (Self) this;
174     }
175
176     /**
177      * Deletes object from A&AI. Automatically handles resource-version.
178      * 
179      * @param uri
180      * @return
181      */
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();
188         return (Self) this;
189     }
190
191     protected abstract <T> Optional<T> get(GenericType<T> genericType, Uri clone);
192
193     protected abstract boolean exists(Uri uri);
194
195     protected abstract String getGraphDBName();
196
197     /**
198      * @param obj - can be any object which will marshal into a valid A&AI payload
199      * @param uri
200      * @return
201      */
202     public Self update(Uri uri, Object obj) {
203
204         final String payload = getPatchConverter().convertPatchFormat(obj);
205         this.patch(uri.build().toString(), payload);
206         incrementActionAmount();
207         return (Self) this;
208     }
209
210     private void incrementActionAmount() {
211         actionCount++;
212     }
213
214     /**
215      * Executes all created transactions in A&AI
216      * 
217      * @throws BulkProcessFailed
218      */
219     public abstract void execute() throws BulkProcessFailed;
220
221     private Relationship buildRelationship(Uri uri) {
222         return buildRelationship(uri, Optional.empty());
223     }
224
225     private Relationship buildRelationship(Uri uri, EdgeLabel label) {
226         return buildRelationship(uri, Optional.of(label));
227     }
228
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());
234         }
235         return result;
236     }
237
238     protected GraphInventoryPatchConverter getPatchConverter() {
239         return this.patchConverter;
240     }
241
242 }