Fix sonar issue affecting in drools-applications
[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.Serializable;
25 import java.io.StringReader;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.Map;
31 import javax.xml.bind.JAXBContext;
32 import javax.xml.bind.JAXBException;
33 import javax.xml.bind.Unmarshaller;
34 import javax.xml.transform.stream.StreamSource;
35 import org.eclipse.persistence.jaxb.JAXBContextFactory;
36 import org.eclipse.persistence.jaxb.JAXBContextProperties;
37 import org.json.JSONArray;
38 import org.json.JSONObject;
39 import org.onap.aai.domain.yang.CloudRegion;
40 import org.onap.aai.domain.yang.GenericVnf;
41 import org.onap.aai.domain.yang.ModelVer;
42 import org.onap.aai.domain.yang.Relationship;
43 import org.onap.aai.domain.yang.RelationshipData;
44 import org.onap.aai.domain.yang.ServiceInstance;
45 import org.onap.aai.domain.yang.Tenant;
46 import org.onap.aai.domain.yang.VfModule;
47 import org.onap.aai.domain.yang.Vserver;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class AaiCqResponse implements Serializable {
52     private static final long serialVersionUID = 1L;
53     private static final String GENERIC_VNF = "generic-vnf";
54     private static final String VF_MODULE = "vf-module";
55     private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponse.class);
56     private static JAXBContext jaxbContext;
57     private static Unmarshaller unmarshaller;
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             // @formatter:off
67             jaxbContext = JAXBContextFactory.createContext(new Class[] {
68                 Vserver.class,
69                 GenericVnf.class,
70                 VfModule.class,
71                 CloudRegion.class,
72                 ServiceInstance.class,
73                 Tenant.class,
74                 ModelVer.class
75             }, properties);
76             // @formatter:on
77             unmarshaller = jaxbContext.createUnmarshaller();
78         } catch (JAXBException e) {
79             LOGGER.error("Could not initialize JAXBContext", e);
80             LOGGER.info("Problem initiatlizing JAXBContext", e);
81         }
82     }
83
84     @SerializedName("results")
85     private List<Serializable> inventoryResponseItems = new LinkedList<>();
86
87     /**
88      * Constructor creates a custom query response from a valid json string.
89      *
90      * @param jsonString A&AI Custom Query response JSON string
91      */
92     public AaiCqResponse(String jsonString) {
93
94         // Read JSON String and add all AaiObjects
95         JSONObject responseObj = new JSONObject(jsonString);
96         JSONArray resultsArray = new JSONArray();
97         if (responseObj.has("results")) {
98             resultsArray = (JSONArray) responseObj.get("results");
99         }
100         for (int i = 0; i < resultsArray.length(); i++) {
101             // Object is a vserver
102             if (resultsArray.getJSONObject(i).has("vserver")) {
103
104                 // Create the StreamSource by creating StringReader using the
105                 // JSON input
106                 StreamSource json = new StreamSource(
107                         new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
108
109                 // Getting the vserver pojo again from the json
110                 Vserver vserver = this.getAaiObject(json, Vserver.class);
111                 this.inventoryResponseItems.add(vserver);
112             }
113
114             // Object is a Generic VNF
115             if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
116                 // Create the StreamSource by creating StringReader using the
117                 // JSON input
118                 StreamSource json = new StreamSource(
119                         new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
120
121                 // Getting the generic vnf pojo again from the json
122                 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
123
124                 this.inventoryResponseItems.add(genericVnf);
125             }
126
127             // Object is a Service Instance
128             if (resultsArray.getJSONObject(i).has("service-instance")) {
129
130                 // Create the StreamSource by creating StringReader using the
131                 // JSON input
132                 StreamSource json = new StreamSource(
133                         new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
134
135                 // Getting the employee pojo again from the json
136                 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
137
138                 this.inventoryResponseItems.add(serviceInstance);
139             }
140
141             // Object is a VF Module
142             if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
143                 // Create the StreamSource by creating StringReader using the
144                 // JSON input
145                 StreamSource json = new StreamSource(
146                         new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
147
148                 // Getting the vf module pojo again from the json
149                 VfModule vfModule = this.getAaiObject(json, VfModule.class);
150
151                 this.inventoryResponseItems.add(vfModule);
152             }
153
154             // Object is a CloudRegion
155             if (resultsArray.getJSONObject(i).has("cloud-region")) {
156                 // Create the StreamSource by creating StringReader using the
157                 // JSON input
158                 StreamSource json = new StreamSource(
159                         new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
160
161                 // Getting the cloud region pojo again from the json
162                 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
163
164                 this.inventoryResponseItems.add(cloudRegion);
165             }
166
167             // Object is a Tenant
168             if (resultsArray.getJSONObject(i).has("tenant")) {
169                 // Create the StreamSource by creating StringReader using the
170                 // JSON input
171                 StreamSource json = new StreamSource(
172                         new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
173
174                 // Getting the tenant pojo again from the json
175                 Tenant tenant = this.getAaiObject(json, Tenant.class);
176
177                 this.inventoryResponseItems.add(tenant);
178             }
179
180             // Object is a ModelVer
181             if (resultsArray.getJSONObject(i).has("model-ver")) {
182                 // Create the StreamSource by creating StringReader using the
183                 // JSON input
184                 StreamSource json = new StreamSource(
185                         new StringReader(resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
186
187                 // Getting the ModelVer pojo again from the json
188                 ModelVer modelVer = this.getAaiObject(json, ModelVer.class);
189
190                 this.inventoryResponseItems.add(modelVer);
191             }
192
193         }
194
195     }
196
197     private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
198         try {
199             return unmarshaller.unmarshal(json, classOfResponse).getValue();
200         } catch (JAXBException e) {
201             LOGGER.error("JAXBCOntext error", e);
202             return null;
203         }
204     }
205
206     public List<Serializable> getInventoryResponseItems() {
207         return inventoryResponseItems;
208     }
209
210     public void setInventoryResponseItems(List<Serializable> inventoryResponseItems) {
211         this.inventoryResponseItems = inventoryResponseItems;
212     }
213
214     /**
215      * Get list of A&AI objects in the custom query.
216      *
217      * @param classOfResponse Class of the type of A&AI objects to be returned
218      * @return List A&AI objects matching the class
219      */
220     @SuppressWarnings("unchecked")
221     public <T> List<T> getItemListByType(Class<T> classOfResponse) {
222         List<T> returnItemList = new ArrayList<>();
223         for (Serializable i : this.inventoryResponseItems) {
224             if (i.getClass() == classOfResponse) {
225                 returnItemList.add((T) i);
226             }
227         }
228         return returnItemList;
229
230     }
231
232     /**
233      * Get Service Instance.
234      *
235      * @return Service Instance
236      */
237     public ServiceInstance getServiceInstance() {
238         ServiceInstance serviceInstance = null;
239         for (Serializable i : this.inventoryResponseItems) {
240             if (i.getClass() == ServiceInstance.class) {
241                 serviceInstance = (ServiceInstance) i;
242             }
243         }
244         return serviceInstance;
245
246     }
247
248     /**
249      * Get Tenant.
250      *
251      * @return Tenant
252      */
253     public Tenant getDefaultTenant() {
254         Tenant tenant = null;
255         for (Serializable i : this.inventoryResponseItems) {
256             if (i.getClass() == Tenant.class) {
257                 tenant = (Tenant) i;
258             }
259         }
260         return tenant;
261
262     }
263
264     /**
265      * Get Cloud Region.
266      *
267      * @return Cloud Region
268      */
269     public CloudRegion getDefaultCloudRegion() {
270         CloudRegion cloudRegion = null;
271         for (Serializable i : this.inventoryResponseItems) {
272             if (i.getClass() == CloudRegion.class) {
273                 cloudRegion = (CloudRegion) i;
274             }
275         }
276         return cloudRegion;
277
278     }
279
280     /**
281      * Get Generic Vnfs in the custom query.
282      *
283      * @return List of generic Vnf
284      */
285     public List<GenericVnf> getGenericVnfs() {
286         List<GenericVnf> genericVnfList = new ArrayList<>();
287         for (Serializable i : this.inventoryResponseItems) {
288             if (i.getClass() == GenericVnf.class) {
289                 genericVnfList.add((GenericVnf) i);
290             }
291         }
292         return genericVnfList;
293
294     }
295
296     /**
297      * Returns a generic Vnf matching vnf name.
298      *
299      * @param vnfName Name of the vnf to match
300      * @return generic Vnf
301      */
302     public GenericVnf getGenericVnfByVnfName(String vnfName) {
303         List<GenericVnf> genericVnfList = new ArrayList<>();
304         GenericVnf genericVnf = null;
305         for (Serializable i : this.inventoryResponseItems) {
306             if (i.getClass() == GenericVnf.class) {
307                 genericVnfList.add((GenericVnf) i);
308             }
309         }
310
311         for (GenericVnf genVnf : genericVnfList) {
312             if (vnfName.equals(genVnf.getVnfName())) {
313                 genericVnf = genVnf;
314             }
315
316         }
317         return genericVnf;
318
319     }
320
321     /**
322      * Returns a generic Vnf matching model invariant ID.
323      *
324      * @param modelInvariantId Name of the vnf to match
325      * @return generic Vnf
326      */
327     public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
328         List<GenericVnf> genericVnfList = new ArrayList<>();
329         GenericVnf genericVnf = null;
330         for (Serializable i : this.inventoryResponseItems) {
331             if (i.getClass() == GenericVnf.class) {
332                 genericVnfList.add((GenericVnf) i);
333             }
334         }
335
336         for (GenericVnf genVnf : genericVnfList) {
337             if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
338                 genericVnf = genVnf;
339             }
340
341         }
342         return genericVnf;
343
344     }
345
346     /**
347      * Returns a generic Vnf of a given VF Module ID.
348      *
349      * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
350      * @return generic Vnf
351      */
352     public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
353         List<GenericVnf> genericVnfList = this.getGenericVnfs();
354
355         for (GenericVnf genVnf : genericVnfList) {
356             // Iterate through all the vfModules of that generic Vnf
357             for (VfModule vfMod : genVnf.getVfModules().getVfModule()) {
358                 if (vfMod.getModelInvariantId() != null
359                         && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) {
360                     return genVnf;
361                 }
362             }
363         }
364         return null;
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
386         // relationship data
387         for (Relationship r : relations) {
388             // Get the name of generic-vnf related to this server
389             if (GENERIC_VNF.equals(r.getRelatedTo())) {
390                 relationshipData = r.getRelationshipData();
391             }
392         }
393
394         // Iterate through relationship data, and get vnf-id
395         for (RelationshipData rd : relationshipData) {
396             // Get the id of the generic-vnf
397             if ("generic-vnf.vnf-id".equals(rd.getRelationshipKey())) {
398                 genericVnfId = rd.getRelationshipValue();
399             }
400         }
401
402         // Get the list of generic vnfs
403         List<GenericVnf> genericVnfList = this.getGenericVnfs();
404
405         for (GenericVnf genVnf : genericVnfList) {
406             if (genericVnfId.equals(genVnf.getVnfId())) {
407                 genericVnf = genVnf;
408             }
409         }
410
411         return genericVnf;
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
434         // relationship data
435         for (Relationship r : relations) {
436             // Get relationship data of vfmodule related to this server
437             if (VF_MODULE.equals(r.getRelatedTo())) {
438                 relationshipData = r.getRelationshipData();
439             }
440         }
441
442         // Iterate through relationship data, and get vf-module-id
443         for (RelationshipData rd : relationshipData) {
444             // Get the id of the vf-module
445             if ("vf-module.vf-module-id".equals(rd.getRelationshipKey())) {
446                 vfModuleId = rd.getRelationshipValue();
447             }
448         }
449
450         // Get the generic VNF associated with this vserver query
451         genericVnf = this.getDefaultGenericVnf();
452
453         // Get the list of VFmodules associated with this generic Vnf
454         List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
455
456         for (VfModule vfMod : vfModuleList) {
457             if (vfModuleId.equals(vfMod.getVfModuleId())) {
458                 vfModule = vfMod;
459             }
460         }
461
462         return vfModule;
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      * Get Vf Module matching a specific VF model invariant ID.
499      *
500      * @return VfModule
501      */
502     public VfModule getVfModuleByVfModelInvariantId(String vfModelInvariantId) {
503         VfModule vfModule = null;
504
505         for (VfModule vfMod : this.getAllVfModules()) {
506             if (vfMod.getModelInvariantId() != null && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
507                 vfModule = vfMod;
508             }
509
510         }
511         return vfModule;
512     }
513
514     /**
515      * Get verver in the custom query.
516      *
517      * @return Vserver
518      */
519     public Vserver getVserver() {
520         Vserver vserver = null;
521         int index = 0;
522         while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
523             index = index + 1;
524         }
525         vserver = (Vserver) this.inventoryResponseItems.get(index);
526         return vserver;
527
528     }
529
530     /**
531      * Get Model Versions in the custom query.
532      *
533      * @return List of model Versions
534      */
535     public List<ModelVer> getAllModelVer() {
536         List<ModelVer> modelVerList = new ArrayList<>();
537         for (Serializable i : this.inventoryResponseItems) {
538             if (i.getClass() == ModelVer.class) {
539                 modelVerList.add((ModelVer) i);
540             }
541         }
542         return modelVerList;
543     }
544
545     /**
546      * Get ModelVer matching a specific version id.
547      *
548      * @return VfModule
549      */
550     public ModelVer getModelVerByVersionId(String versionId) {
551         ModelVer modelVer = null;
552
553         for (ModelVer modVersion : this.getAllModelVer()) {
554             if (versionId.equals(modVersion.getModelVersionId())) {
555                 modelVer = modVersion;
556             }
557
558         }
559         return modelVer;
560     }
561
562     /**
563      * Get the count of vfModules matching customizationId, InvariantId and VersionId.
564      *
565      * @param custId ModelCustomizationId
566      * @param invId ModelInvariantId
567      * @param verId ModelVersionId
568      * @return Returns the count of vf modules
569      */
570     public int getVfModuleCount(String custId, String invId, String verId) {
571         List<VfModule> vfModuleList = this.getAllVfModules();
572         int count = 0;
573         for (VfModule vfModule : vfModuleList) {
574             if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
575                     || vfModule.getModelVersionId() == null) {
576                 continue;
577             }
578
579             if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
580                     && vfModule.getModelVersionId().equals(verId)) {
581                 count = count + 1;
582             }
583         }
584         return count;
585     }
586
587 }