Replace Eclipselink with Hibernate
[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  * Modifications Copyright (C) 2023 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.aai;
23
24 import com.google.gson.Gson;
25 import com.google.gson.GsonBuilder;
26 import com.google.gson.annotations.SerializedName;
27 import java.io.Serializable;
28 import java.util.ArrayList;
29 import java.util.LinkedList;
30 import java.util.List;
31 import org.json.JSONArray;
32 import org.json.JSONObject;
33 import org.onap.aai.domain.yang.CloudRegion;
34 import org.onap.aai.domain.yang.GenericVnf;
35 import org.onap.aai.domain.yang.ModelVer;
36 import org.onap.aai.domain.yang.Relationship;
37 import org.onap.aai.domain.yang.RelationshipData;
38 import org.onap.aai.domain.yang.ServiceInstance;
39 import org.onap.aai.domain.yang.Tenant;
40 import org.onap.aai.domain.yang.VfModule;
41 import org.onap.aai.domain.yang.Vserver;
42
43 public class AaiCqResponse implements Serializable {
44     private static final long serialVersionUID = 1L;
45     public static final String CONTEXT_KEY = AaiConstants.CONTEXT_PREFIX + "AaiCqResponse";
46     public static final String OPERATION = "CustomQuery";
47     private static final String GENERIC_VNF = "generic-vnf";
48     private static final String VF_MODULE = "vf-module";
49
50     @SerializedName("results")
51     private List<Serializable> inventoryResponseItems = new LinkedList<>();
52
53     private final Gson gson;
54
55     /**
56      * Constructor creates a custom query response from a valid json string.
57      *
58      * @param jsonString A&AI Custom Query response JSON string
59      */
60     public AaiCqResponse(String jsonString) {
61         gson = new GsonBuilder()
62             .setFieldNamingStrategy(new XmlElementFieldNamingStrategy())
63             .create();
64
65         // Read JSON String and add all AaiObjects
66         var responseObj = new JSONObject(jsonString);
67         var resultsArray = new JSONArray();
68         if (responseObj.has("results")) {
69             resultsArray = (JSONArray) responseObj.get("results");
70         }
71         for (var i = 0; i < resultsArray.length(); i++) {
72             final var resultObject = resultsArray.getJSONObject(i);
73
74             extractVserver(resultObject);
75             extractGenericVnf(resultObject);
76             extractServiceInstance(resultObject);
77             extractVfModule(resultObject);
78             extractCloudRegion(resultObject);
79             extractTenant(resultObject);
80             extractModelVer(resultObject);
81         }
82     }
83
84     private void extractVserver(final JSONObject resultObject) {
85         if (resultObject.has("vserver")) {
86
87             // Create the StreamSource by creating StringReader using the
88             // JSON input
89             var json = resultObject.getJSONObject("vserver").toString();
90
91             // Getting the vserver pojo again from the json
92             var vserver = gson.fromJson(json, Vserver.class);
93             this.inventoryResponseItems.add(vserver);
94         }
95     }
96
97     private void extractGenericVnf(final JSONObject resultObject) {
98         if (resultObject.has(GENERIC_VNF)) {
99             // Create the StreamSource by creating StringReader using the
100             // JSON input
101             var json = resultObject.getJSONObject(GENERIC_VNF).toString();
102
103             // Getting the generic vnf pojo again from the json
104             var genericVnf = gson.fromJson(json, GenericVnf.class);
105             this.inventoryResponseItems.add(genericVnf);
106         }
107     }
108
109     private void extractServiceInstance(final JSONObject resultObject) {
110         if (resultObject.has("service-instance")) {
111
112             // Create the StreamSource by creating StringReader using the
113             // JSON input
114             var json = resultObject.getJSONObject("service-instance").toString();
115
116             // Getting the employee pojo again from the json
117             var serviceInstance = gson.fromJson(json, ServiceInstance.class);
118             this.inventoryResponseItems.add(serviceInstance);
119         }
120     }
121
122     private void extractVfModule(final JSONObject resultObject) {
123         if (resultObject.has(VF_MODULE)) {
124             // Create the StreamSource by creating StringReader using the
125             // JSON input
126             var json = resultObject.getJSONObject(VF_MODULE).toString();
127
128             // Getting the vf module pojo again from the json
129             var vfModule = gson.fromJson(json, VfModule.class);
130             this.inventoryResponseItems.add(vfModule);
131         }
132     }
133
134     private void extractCloudRegion(final JSONObject resultObject) {
135         if (resultObject.has("cloud-region")) {
136             // Create the StreamSource by creating StringReader using the
137             // JSON input
138             var json = resultObject.getJSONObject("cloud-region").toString();
139
140             // Getting the cloud region pojo again from the json
141             var cloudRegion = gson.fromJson(json, CloudRegion.class);
142             this.inventoryResponseItems.add(cloudRegion);
143         }
144     }
145
146     private void extractTenant(final JSONObject resultObject) {
147         if (resultObject.has("tenant")) {
148             // Create the StreamSource by creating StringReader using the
149             // JSON input
150             var json = resultObject.getJSONObject("tenant").toString();
151
152             // Getting the tenant pojo again from the json
153             var tenant = gson.fromJson(json, Tenant.class);
154             this.inventoryResponseItems.add(tenant);
155         }
156     }
157
158     private void extractModelVer(final JSONObject resultObject) {
159         if (resultObject.has("model-ver")) {
160             // Create the StreamSource by creating StringReader using the
161             // JSON input
162             var json = resultObject.getJSONObject("model-ver").toString();
163
164             // Getting the ModelVer pojo again from the json
165             var modelVer = gson.fromJson(json, ModelVer.class);
166             this.inventoryResponseItems.add(modelVer);
167         }
168     }
169
170     public List<Serializable> getInventoryResponseItems() {
171         return inventoryResponseItems;
172     }
173
174     public void setInventoryResponseItems(List<Serializable> inventoryResponseItems) {
175         this.inventoryResponseItems = inventoryResponseItems;
176     }
177
178     /**
179      * Get list of A&AI objects in the custom query.
180      *
181      * @param classOfResponse Class of the type of A&AI objects to be returned
182      * @return List A&AI objects matching the class
183      */
184     @SuppressWarnings("unchecked")
185     public <T> List<T> getItemListByType(Class<T> classOfResponse) {
186         List<T> returnItemList = new ArrayList<>();
187         for (Serializable i : this.inventoryResponseItems) {
188             if (i.getClass() == classOfResponse) {
189                 returnItemList.add((T) i);
190             }
191         }
192         return returnItemList;
193
194     }
195
196     /**
197      * Get Service Instance.
198      *
199      * @return Service Instance
200      */
201     public ServiceInstance getServiceInstance() {
202         ServiceInstance serviceInstance = null;
203         for (Serializable i : this.inventoryResponseItems) {
204             if (i.getClass() == ServiceInstance.class) {
205                 serviceInstance = (ServiceInstance) i;
206             }
207         }
208         return serviceInstance;
209
210     }
211
212     /**
213      * Get Tenant.
214      *
215      * @return Tenant
216      */
217     public Tenant getDefaultTenant() {
218         Tenant tenant = null;
219         for (Serializable i : this.inventoryResponseItems) {
220             if (i.getClass() == Tenant.class) {
221                 tenant = (Tenant) i;
222             }
223         }
224         return tenant;
225
226     }
227
228     /**
229      * Get Cloud Region.
230      *
231      * @return Cloud Region
232      */
233     public CloudRegion getDefaultCloudRegion() {
234         CloudRegion cloudRegion = null;
235         for (Serializable i : this.inventoryResponseItems) {
236             if (i.getClass() == CloudRegion.class) {
237                 cloudRegion = (CloudRegion) i;
238             }
239         }
240         return cloudRegion;
241
242     }
243
244     /**
245      * Get Generic Vnfs in the custom query.
246      *
247      * @return List of generic Vnf
248      */
249     public List<GenericVnf> getGenericVnfs() {
250         List<GenericVnf> genericVnfList = new ArrayList<>();
251         for (Serializable i : this.inventoryResponseItems) {
252             if (i.getClass() == GenericVnf.class) {
253                 genericVnfList.add((GenericVnf) i);
254             }
255         }
256         return genericVnfList;
257
258     }
259
260     /**
261      * Returns a generic Vnf matching vnf name.
262      *
263      * @param vnfName Name of the vnf to match
264      * @return generic Vnf
265      */
266     public GenericVnf getGenericVnfByVnfName(String vnfName) {
267         List<GenericVnf> genericVnfList = new ArrayList<>();
268         GenericVnf genericVnf = null;
269         for (Serializable i : this.inventoryResponseItems) {
270             if (i.getClass() == GenericVnf.class) {
271                 genericVnfList.add((GenericVnf) i);
272             }
273         }
274
275         for (GenericVnf genVnf : genericVnfList) {
276             if (vnfName.equals(genVnf.getVnfName())) {
277                 genericVnf = genVnf;
278             }
279
280         }
281         return genericVnf;
282
283     }
284
285     /**
286      * Returns a generic Vnf matching model invariant ID.
287      *
288      * @param modelInvariantId Name of the vnf to match
289      * @return generic Vnf
290      */
291     public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
292         List<GenericVnf> genericVnfList = new ArrayList<>();
293         GenericVnf genericVnf = null;
294         for (Serializable i : this.inventoryResponseItems) {
295             if (i.getClass() == GenericVnf.class) {
296                 genericVnfList.add((GenericVnf) i);
297             }
298         }
299
300         for (GenericVnf genVnf : genericVnfList) {
301             if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
302                 genericVnf = genVnf;
303             }
304
305         }
306         return genericVnf;
307
308     }
309
310     /**
311      * Returns a generic Vnf of a given VF Module ID.
312      *
313      * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
314      * @return generic Vnf
315      */
316     public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
317         List<GenericVnf> genericVnfList = this.getGenericVnfs();
318
319         for (GenericVnf genVnf : genericVnfList) {
320             // Iterate through all the vfModules of that generic Vnf
321             for (VfModule vfMod : genVnf.getVfModules().getVfModule()) {
322                 if (vfMod.getModelInvariantId() != null
323                     && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) {
324                     return genVnf;
325                 }
326             }
327         }
328         return null;
329     }
330
331     /**
332      * Returns the VNF given the vnf-id.
333      *
334      * @param vnfId The vnf-id
335      * @return generic Vnf
336      */
337     public GenericVnf getGenericVnfByVnfId(String vnfId) {
338         List<GenericVnf> genericVnfList = this.getGenericVnfs();
339
340         for (GenericVnf genVnf : genericVnfList) {
341             if (vnfId.equals(genVnf.getVnfId())) {
342                 return genVnf;
343             }
344         }
345
346         return null;
347     }
348
349     /**
350      * Get the generic vnf associated with the vserver in the custom query.
351      *
352      * @return Generic VNF
353      */
354     public GenericVnf getDefaultGenericVnf() {
355         GenericVnf genericVnf = null;
356
357         // Get the vserver associated with the query
358         var vserver = this.getVserver();
359
360         // Get the relationships of the vserver
361         List<Relationship> relations = vserver.getRelationshipList().getRelationship();
362
363         // Find the relationship of the genericVNF
364         var genericVnfId = "";
365         List<RelationshipData> relationshipData = null;
366
367         // Iterate through the list of relationships and get generic vnf
368         // relationship data
369         for (Relationship r : relations) {
370             // Get the name of generic-vnf related to this server
371             if (GENERIC_VNF.equals(r.getRelatedTo())) {
372                 relationshipData = r.getRelationshipData();
373             }
374         }
375
376         // Iterate through relationship data, and get vnf-id
377         for (RelationshipData rd : relationshipData) {
378             // Get the id of the generic-vnf
379             if ("generic-vnf.vnf-id".equals(rd.getRelationshipKey())) {
380                 genericVnfId = rd.getRelationshipValue();
381             }
382         }
383
384         // Get the list of generic vnfs
385         List<GenericVnf> genericVnfList = this.getGenericVnfs();
386
387         for (GenericVnf genVnf : genericVnfList) {
388             if (genericVnfId.equals(genVnf.getVnfId())) {
389                 genericVnf = genVnf;
390             }
391         }
392
393         return genericVnf;
394     }
395
396     /**
397      * Get Vf Module associated with the vserver in the custom query.
398      *
399      * @return Vf Module
400      */
401     public VfModule getDefaultVfModule() {
402         GenericVnf genericVnf = null;
403         VfModule vfModule = null;
404
405         // Get the vserver associated with the query
406         var vserver = this.getVserver();
407
408         // Get the relationships of the vserver
409         List<Relationship> relations = vserver.getRelationshipList().getRelationship();
410
411         // Find the relationship of VfModule
412         var vfModuleId = "";
413         List<RelationshipData> relationshipData = null;
414
415         // Iterate through the list of relationships and get vf module
416         // relationship data
417         for (Relationship r : relations) {
418             // Get relationship data of vfmodule related to this server
419             if (VF_MODULE.equals(r.getRelatedTo())) {
420                 relationshipData = r.getRelationshipData();
421             }
422         }
423
424         // Iterate through relationship data, and get vf-module-id
425         for (RelationshipData rd : relationshipData) {
426             // Get the id of the vf-module
427             if ("vf-module.vf-module-id".equals(rd.getRelationshipKey())) {
428                 vfModuleId = rd.getRelationshipValue();
429             }
430         }
431
432         // Get the generic VNF associated with this vserver query
433         genericVnf = this.getDefaultGenericVnf();
434
435         // Get the list of VFmodules associated with this generic Vnf
436         List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
437
438         for (VfModule vfMod : vfModuleList) {
439             if (vfModuleId.equals(vfMod.getVfModuleId())) {
440                 vfModule = vfMod;
441             }
442         }
443
444         return vfModule;
445     }
446
447     /**
448      * Get vf modules in the custom query.
449      *
450      * @return List of VfModule
451      */
452     public List<VfModule> getAllVfModules() {
453         List<VfModule> vfModuleList = new ArrayList<>();
454
455         for (GenericVnf genVnf : this.getGenericVnfs()) {
456             vfModuleList.addAll(genVnf.getVfModules().getVfModule());
457         }
458         return vfModuleList;
459
460     }
461
462     /**
463      * Get Vf Module matching a specific VF module name.
464      *
465      * @return VfModule
466      */
467     public VfModule getVfModuleByVfModuleName(String vfModuleName) {
468         VfModule vfModule = null;
469
470         for (VfModule vfMod : this.getAllVfModules()) {
471             if (vfModuleName.equals(vfMod.getVfModuleName())) {
472                 vfModule = vfMod;
473             }
474
475         }
476         return vfModule;
477     }
478
479     /**
480      * Get Vf Module matching a specific VF model invariant ID.
481      *
482      * @return VfModule
483      */
484     public VfModule getVfModuleByVfModelInvariantId(String vfModelInvariantId) {
485         VfModule vfModule = null;
486
487         for (VfModule vfMod : this.getAllVfModules()) {
488             if (vfMod.getModelInvariantId() != null && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
489                 vfModule = vfMod;
490             }
491
492         }
493         return vfModule;
494     }
495
496     /**
497      * Get verver in the custom query.
498      *
499      * @return Vserver
500      */
501     public Vserver getVserver() {
502         Vserver vserver = null;
503         var index = 0;
504         while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
505             index = index + 1;
506         }
507         vserver = (Vserver) this.inventoryResponseItems.get(index);
508         return vserver;
509
510     }
511
512     /**
513      * Get Model Versions in the custom query.
514      *
515      * @return List of model Versions
516      */
517     public List<ModelVer> getAllModelVer() {
518         List<ModelVer> modelVerList = new ArrayList<>();
519         for (Serializable i : this.inventoryResponseItems) {
520             if (i.getClass() == ModelVer.class) {
521                 modelVerList.add((ModelVer) i);
522             }
523         }
524         return modelVerList;
525     }
526
527     /**
528      * Get ModelVer matching a specific version id.
529      *
530      * @return VfModule
531      */
532     public ModelVer getModelVerByVersionId(String versionId) {
533         ModelVer modelVer = null;
534
535         for (ModelVer modVersion : this.getAllModelVer()) {
536             if (versionId.equals(modVersion.getModelVersionId())) {
537                 modelVer = modVersion;
538             }
539
540         }
541         return modelVer;
542     }
543
544     /**
545      * Get the count of vfModules matching customizationId, InvariantId and VersionId.
546      *
547      * @param custId ModelCustomizationId
548      * @param invId ModelInvariantId
549      * @param verId ModelVersionId
550      * @return Returns the count of vf modules
551      */
552     public int getVfModuleCount(String custId, String invId, String verId) {
553         List<VfModule> vfModuleList = this.getAllVfModules();
554         var count = 0;
555         for (VfModule vfModule : vfModuleList) {
556             if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
557                 || vfModule.getModelVersionId() == null) {
558                 continue;
559             }
560
561             if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
562                 && vfModule.getModelVersionId().equals(verId)) {
563                 count = count + 1;
564             }
565         }
566         return count;
567     }
568
569 }