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