2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.aai;
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;
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";
50 @SerializedName("results")
51 private List<Serializable> inventoryResponseItems = new LinkedList<>();
53 private final Gson gson;
56 * Constructor creates a custom query response from a valid json string.
58 * @param jsonString A&AI Custom Query response JSON string
60 public AaiCqResponse(String jsonString) {
61 gson = new GsonBuilder()
62 .setFieldNamingStrategy(new XmlElementFieldNamingStrategy())
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");
71 for (var i = 0; i < resultsArray.length(); i++) {
72 final var resultObject = resultsArray.getJSONObject(i);
74 extractVserver(resultObject);
75 extractGenericVnf(resultObject);
76 extractServiceInstance(resultObject);
77 extractVfModule(resultObject);
78 extractCloudRegion(resultObject);
79 extractTenant(resultObject);
80 extractModelVer(resultObject);
84 private void extractVserver(final JSONObject resultObject) {
85 if (resultObject.has("vserver")) {
87 // Create the StreamSource by creating StringReader using the
89 var json = resultObject.getJSONObject("vserver").toString();
91 // Getting the vserver pojo again from the json
92 var vserver = gson.fromJson(json, Vserver.class);
93 this.inventoryResponseItems.add(vserver);
97 private void extractGenericVnf(final JSONObject resultObject) {
98 if (resultObject.has(GENERIC_VNF)) {
99 // Create the StreamSource by creating StringReader using the
101 var json = resultObject.getJSONObject(GENERIC_VNF).toString();
103 // Getting the generic vnf pojo again from the json
104 var genericVnf = gson.fromJson(json, GenericVnf.class);
105 this.inventoryResponseItems.add(genericVnf);
109 private void extractServiceInstance(final JSONObject resultObject) {
110 if (resultObject.has("service-instance")) {
112 // Create the StreamSource by creating StringReader using the
114 var json = resultObject.getJSONObject("service-instance").toString();
116 // Getting the employee pojo again from the json
117 var serviceInstance = gson.fromJson(json, ServiceInstance.class);
118 this.inventoryResponseItems.add(serviceInstance);
122 private void extractVfModule(final JSONObject resultObject) {
123 if (resultObject.has(VF_MODULE)) {
124 // Create the StreamSource by creating StringReader using the
126 var json = resultObject.getJSONObject(VF_MODULE).toString();
128 // Getting the vf module pojo again from the json
129 var vfModule = gson.fromJson(json, VfModule.class);
130 this.inventoryResponseItems.add(vfModule);
134 private void extractCloudRegion(final JSONObject resultObject) {
135 if (resultObject.has("cloud-region")) {
136 // Create the StreamSource by creating StringReader using the
138 var json = resultObject.getJSONObject("cloud-region").toString();
140 // Getting the cloud region pojo again from the json
141 var cloudRegion = gson.fromJson(json, CloudRegion.class);
142 this.inventoryResponseItems.add(cloudRegion);
146 private void extractTenant(final JSONObject resultObject) {
147 if (resultObject.has("tenant")) {
148 // Create the StreamSource by creating StringReader using the
150 var json = resultObject.getJSONObject("tenant").toString();
152 // Getting the tenant pojo again from the json
153 var tenant = gson.fromJson(json, Tenant.class);
154 this.inventoryResponseItems.add(tenant);
158 private void extractModelVer(final JSONObject resultObject) {
159 if (resultObject.has("model-ver")) {
160 // Create the StreamSource by creating StringReader using the
162 var json = resultObject.getJSONObject("model-ver").toString();
164 // Getting the ModelVer pojo again from the json
165 var modelVer = gson.fromJson(json, ModelVer.class);
166 this.inventoryResponseItems.add(modelVer);
170 public List<Serializable> getInventoryResponseItems() {
171 return inventoryResponseItems;
174 public void setInventoryResponseItems(List<Serializable> inventoryResponseItems) {
175 this.inventoryResponseItems = inventoryResponseItems;
179 * Get list of A&AI objects in the custom query.
181 * @param classOfResponse Class of the type of A&AI objects to be returned
182 * @return List A&AI objects matching the class
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);
192 return returnItemList;
197 * Get Service Instance.
199 * @return Service Instance
201 public ServiceInstance getServiceInstance() {
202 ServiceInstance serviceInstance = null;
203 for (Serializable i : this.inventoryResponseItems) {
204 if (i.getClass() == ServiceInstance.class) {
205 serviceInstance = (ServiceInstance) i;
208 return serviceInstance;
217 public Tenant getDefaultTenant() {
218 Tenant tenant = null;
219 for (Serializable i : this.inventoryResponseItems) {
220 if (i.getClass() == Tenant.class) {
231 * @return Cloud Region
233 public CloudRegion getDefaultCloudRegion() {
234 CloudRegion cloudRegion = null;
235 for (Serializable i : this.inventoryResponseItems) {
236 if (i.getClass() == CloudRegion.class) {
237 cloudRegion = (CloudRegion) i;
245 * Get Generic Vnfs in the custom query.
247 * @return List of generic Vnf
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);
256 return genericVnfList;
261 * Returns a generic Vnf matching vnf name.
263 * @param vnfName Name of the vnf to match
264 * @return generic Vnf
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);
275 for (GenericVnf genVnf : genericVnfList) {
276 if (vnfName.equals(genVnf.getVnfName())) {
286 * Returns a generic Vnf matching model invariant ID.
288 * @param modelInvariantId Name of the vnf to match
289 * @return generic Vnf
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);
300 for (GenericVnf genVnf : genericVnfList) {
301 if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
311 * Returns a generic Vnf of a given VF Module ID.
313 * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
314 * @return generic Vnf
316 public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
317 List<GenericVnf> genericVnfList = this.getGenericVnfs();
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)) {
332 * Returns the VNF given the vnf-id.
334 * @param vnfId The vnf-id
335 * @return generic Vnf
337 public GenericVnf getGenericVnfByVnfId(String vnfId) {
338 List<GenericVnf> genericVnfList = this.getGenericVnfs();
340 for (GenericVnf genVnf : genericVnfList) {
341 if (vnfId.equals(genVnf.getVnfId())) {
350 * Get the generic vnf associated with the vserver in the custom query.
352 * @return Generic VNF
354 public GenericVnf getDefaultGenericVnf() {
355 GenericVnf genericVnf = null;
357 // Get the vserver associated with the query
358 var vserver = this.getVserver();
360 // Get the relationships of the vserver
361 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
363 // Find the relationship of the genericVNF
364 var genericVnfId = "";
365 List<RelationshipData> relationshipData = null;
367 // Iterate through the list of relationships and get generic vnf
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();
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();
384 // Get the list of generic vnfs
385 List<GenericVnf> genericVnfList = this.getGenericVnfs();
387 for (GenericVnf genVnf : genericVnfList) {
388 if (genericVnfId.equals(genVnf.getVnfId())) {
397 * Get Vf Module associated with the vserver in the custom query.
401 public VfModule getDefaultVfModule() {
402 GenericVnf genericVnf = null;
403 VfModule vfModule = null;
405 // Get the vserver associated with the query
406 var vserver = this.getVserver();
408 // Get the relationships of the vserver
409 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
411 // Find the relationship of VfModule
413 List<RelationshipData> relationshipData = null;
415 // Iterate through the list of relationships and get vf module
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();
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();
432 // Get the generic VNF associated with this vserver query
433 genericVnf = this.getDefaultGenericVnf();
435 // Get the list of VFmodules associated with this generic Vnf
436 List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
438 for (VfModule vfMod : vfModuleList) {
439 if (vfModuleId.equals(vfMod.getVfModuleId())) {
448 * Get vf modules in the custom query.
450 * @return List of VfModule
452 public List<VfModule> getAllVfModules() {
453 List<VfModule> vfModuleList = new ArrayList<>();
455 for (GenericVnf genVnf : this.getGenericVnfs()) {
456 vfModuleList.addAll(genVnf.getVfModules().getVfModule());
463 * Get Vf Module matching a specific VF module name.
467 public VfModule getVfModuleByVfModuleName(String vfModuleName) {
468 VfModule vfModule = null;
470 for (VfModule vfMod : this.getAllVfModules()) {
471 if (vfModuleName.equals(vfMod.getVfModuleName())) {
480 * Get Vf Module matching a specific VF model invariant ID.
484 public VfModule getVfModuleByVfModelInvariantId(String vfModelInvariantId) {
485 VfModule vfModule = null;
487 for (VfModule vfMod : this.getAllVfModules()) {
488 if (vfMod.getModelInvariantId() != null && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
497 * Get verver in the custom query.
501 public Vserver getVserver() {
502 Vserver vserver = null;
504 while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
507 vserver = (Vserver) this.inventoryResponseItems.get(index);
513 * Get Model Versions in the custom query.
515 * @return List of model Versions
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);
528 * Get ModelVer matching a specific version id.
532 public ModelVer getModelVerByVersionId(String versionId) {
533 ModelVer modelVer = null;
535 for (ModelVer modVersion : this.getAllModelVer()) {
536 if (versionId.equals(modVersion.getModelVersionId())) {
537 modelVer = modVersion;
545 * Get the count of vfModules matching customizationId, InvariantId and VersionId.
547 * @param custId ModelCustomizationId
548 * @param invId ModelInvariantId
549 * @param verId ModelVersionId
550 * @return Returns the count of vf modules
552 public int getVfModuleCount(String custId, String invId, String verId) {
553 List<VfModule> vfModuleList = this.getAllVfModules();
555 for (VfModule vfModule : vfModuleList) {
556 if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
557 || vfModule.getModelVersionId() == null) {
561 if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
562 && vfModule.getModelVersionId().equals(verId)) {