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