Custom Query Code
[policy/models.git] / models-interactions / model-impl / aai / src / main / java / org / onap / policy / aai / AaiCqResponse.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *
4  * ================================================================================
5  * Copyright (C) 2019 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.policy.aai;
22
23 import com.google.gson.annotations.SerializedName;
24 import java.io.StringReader;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Map;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.bind.Unmarshaller;
33 import javax.xml.transform.stream.StreamSource;
34 import org.eclipse.persistence.jaxb.JAXBContextFactory;
35 import org.eclipse.persistence.jaxb.JAXBContextProperties;
36 import org.json.JSONArray;
37 import org.json.JSONObject;
38 import org.onap.aai.domain.yang.CloudRegion;
39 import org.onap.aai.domain.yang.GenericVnf;
40 import org.onap.aai.domain.yang.Relationship;
41 import org.onap.aai.domain.yang.RelationshipData;
42 import org.onap.aai.domain.yang.ServiceInstance;
43 import org.onap.aai.domain.yang.Tenant;
44 import org.onap.aai.domain.yang.VfModule;
45 import org.onap.aai.domain.yang.Vserver;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class AaiCqResponse {
50
51     private static final String GENERIC_VNF = "generic-vnf";
52     private static final String VF_MODULE = "vf-module";
53     private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponse.class);
54     private static JAXBContext jaxbContext;
55     private static Unmarshaller unmarshaller;
56
57
58     // JABX initial stuff
59     static {
60         Map<String, Object> properties = new HashMap<>();
61         properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
62         properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
63         // Define JAXB context
64         try {
65             jaxbContext = JAXBContextFactory.createContext(new Class[] {Vserver.class, GenericVnf.class, VfModule.class,
66                 CloudRegion.class, ServiceInstance.class, Tenant.class}, properties);
67             unmarshaller = jaxbContext.createUnmarshaller();
68         } catch (JAXBException e) {
69             LOGGER.error("Could not initialize JAXBContext", e);
70             LOGGER.info("Problem initiatlizing JAXBContext", e);
71         }
72     }
73
74     @SerializedName("results")
75     private List<Object> inventoryResponseItems = new LinkedList<>();
76
77
78     /**
79      * Constructor creates a custom query response from a valid json string.
80      *
81      * @param jsonString A&AI Custom Query response JSON string
82      */
83     public AaiCqResponse(String jsonString) {
84
85         // Read JSON String and add all AaiObjects
86         JSONObject responseObj = new JSONObject(jsonString);
87         JSONArray resultsArray = new JSONArray();
88         if (responseObj.has("results")) {
89             resultsArray = (JSONArray) responseObj.get("results");
90         }
91         for (int i = 0; i < resultsArray.length(); i++) {
92             // Object is a vserver
93             if (resultsArray.getJSONObject(i).has("vserver")) {
94
95                 // Create the StreamSource by creating StringReader using the
96                 // JSON input
97                 StreamSource json = new StreamSource(
98                         new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
99
100                 // Getting the vserver pojo again from the json
101                 Vserver vserver = this.getAaiObject(json, Vserver.class);
102                 this.inventoryResponseItems.add(vserver);
103             }
104
105             // Object is a Generic VNF
106             if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
107                 // Create the StreamSource by creating StringReader using the
108                 // JSON input
109                 StreamSource json = new StreamSource(
110                         new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
111
112                 // Getting the generic vnf pojo again from the json
113                 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
114
115                 this.inventoryResponseItems.add(genericVnf);
116             }
117
118             // Object is a Service Instance
119             if (resultsArray.getJSONObject(i).has("service-instance")) {
120
121                 // Create the StreamSource by creating StringReader using the
122                 // JSON input
123                 StreamSource json = new StreamSource(
124                         new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
125
126                 // Getting the employee pojo again from the json
127                 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
128
129                 this.inventoryResponseItems.add(serviceInstance);
130             }
131
132             // Object is a VF Module
133             if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
134                 // Create the StreamSource by creating StringReader using the
135                 // JSON input
136                 StreamSource json = new StreamSource(
137                         new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
138
139                 // Getting the vf module pojo again from the json
140                 VfModule vfModule = this.getAaiObject(json, VfModule.class);
141
142                 this.inventoryResponseItems.add(vfModule);
143             }
144
145             // Object is a CloudRegion
146             if (resultsArray.getJSONObject(i).has("cloud-region")) {
147                 // Create the StreamSource by creating StringReader using the
148                 // JSON input
149                 StreamSource json = new StreamSource(
150                         new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
151
152                 // Getting the cloud region pojo again from the json
153                 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
154
155                 this.inventoryResponseItems.add(cloudRegion);
156             }
157
158             // Object is a Tenant
159             if (resultsArray.getJSONObject(i).has("tenant")) {
160                 // Create the StreamSource by creating StringReader using the
161                 // JSON input
162                 StreamSource json = new StreamSource(
163                         new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
164
165                 // Getting the tenant pojo again from the json
166                 Tenant tenant = this.getAaiObject(json, Tenant.class);
167
168                 this.inventoryResponseItems.add(tenant);
169             }
170
171         }
172
173
174
175     }
176
177
178
179     private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
180         try {
181             return unmarshaller.unmarshal(json, classOfResponse).getValue();
182         } catch (JAXBException e) {
183             LOGGER.error("JAXBCOntext error", e);
184             return null;
185         }
186     }
187
188     public List<Object> getInventoryResponseItems() {
189         return inventoryResponseItems;
190     }
191
192     public void setInventoryResponseItems(List<Object> inventoryResponseItems) {
193         this.inventoryResponseItems = inventoryResponseItems;
194     }
195
196     /**
197      * Get list of A&AI objects in the custom query.
198      *
199      * @param classOfResponse Class of the type of A&AI objects to be returned
200      * @return List A&AI objects matching the class
201      */
202     @SuppressWarnings("unchecked")
203     public <T> List<T> getItemListByType(Class<T> classOfResponse) {
204         List<T> returnItemList = new ArrayList<>();
205         for (Object i : this.inventoryResponseItems) {
206             if (i.getClass() == classOfResponse) {
207                 returnItemList.add((T) i);
208             }
209         }
210         return returnItemList;
211
212     }
213
214     /**
215      * Get Service Instance.
216      *
217      * @return Service Instance
218      */
219     public ServiceInstance getServiceInstance() {
220         ServiceInstance serviceInstance = null;
221         for (Object i : this.inventoryResponseItems) {
222             if (i.getClass() == ServiceInstance.class) {
223                 serviceInstance = (ServiceInstance) i;
224             }
225         }
226         return serviceInstance;
227
228     }
229
230     /**
231      * Get Tenant.
232      *
233      * @return Tenant
234      */
235     public Tenant getDefaultTenant() {
236         Tenant tenant = null;
237         for (Object i : this.inventoryResponseItems) {
238             if (i.getClass() == Tenant.class) {
239                 tenant = (Tenant) i;
240             }
241         }
242         return tenant;
243
244     }
245
246     /**
247      * Get Cloud Region.
248      *
249      * @return Cloud Region
250      */
251     public CloudRegion getDefaultCloudRegion() {
252         CloudRegion cloudRegion = null;
253         for (Object i : this.inventoryResponseItems) {
254             if (i.getClass() == CloudRegion.class) {
255                 cloudRegion = (CloudRegion) i;
256             }
257         }
258         return cloudRegion;
259
260     }
261
262     /**
263      * Get Generic Vnfs in the custom query.
264      *
265      * @return List of generic Vnf
266      */
267     public List<GenericVnf> getGenericVnfs() {
268         List<GenericVnf> genericVnfList = new ArrayList<>();
269         for (Object i : this.inventoryResponseItems) {
270             if (i.getClass() == GenericVnf.class) {
271                 genericVnfList.add((GenericVnf) i);
272             }
273         }
274         return genericVnfList;
275
276     }
277
278
279     /**
280      * Returns a generic Vnf matching vnf name.
281      *
282      * @param vnfName Name of the vnf to match
283      * @return generic Vnf
284      */
285     public GenericVnf getGenericVnfByVnfName(String vnfName) {
286         List<GenericVnf> genericVnfList = new ArrayList<>();
287         GenericVnf genericVnf = null;
288         for (Object i : this.inventoryResponseItems) {
289             if (i.getClass() == GenericVnf.class) {
290                 genericVnfList.add((GenericVnf) i);
291             }
292         }
293
294         for (GenericVnf genVnf : genericVnfList) {
295             if (vnfName.equals(genVnf.getVnfName())) {
296                 genericVnf = genVnf;
297             }
298
299         }
300         return genericVnf;
301
302     }
303
304     /**
305      * Returns a generic Vnf matching model invariant ID.
306      *
307      * @param modelInvariantId Name of the vnf to match
308      * @return generic Vnf
309      */
310     public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
311         List<GenericVnf> genericVnfList = new ArrayList<>();
312         GenericVnf genericVnf = null;
313         for (Object i : this.inventoryResponseItems) {
314             if (i.getClass() == GenericVnf.class) {
315                 genericVnfList.add((GenericVnf) i);
316             }
317         }
318
319         for (GenericVnf genVnf : genericVnfList) {
320             if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
321                 genericVnf = genVnf;
322             }
323
324         }
325         return genericVnf;
326
327     }
328
329
330
331     /**
332      * Get the generic vnf associated with the vserver in the custom query.
333      *
334      * @return Generic VNF
335      */
336     public GenericVnf getDefaultGenericVnf() {
337         GenericVnf genericVnf = null;
338
339         // Get the vserver associated with the query
340         Vserver vserver = this.getVserver();
341
342         // Get the relationships of the vserver
343         List<Relationship> relations = vserver.getRelationshipList().getRelationship();
344
345         // Find the relationship of the genericVNF
346         String genericVnfId = "";
347         List<RelationshipData> relationshipData = null;
348
349         // Iterate through the list of relationships and get generic vnf relationship data
350         for (Relationship r : relations) {
351             // Get the name of generic-vnf related to this server
352             if (GENERIC_VNF.equals(r.getRelatedTo())) {
353                 relationshipData = r.getRelationshipData();
354             }
355         }
356
357         // Iterate through relationship data, and get vnf-id
358         for (RelationshipData rd : relationshipData) {
359             // Get the id of the generic-vnf
360             if ("generic-vnf.vnf-id".equals(rd.getRelationshipKey())) {
361                 genericVnfId = rd.getRelationshipValue();
362             }
363         }
364
365         // Get the list of generic vnfs
366         List<GenericVnf> genericVnfList = this.getGenericVnfs();
367
368         for (GenericVnf genVnf : genericVnfList) {
369             if (genericVnfId.equals(genVnf.getVnfId())) {
370                 genericVnf = genVnf;
371             }
372         }
373
374         return genericVnf;
375     }
376
377
378     /**
379      * Get Vf Module associated with the vserver in the custom query.
380      *
381      * @return Vf Module
382      */
383     public VfModule getDefaultVfModule() {
384         GenericVnf genericVnf = null;
385         VfModule vfModule = null;
386
387         // Get the vserver associated with the query
388         Vserver vserver = this.getVserver();
389
390         // Get the relationships of the vserver
391         List<Relationship> relations = vserver.getRelationshipList().getRelationship();
392
393         // Find the relationship of VfModule
394         String vfModuleId = "";
395         List<RelationshipData> relationshipData = null;
396
397         // Iterate through the list of relationships and get vf module relationship data
398         for (Relationship r : relations) {
399             // Get relationship data of vfmodule related to this server
400             if (VF_MODULE.equals(r.getRelatedTo())) {
401                 relationshipData = r.getRelationshipData();
402             }
403         }
404
405         // Iterate through relationship data, and get vf-module-id
406         for (RelationshipData rd : relationshipData) {
407             // Get the id of the vf-module
408             if ("vf-module.vf-module-id".equals(rd.getRelationshipKey())) {
409                 vfModuleId = rd.getRelationshipValue();
410             }
411         }
412
413         // Get the generic VNF associated with this vserver query
414         genericVnf = this.getDefaultGenericVnf();
415
416         // Get the list of VFmodules associated with this generic Vnf
417         List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
418
419         for (VfModule vfMod : vfModuleList) {
420             if (vfModuleId.equals(vfMod.getVfModuleId())) {
421                 vfModule = vfMod;
422             }
423         }
424
425         return vfModule;
426     }
427
428
429     /**
430      * Get vf modules in the custom query.
431      *
432      * @return List of VfModule
433      */
434     public List<VfModule> getAllVfModules() {
435         List<VfModule> vfModuleList = new ArrayList<>();
436
437         for (GenericVnf genVnf : this.getGenericVnfs()) {
438             vfModuleList.addAll(genVnf.getVfModules().getVfModule());
439         }
440         return vfModuleList;
441
442     }
443
444     /**
445      * Get Vf Module matching a specific VF module name.
446      *
447      * @return VfModule
448      */
449     public VfModule getVfModuleByVfModuleName(String vfModuleName) {
450         VfModule vfModule = null;
451
452         for (VfModule vfMod : this.getAllVfModules()) {
453             if (vfModuleName.equals(vfMod.getVfModuleName())) {
454                 vfModule = vfMod;
455             }
456
457         }
458         return vfModule;
459     }
460
461     /**
462      * Get verver in the custom query.
463      *
464      * @return Vserver
465      */
466     public Vserver getVserver() {
467         Vserver vserver = null;
468         int index = 0;
469         while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
470             index = index + 1;
471         }
472         vserver = (Vserver) this.inventoryResponseItems.get(index);
473         return vserver;
474
475     }
476
477 }
478