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