4c228b2ea34dc53510778bb23050b483a37a7422
[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
28 import javax.ws.rs.NotFoundException;
29 import javax.ws.rs.core.GenericType;
30
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;
39
40 public abstract class GraphInventoryTransactionClient<Self, Uri extends GraphInventoryResourceUri, EdgeLabel extends GraphInventoryEdgeLabel> implements TransactionBuilder {
41
42         protected static Logger logger = LoggerFactory.getLogger(GraphInventoryTransactionClient.class);
43
44         protected int actionCount = 0;
45         
46         protected final GraphInventoryPatchConverter patchConverter = new GraphInventoryPatchConverter();
47         
48         protected GraphInventoryTransactionClient() {
49         }
50
51         /**
52          * creates a new object in A&AI
53          * 
54          * @param obj - can be any object which will marshal into a valid A&AI payload
55          * @param uri
56          * @return
57          */
58         public Self create(Uri uri, Object obj) {
59                 this.put(uri.build().toString(), obj);
60                 incrementActionAmount();
61                 return (Self)this;
62         }
63
64         /**
65          * creates a new object in A&AI with no payload body
66          * 
67          * @param uri
68          * @return
69          */
70         public Self createEmpty(Uri uri) {
71                 this.put(uri.build().toString(), new HashMap<String, String>());
72                 incrementActionAmount();
73                 return (Self)this;
74         }
75
76         /**
77          * Will automatically create the object if it does not exist
78          * 
79          * @param obj - Optional object which serializes to a valid GraphInventory payload
80          * @param uri
81          * @return
82          */
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                         } else {
88                                 this.createEmpty(uri);
89                         }
90                         
91                 }
92                 return (Self)this;
93         }
94         
95         /**
96          * Adds a relationship between two objects in A&AI 
97          * @param uriA
98          * @param uriB
99          * @return
100          */
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();
105                 return (Self)this;
106         }
107
108         /**
109          * relationship between multiple objects in A&AI - connects A to all objects specified in list
110          * 
111          * @param uriA
112          * @param uris
113          * @return
114          */
115         public Self connect(Uri uriA, List<Uri> uris) {
116                 for (Uri uri : uris) {
117                         this.connect(uriA, uri);
118                 }
119                 return (Self)this;
120         }
121         
122         /**
123          * relationship between multiple objects in A&AI - connects A to all objects specified in list
124          * 
125          * @param uriA
126          * @param uris
127          * @return
128          */
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));
132                 return (Self)this;
133         }
134         
135         /**
136          * relationship between multiple objects in A&AI - connects A to all objects specified in list
137          * 
138          * @param uriA
139          * @param uris
140          * @return
141          */
142         public Self connect(Uri uriA, List<Uri> uris, EdgeLabel label) {
143                 for (Uri uri : uris) {
144                         this.connect(uriA, uri, label);
145                 }
146                 return (Self)this;
147         }
148
149         /**
150          * Removes relationship from two objects in A&AI
151          * 
152          * @param uriA
153          * @param uriB
154          * @return
155          */
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();
160                 return (Self)this;
161         }
162
163         /**
164          * Removes relationship from multiple objects - disconnects A from all objects specified in list
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          * Deletes object from A&AI. Automatically handles resource-version.
177          * 
178          * @param uri
179          * @return
180          */
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();
187                 return (Self)this;
188         }
189
190         protected abstract <T> Optional<T> get(GenericType<T> genericType, Uri clone);
191         
192         protected abstract boolean exists(Uri uri);
193         
194         protected abstract String getGraphDBName();
195         
196         /**
197          * @param obj - can be any object which will marshal into a valid A&AI payload
198          * @param uri
199          * @return
200          */
201         public Self update(Uri uri, Object obj) {
202                 
203                 final String payload = getPatchConverter().convertPatchFormat(obj);
204                 this.patch(uri.build().toString(), payload);
205                 incrementActionAmount();
206                 return (Self)this;
207         }
208
209         private void incrementActionAmount() {
210                 actionCount++;
211         }
212         /**
213          * Executes all created transactions in A&AI
214          * @throws BulkProcessFailed 
215          */
216         public abstract void execute() throws BulkProcessFailed;
217         
218         private Relationship buildRelationship(Uri uri) {
219                 return buildRelationship(uri, Optional.empty());
220         }
221         
222         private Relationship buildRelationship(Uri uri, EdgeLabel label) {
223                 return buildRelationship(uri, Optional.of(label));
224         }
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());
230                 }
231                 return result;
232         }
233         
234         protected GraphInventoryPatchConverter getPatchConverter() {
235                 return this.patchConverter;
236         }
237         
238 }