Merge "fixed CandidateType json serialization"
[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                                 incrementActionAmount();
88                         } else {
89                                 this.createEmpty(uri);
90                                 incrementActionAmount();
91                         }
92                         
93                 }
94                 return (Self)this;
95         }
96         
97         /**
98          * Adds a relationship between two objects in A&AI 
99          * @param uriA
100          * @param uriB
101          * @return
102          */
103         public Self connect(Uri uriA, Uri uriB) {
104                 GraphInventoryResourceUri uriAClone = uriA.clone();
105                 this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
106                 incrementActionAmount();
107                 return (Self)this;
108         }
109
110         /**
111          * relationship between multiple objects in A&AI - connects A to all objects specified in list
112          * 
113          * @param uriA
114          * @param uris
115          * @return
116          */
117         public Self connect(Uri uriA, List<Uri> uris) {
118                 for (Uri uri : uris) {
119                         this.connect(uriA, uri);
120                 }
121                 return (Self)this;
122         }
123         
124         /**
125          * relationship between multiple objects in A&AI - connects A to all objects specified in list
126          * 
127          * @param uriA
128          * @param uris
129          * @return
130          */
131         public Self connect(Uri uriA, Uri uriB, EdgeLabel label) {
132                 GraphInventoryResourceUri uriAClone = uriA.clone();
133                 this.put(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB, label));
134                 return (Self)this;
135         }
136         
137         /**
138          * relationship between multiple objects in A&AI - connects A to all objects specified in list
139          * 
140          * @param uriA
141          * @param uris
142          * @return
143          */
144         public Self connect(Uri uriA, List<Uri> uris, EdgeLabel label) {
145                 for (Uri uri : uris) {
146                         this.connect(uriA, uri, label);
147                 }
148                 return (Self)this;
149         }
150
151         /**
152          * Removes relationship from two objects in A&AI
153          * 
154          * @param uriA
155          * @param uriB
156          * @return
157          */
158         public Self disconnect(Uri uriA, Uri uriB) {
159                 GraphInventoryResourceUri uriAClone = uriA.clone();
160                 this.delete(uriAClone.relationshipAPI().build().toString(), this.buildRelationship(uriB));
161                 incrementActionAmount();
162                 return (Self)this;
163         }
164
165         /**
166          * Removes relationship from multiple objects - disconnects A from all objects specified in list
167          * @param uriA
168          * @param uris
169          * @return
170          */
171         public Self disconnect(Uri uriA, List<Uri> uris) {
172                 for (Uri uri : uris) {
173                         this.disconnect(uriA, uri);
174                 }
175                 return (Self)this;
176         }
177         /**
178          * Deletes object from A&AI. Automatically handles resource-version.
179          * 
180          * @param uri
181          * @return
182          */
183         public Self delete(Uri uri) {
184                 Map<String, Object> result = this.get(new GenericType<Map<String, Object>>(){}, (Uri)uri.clone())
185                                 .orElseThrow(() -> new NotFoundException(uri.build() + " does not exist in " + this.getGraphDBName()));
186                 String resourceVersion = (String) result.get("resource-version");
187                 this.delete(uri.resourceVersion(resourceVersion).build().toString(), "");
188                 incrementActionAmount();
189                 return (Self)this;
190         }
191
192         protected abstract <T> Optional<T> get(GenericType<T> genericType, Uri clone);
193         
194         protected abstract boolean exists(Uri uri);
195         
196         protected abstract String getGraphDBName();
197
198         /**
199          * @param obj - can be any object which will marshal into a valid A&AI payload
200          * @param uri
201          * @return
202          */
203         public Self update(Uri uri, Object obj) {
204                 
205                 final String payload = getPatchConverter().convertPatchFormat(obj);
206                 this.patch(uri.build().toString(), payload);
207                 incrementActionAmount();
208                 return (Self)this;
209         }
210
211         private void incrementActionAmount() {
212                 actionCount++;
213         }
214         /**
215          * Executes all created transactions in A&AI
216          * @throws BulkProcessFailed 
217          */
218         public abstract void execute() throws BulkProcessFailed;
219         
220         private Relationship buildRelationship(Uri uri) {
221                 return buildRelationship(uri, Optional.empty());
222         }
223         
224         private Relationship buildRelationship(Uri uri, EdgeLabel label) {
225                 return buildRelationship(uri, Optional.of(label));
226         }
227         private Relationship buildRelationship(Uri uri, Optional<EdgeLabel> label) {
228                 final Relationship result = new Relationship();
229                 result.setRelatedLink(uri.build().toString());
230                 if (label.isPresent()) {
231                         result.setRelationshipLabel(label.toString());
232                 }
233                 return result;
234         }
235         
236         protected GraphInventoryPatchConverter getPatchConverter() {
237                 return this.patchConverter;
238         }
239         
240 }