Merge "Reorder modifiers"
[so.git] / common / src / main / java / org / openecomp / mso / client / aai / AAIResourcesClient.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.mso.client.aai;
22
23 import java.util.Map;
24 import java.util.Optional;
25 import java.util.UUID;
26
27 import javax.ws.rs.NotFoundException;
28 import javax.ws.rs.client.ResponseProcessingException;
29 import javax.ws.rs.core.GenericType;
30
31 import javax.ws.rs.core.Response;
32 import org.onap.aai.domain.yang.Relationship;
33 import org.openecomp.mso.client.aai.entities.AAIResultWrapper;
34 import org.openecomp.mso.client.aai.entities.uri.AAIResourceUri;
35 import org.openecomp.mso.client.aai.entities.uri.AAIUri;
36 import org.openecomp.mso.client.aai.entities.uri.Depth;
37 import org.openecomp.mso.client.policy.RestClient;
38
39 public class AAIResourcesClient extends AAIClient {
40         
41         private final AAIVersion version;
42                 
43         public AAIResourcesClient() {
44                 super(UUID.randomUUID());
45                 this.version = super.getVersion();
46         }
47         
48         public AAIResourcesClient(AAIVersion version) {
49                 super(UUID.randomUUID());
50                 this.version = version;
51         }
52         
53         public AAIResourcesClient(AAIVersion version, UUID requestId) {
54                 super(requestId);
55                 this.version = version;
56         }
57         
58         /**
59          * creates a new object in A&AI
60          * 
61          * @param obj - can be any object which will marshal into a valid A&AI payload
62          * @param uri
63          * @return
64          */
65         public void create(AAIResourceUri uri, Object obj) {
66                 RestClient aaiRC = this.createClient(uri);
67                 aaiRC.put(obj);
68                 return;
69         }
70         
71         /**
72          * creates a new object in A&AI with no payload body
73          * 
74          * @param uri
75          * @return
76          */
77         public void createEmpty(AAIResourceUri uri) {
78                 RestClient aaiRC = this.createClient(uri);
79                 aaiRC.put("");
80                 return;
81         }
82         
83         /**
84          * returns false if the object does not exist in A&AI
85          * 
86          * @param uri
87          * @return
88          */
89         public boolean exists(AAIResourceUri uri) {
90                 AAIUri forceMinimal = this.addParams(Optional.of(Depth.ZERO), true, uri);
91                 RestClient aaiRC = this.createClient(forceMinimal);
92                 try{
93                         aaiRC.get();    
94                 } catch(ResponseProcessingException e) {
95                         if (e.getCause() instanceof NotFoundException) {
96                                 return false;
97                         } else {
98                                 throw e;
99                         }
100                 }
101                 return true;
102         }
103         
104         /**
105          * Adds a relationship between two objects in A&AI 
106          * @param uriA
107          * @param uriB
108          * @return
109          */
110         public void connect(AAIResourceUri uriA, AAIResourceUri uriB) {
111                 AAIResourceUri uriAClone = uriA.clone();
112                 RestClient aaiRC = this.createClient(uriAClone.relationshipAPI());
113                 aaiRC.put(this.buildRelationship(uriB));
114                 return;
115         }
116         
117         /**
118          * Removes relationship from two objects in A&AI
119          * 
120          * @param uriA
121          * @param uriB
122          * @return
123          */
124         public void disconnect(AAIResourceUri uriA, AAIResourceUri uriB) {
125                 AAIResourceUri uriAClone = uriA.clone();
126                 RestClient aaiRC = this.createClient(uriAClone.relationshipAPI());
127                 aaiRC.delete(this.buildRelationship(uriB));
128                 return;
129         }
130         
131         /**
132          * Deletes object from A&AI. Automatically handles resource-version.
133          * 
134          * @param uri
135          * @return
136          */
137         public void delete(AAIResourceUri uri) {
138                 AAIResourceUri clone = uri.clone();
139                 RestClient aaiRC = this.createClient(clone);
140                 Map<String, Object> result = aaiRC.get(new GenericType<Map<String, Object>>(){});
141                 String resourceVersion = (String) result.get("resource-version");
142                 aaiRC = this.createClient(clone.resourceVersion(resourceVersion));
143                 aaiRC.delete();
144                 return;
145         }
146         
147         /**
148          * @param obj - can be any object which will marshal into a valid A&AI payload
149          * @param uri
150          * @return
151          */
152         public void update(AAIResourceUri uri, Object obj) {
153                 RestClient aaiRC = this.createClient(uri);
154                 aaiRC.patch(obj);
155                 return;
156         }
157
158         /**
159          * Retrieves an object from A&AI and unmarshalls it into the Class specified
160          * @param clazz
161          * @param uri
162          * @return
163          */
164         public <T> T get(Class<T> clazz, AAIResourceUri uri) {
165                 return this.createClient(uri).get(clazz);
166         }
167
168         /**
169          * Retrieves an object from A&AI and returns complete response
170          * @param uri
171          * @return
172          */
173         public Response getFullResponse(AAIResourceUri uri) {
174                 return this.createClient(uri).get();
175         }
176         
177         /**
178          * Retrieves an object from A&AI and automatically unmarshalls it into a Map or List 
179          * @param resultClass
180          * @param uri
181          * @return
182          */
183         public <T> T get(GenericType<T> resultClass, AAIResourceUri uri) {
184                 return this.createClient(uri).get(resultClass);
185         }
186         
187         /**
188          * Retrieves an object from A&AI wrapped in a helper class which offer additional features
189          * 
190          * @param uri
191          * @return
192          */
193         public AAIResultWrapper get(AAIResourceUri uri) {
194                 String json = this.createClient(uri).get(String.class);
195                 
196                 return new AAIResultWrapper(json);
197
198         }
199         private Relationship buildRelationship(AAIResourceUri uri) {
200                 final Relationship result = new Relationship();
201                 result.setRelatedLink(uri.build().toString());
202                 return result;
203         }
204         
205         /**
206          * Will automatically create the object if it does not exist
207          * 
208          * @param obj - Optional object which serializes to a valid A&AI payload
209          * @param uri
210          * @return
211          */
212         public AAIResourcesClient createIfNotExists(AAIResourceUri uri, Optional<Object> obj) {
213                 if(!this.exists(uri)){
214                         if (obj.isPresent()) {
215                                 this.create(uri, obj.get());
216                         } else {
217                                 this.createEmpty(uri);
218                         }
219                         
220                 }
221                 return this;
222         }
223
224         /**
225          * Starts a transaction which encloses multiple A&AI mutations
226          * 
227          * @return
228          */
229         public AAITransactionalClient beginTransaction() {
230                 return new AAITransactionalClient(this.version, this.requestId);
231         }
232
233         @Override
234         protected AAIVersion getVersion() {
235                 return this.version;
236         }
237         
238         @Override
239         protected RestClient createClient(AAIUri uri) {
240                 return super.createClient(uri);
241         }
242         
243         private AAIUri addParams(Optional<Depth> depth, boolean nodesOnly, AAIUri uri) {
244                 AAIUri clone = uri.clone();
245                 if (depth.isPresent()) {
246                         clone.depth(depth.get());
247                 }
248                 if (nodesOnly) {
249                         clone.nodesOnly(nodesOnly);
250                 }
251                 
252                 return clone;
253         }
254 }