4390e7e6bcbae1b74821c4d41996feb73a0ac2e7
[so.git] /
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.aaiclient.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.aaiclient.client.graphinventory.entities.GraphInventoryEdgeLabel;
31 import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventoryResourceUri;
32 import org.onap.aaiclient.client.graphinventory.entities.uri.GraphInventorySingleResourceUri;
33 import org.onap.aaiclient.client.graphinventory.exceptions.BulkProcessFailed;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public abstract class GraphInventoryTransactionClient<Self, Uri extends GraphInventoryResourceUri<?, ?>, SingleUri extends GraphInventorySingleResourceUri<?, ?, ?, ?, ?, ?>, EdgeLabel extends GraphInventoryEdgeLabel> {
38
39     protected static Logger logger = LoggerFactory.getLogger(GraphInventoryTransactionClient.class);
40
41     protected int actionCount = 0;
42
43     protected final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter();
44
45     protected GraphInventoryTransactionClient() {}
46
47     /**
48      * creates a new object in A&AI
49      * 
50      * @param obj - can be any object which will marshal into a valid A&AI payload
51      * @param uri
52      * @return
53      */
54     public Self create(SingleUri uri, Object obj) {
55         this.put(uri.build().toString(), obj);
56         incrementActionAmount();
57         return (Self) this;
58     }
59
60     /**
61      * creates a new object in A&AI with no payload body
62      * 
63      * @param uri
64      * @return
65      */
66     public Self createEmpty(SingleUri uri) {
67         this.put(uri.build().toString(), new HashMap<String, String>());
68         incrementActionAmount();
69         return (Self) this;
70     }
71
72     /**
73      * Will automatically create the object if it does not exist
74      * 
75      * @param obj - Optional object which serializes to a valid GraphInventory payload
76      * @param uri
77      * @return
78      */
79     public Self createIfNotExists(SingleUri uri, Optional<Object> obj) {
80         if (!this.exists((Uri) uri)) {
81             if (obj.isPresent()) {
82                 this.create(uri, obj.get());
83             } else {
84                 this.createEmpty(uri);
85             }
86
87         }
88         return (Self) this;
89     }
90
91     /**
92      * Adds a relationship between two objects in A&AI
93      * 
94      * @param uriA
95      * @param uriB
96      * @return
97      */
98     public Self connect(SingleUri uriA, SingleUri uriB) {
99         GraphInventorySingleResourceUri uriAClone = uriA.clone();
100         this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
101         incrementActionAmount();
102         return (Self) this;
103     }
104
105     /**
106      * relationship between multiple objects in A&AI - connects A to all objects specified in list
107      * 
108      * @param uriA
109      * @param uris
110      * @return
111      */
112     public Self connect(SingleUri uriA, List<SingleUri> uris) {
113         for (SingleUri uri : uris) {
114             this.connect(uriA, uri);
115         }
116         return (Self) this;
117     }
118
119     /**
120      * relationship between multiple objects in A&AI - connects A to all objects specified in list
121      * 
122      * @param uriA
123      * @param uris
124      * @return
125      */
126     public Self connect(SingleUri uriA, SingleUri uriB, EdgeLabel label) {
127         GraphInventorySingleResourceUri uriAClone = uriA.clone();
128         this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB, label));
129         return (Self) this;
130     }
131
132     /**
133      * relationship between multiple objects in A&AI - connects A to all objects specified in list
134      * 
135      * @param uriA
136      * @param uris
137      * @return
138      */
139     public Self connect(SingleUri uriA, List<SingleUri> uris, EdgeLabel label) {
140         for (SingleUri uri : uris) {
141             this.connect(uriA, uri, label);
142         }
143         return (Self) this;
144     }
145
146     /**
147      * Removes relationship from two objects in A&AI
148      * 
149      * @param uriA
150      * @param uriB
151      * @return
152      */
153     public Self disconnect(SingleUri uriA, SingleUri uriB) {
154         GraphInventorySingleResourceUri uriAClone = uriA.clone();
155         this.delete(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
156         incrementActionAmount();
157         return (Self) this;
158     }
159
160     /**
161      * Removes relationship from multiple objects - disconnects A from all objects specified in list
162      * 
163      * @param uriA
164      * @param uris
165      * @return
166      */
167     public Self disconnect(SingleUri uriA, List<SingleUri> uris) {
168         for (SingleUri uri : uris) {
169             this.disconnect(uriA, uri);
170         }
171         return (Self) this;
172     }
173
174     /**
175      * Deletes object from A&AI. Automatically handles resource-version.
176      * 
177      * @param uri
178      * @return
179      */
180     public Self delete(SingleUri uri) {
181         Map<String, Object> result = this.get(new GenericType<Map<String, Object>>() {}, (Uri) uri)
182                 .orElseThrow(() -> new NotFoundException(uri.build() + " does not exist in " + this.getGraphDBName()));
183         String resourceVersion = (String) result.get("resource-version");
184         this.delete(uri.resourceVersion(resourceVersion).build().toString());
185         incrementActionAmount();
186         return (Self) this;
187     }
188
189     protected abstract <T> Optional<T> get(GenericType<T> genericType, Uri clone);
190
191     protected abstract boolean exists(Uri uri);
192
193     protected abstract String getGraphDBName();
194
195     protected abstract void put(String uri, Object body);
196
197     protected abstract void delete(String uri);
198
199     protected abstract void delete(String uri, Object obj);
200
201     protected abstract void patch(String uri, Object body);
202
203     /**
204      * @param obj - can be any object which will marshal into a valid A&AI payload
205      * @param uri
206      * @return
207      */
208     public Self update(Uri uri, Object obj) {
209
210         final String payload = getPatchConverter().convertPatchFormat(obj);
211         this.patch(uri.build().toString(), payload);
212         incrementActionAmount();
213         return (Self) this;
214     }
215
216     private void incrementActionAmount() {
217         actionCount++;
218     }
219
220     /**
221      * Executes all created transactions in A&AI
222      * 
223      * @throws BulkProcessFailed
224      */
225     public abstract void execute() throws BulkProcessFailed;
226
227
228     /**
229      * Executes all created transactions in A&AI, with optional dry run flag
230      * 
231      * @throws BulkProcessFailed
232      */
233     public abstract void execute(boolean dryrun) throws BulkProcessFailed;
234
235     private Relationship buildRelationship(SingleUri uri) {
236         return buildRelationship(uri, Optional.empty());
237     }
238
239     private Relationship buildRelationship(SingleUri uri, EdgeLabel label) {
240         return buildRelationship(uri, Optional.of(label));
241     }
242
243     private Relationship buildRelationship(SingleUri uri, Optional<EdgeLabel> label) {
244         final Relationship result = new Relationship();
245         result.setRelatedLink(uri.build().toString());
246         if (label.isPresent()) {
247             result.setRelationshipLabel(label.toString());
248         }
249         return result;
250     }
251
252     protected GraphInventoryPatchConverter getPatchConverter() {
253         return this.patchConverter;
254     }
255
256 }