Remove actor and recipe checks from ControlLoopCompiler.java
[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             jaxbContext =
66                 JAXBContextFactory
67                     .createContext(
68                         new Class[] {Vserver.class, GenericVnf.class, VfModule.class,
69                             CloudRegion.class, ServiceInstance.class, Tenant.class, ModelVer.class},
70                         properties);
71             unmarshaller = jaxbContext.createUnmarshaller();
72         } catch (JAXBException e) {
73             LOGGER.error("Could not initialize JAXBContext", e);
74             LOGGER.info("Problem initiatlizing JAXBContext", e);
75         }
76     }
77
78     @SerializedName("results")
79     private List<Object> inventoryResponseItems = new LinkedList<>();
80
81     /**
82      * Constructor creates a custom query response from a valid json string.
83      *
84      * @param jsonString
85      *        A&AI Custom Query response JSON string
86      */
87     public AaiCqResponse(String jsonString) {
88
89         // Read JSON String and add all AaiObjects
90         JSONObject responseObj = new JSONObject(jsonString);
91         JSONArray resultsArray = new JSONArray();
92         if (responseObj.has("results")) {
93             resultsArray = (JSONArray) responseObj.get("results");
94         }
95         for (int i = 0; i < resultsArray.length(); i++) {
96             // Object is a vserver
97             if (resultsArray.getJSONObject(i).has("vserver")) {
98
99                 // Create the StreamSource by creating StringReader using the
100                 // JSON input
101                 StreamSource json = new StreamSource(new StringReader(
102                     resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
103
104                 // Getting the vserver pojo again from the json
105                 Vserver vserver = this.getAaiObject(json, Vserver.class);
106                 this.inventoryResponseItems.add(vserver);
107             }
108
109             // Object is a Generic VNF
110             if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
111                 // Create the StreamSource by creating StringReader using the
112                 // JSON input
113                 StreamSource json = new StreamSource(new StringReader(
114                     resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
115
116                 // Getting the generic vnf pojo again from the json
117                 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
118
119                 this.inventoryResponseItems.add(genericVnf);
120             }
121
122             // Object is a Service Instance
123             if (resultsArray.getJSONObject(i).has("service-instance")) {
124
125                 // Create the StreamSource by creating StringReader using the
126                 // JSON input
127                 StreamSource json = new StreamSource(new StringReader(
128                     resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
129
130                 // Getting the employee pojo again from the json
131                 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
132
133                 this.inventoryResponseItems.add(serviceInstance);
134             }
135
136             // Object is a VF Module
137             if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
138                 // Create the StreamSource by creating StringReader using the
139                 // JSON input
140                 StreamSource json = new StreamSource(new StringReader(
141                     resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
142
143                 // Getting the vf module pojo again from the json
144                 VfModule vfModule = this.getAaiObject(json, VfModule.class);
145
146                 this.inventoryResponseItems.add(vfModule);
147             }
148
149             // Object is a CloudRegion
150             if (resultsArray.getJSONObject(i).has("cloud-region")) {
151                 // Create the StreamSource by creating StringReader using the
152                 // JSON input
153                 StreamSource json = new StreamSource(new StringReader(
154                     resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
155
156                 // Getting the cloud region pojo again from the json
157                 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
158
159                 this.inventoryResponseItems.add(cloudRegion);
160             }
161
162             // Object is a Tenant
163             if (resultsArray.getJSONObject(i).has("tenant")) {
164                 // Create the StreamSource by creating StringReader using the
165                 // JSON input
166                 StreamSource json = new StreamSource(new StringReader(
167                     resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
168
169                 // Getting the tenant pojo again from the json
170                 Tenant tenant = this.getAaiObject(json, Tenant.class);
171
172                 this.inventoryResponseItems.add(tenant);
173             }
174
175             // Object is a ModelVer
176             if (resultsArray.getJSONObject(i).has("model-ver")) {
177                 // Create the StreamSource by creating StringReader using the
178                 // JSON input
179                 StreamSource json = new StreamSource(new StringReader(
180                     resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
181
182                 // Getting the ModelVer pojo again from the json
183                 ModelVer modelVer = this.getAaiObject(json, ModelVer.class);
184
185                 this.inventoryResponseItems.add(modelVer);
186             }
187
188         }
189
190     }
191
192     private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
193         try {
194             return unmarshaller.unmarshal(json, classOfResponse).getValue();
195         } catch (JAXBException e) {
196             LOGGER.error("JAXBCOntext error", e);
197             return null;
198         }
199     }
200
201     public List<Object> getInventoryResponseItems() {
202         return inventoryResponseItems;
203     }
204
205     public void setInventoryResponseItems(List<Object> inventoryResponseItems) {
206         this.inventoryResponseItems = inventoryResponseItems;
207     }
208
209     /**
210      * Get list of A&AI objects in the custom query.
211      *
212      * @param classOfResponse
213      *        Class of the type of A&AI objects to be returned
214      * @return List A&AI objects matching the class
215      */
216     @SuppressWarnings("unchecked")
217     public <T> List<T> getItemListByType(Class<T> classOfResponse) {
218         List<T> returnItemList = new ArrayList<>();
219         for (Object i : this.inventoryResponseItems) {
220             if (i.getClass() == classOfResponse) {
221                 returnItemList.add((T) i);
222             }
223         }
224         return returnItemList;
225
226     }
227
228     /**
229      * Get Service Instance.
230      *
231      * @return Service Instance
232      */
233     public ServiceInstance getServiceInstance() {
234         ServiceInstance serviceInstance = null;
235         for (Object i : this.inventoryResponseItems) {
236             if (i.getClass() == ServiceInstance.class) {
237                 serviceInstance = (ServiceInstance) i;
238             }
239         }
240         return serviceInstance;
241
242     }
243
244     /**
245      * Get Tenant.
246      *
247      * @return Tenant
248      */
249     public Tenant getDefaultTenant() {
250         Tenant tenant = null;
251         for (Object i : this.inventoryResponseItems) {
252             if (i.getClass() == Tenant.class) {
253                 tenant = (Tenant) i;
254             }
255         }
256         return tenant;
257
258     }
259
260     /**
261      * Get Cloud Region.
262      *
263      * @return Cloud Region
264      */
265     public CloudRegion getDefaultCloudRegion() {
266         CloudRegion cloudRegion = null;
267         for (Object i : this.inventoryResponseItems) {
268             if (i.getClass() == CloudRegion.class) {
269                 cloudRegion = (CloudRegion) i;
270             }
271         }
272         return cloudRegion;
273
274     }
275
276     /**
277      * Get Generic Vnfs in the custom query.
278      *
279      * @return List of generic Vnf
280      */
281     public List<GenericVnf> getGenericVnfs() {
282         List<GenericVnf> genericVnfList = new ArrayList<>();
283         for (Object i : this.inventoryResponseItems) {
284             if (i.getClass() == GenericVnf.class) {
285                 genericVnfList.add((GenericVnf) i);
286             }
287         }
288         return genericVnfList;
289
290     }
291
292     /**
293      * Returns a generic Vnf matching vnf name.
294      *
295      * @param vnfName
296      *        Name of the vnf to match
297      * @return generic Vnf
298      */
299     public GenericVnf getGenericVnfByVnfName(String vnfName) {
300         List<GenericVnf> genericVnfList = new ArrayList<>();
301         GenericVnf genericVnf = null;
302         for (Object i : this.inventoryResponseItems) {
303             if (i.getClass() == GenericVnf.class) {
304                 genericVnfList.add((GenericVnf) i);
305             }
306         }
307
308         for (GenericVnf genVnf : genericVnfList) {
309             if (vnfName.equals(genVnf.getVnfName())) {
310                 genericVnf = genVnf;
311             }
312
313         }
314         return genericVnf;
315
316     }
317
318     /**
319      * Returns a generic Vnf matching model invariant ID.
320      *
321      * @param modelInvariantId
322      *        Name of the vnf to match
323      * @return generic Vnf
324      */
325     public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
326         List<GenericVnf> genericVnfList = new ArrayList<>();
327         GenericVnf genericVnf = null;
328         for (Object i : this.inventoryResponseItems) {
329             if (i.getClass() == GenericVnf.class) {
330                 genericVnfList.add((GenericVnf) i);
331             }
332         }
333
334         for (GenericVnf genVnf : genericVnfList) {
335             if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
336                 genericVnf = genVnf;
337             }
338
339         }
340         return genericVnf;
341
342     }
343
344     /**
345      * Returns a generic Vnf of a given VF Module ID.
346      *
347      * @param vfModuleModelInvariantId
348      *        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
506                 && 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 (Object 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
564      * VersionId.
565      *
566      * @param custId
567      *        ModelCustomizationId
568      * @param invId
569      *        ModelInvariantId
570      * @param verId
571      *        ModelVersionId
572      * @return Returns the count of vf modules
573      */
574     public int getVfModuleCount(String custId, String invId, String verId) {
575         List<VfModule> vfModuleList = this.getAllVfModules();
576         int count = 0;
577         for (VfModule vfModule : vfModuleList) {
578             if (vfModule.getModelCustomizationId() != null && vfModule.getModelInvariantId() != null
579                 && vfModule.getModelVersionId() != null) {
580                 if (vfModule.getModelCustomizationId().equals(custId)
581                     && vfModule.getModelInvariantId().equals(invId)
582                     && vfModule.getModelVersionId().equals(verId)) {
583                     count = count + 1;
584                 }
585             }
586         }
587         return count;
588     }
589
590 }