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