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