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