2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.aai;
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;
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;
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;
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
67 jaxbContext = JAXBContextFactory.createContext(new Class[] {
72 ServiceInstance.class,
78 // verify that we can create an unmarshaller
79 jaxbContext.createUnmarshaller();
81 } catch (JAXBException e) {
82 LOGGER.error("Could not initialize JAXBContext", e);
83 LOGGER.info("Problem initiatlizing JAXBContext", e);
87 @SerializedName("results")
88 private List<Serializable> inventoryResponseItems = new LinkedList<>();
91 * Constructor creates a custom query response from a valid json string.
93 * @param jsonString A&AI Custom Query response JSON string
95 public AaiCqResponse(String jsonString) {
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");
103 for (var i = 0; i < resultsArray.length(); i++) {
104 final var resultObject = resultsArray.getJSONObject(i);
106 extractVserver(resultObject);
107 extractGenericVnf(resultObject);
108 extractServiceInstance(resultObject);
109 extractVfModule(resultObject);
110 extractCloudRegion(resultObject);
111 extractTenant(resultObject);
112 extractModelVer(resultObject);
116 private void extractVserver(final JSONObject resultObject) {
117 if (resultObject.has("vserver")) {
119 // Create the StreamSource by creating StringReader using the
121 var json = new StreamSource(
122 new StringReader(resultObject.getJSONObject("vserver").toString()));
124 // Getting the vserver pojo again from the json
125 var vserver = this.getAaiObject(json, Vserver.class);
126 this.inventoryResponseItems.add(vserver);
130 private void extractGenericVnf(final JSONObject resultObject) {
131 if (resultObject.has(GENERIC_VNF)) {
132 // Create the StreamSource by creating StringReader using the
134 var json = new StreamSource(
135 new StringReader(resultObject.getJSONObject(GENERIC_VNF).toString()));
137 // Getting the generic vnf pojo again from the json
138 var genericVnf = this.getAaiObject(json, GenericVnf.class);
140 this.inventoryResponseItems.add(genericVnf);
144 private void extractServiceInstance(final JSONObject resultObject) {
145 if (resultObject.has("service-instance")) {
147 // Create the StreamSource by creating StringReader using the
149 var json = new StreamSource(
150 new StringReader(resultObject.getJSONObject("service-instance").toString()));
152 // Getting the employee pojo again from the json
153 var serviceInstance = this.getAaiObject(json, ServiceInstance.class);
155 this.inventoryResponseItems.add(serviceInstance);
159 private void extractVfModule(final JSONObject resultObject) {
160 if (resultObject.has(VF_MODULE)) {
161 // Create the StreamSource by creating StringReader using the
163 var json = new StreamSource(
164 new StringReader(resultObject.getJSONObject(VF_MODULE).toString()));
166 // Getting the vf module pojo again from the json
167 var vfModule = this.getAaiObject(json, VfModule.class);
169 this.inventoryResponseItems.add(vfModule);
173 private void extractCloudRegion(final JSONObject resultObject) {
174 if (resultObject.has("cloud-region")) {
175 // Create the StreamSource by creating StringReader using the
177 var json = new StreamSource(
178 new StringReader(resultObject.getJSONObject("cloud-region").toString()));
180 // Getting the cloud region pojo again from the json
181 var cloudRegion = this.getAaiObject(json, CloudRegion.class);
183 this.inventoryResponseItems.add(cloudRegion);
187 private void extractTenant(final JSONObject resultObject) {
188 if (resultObject.has("tenant")) {
189 // Create the StreamSource by creating StringReader using the
191 var json = new StreamSource(
192 new StringReader(resultObject.getJSONObject("tenant").toString()));
194 // Getting the tenant pojo again from the json
195 var tenant = this.getAaiObject(json, Tenant.class);
197 this.inventoryResponseItems.add(tenant);
201 private void extractModelVer(final JSONObject resultObject) {
202 if (resultObject.has("model-ver")) {
203 // Create the StreamSource by creating StringReader using the
205 var json = new StreamSource(
206 new StringReader(resultObject.getJSONObject("model-ver").toString()));
208 // Getting the ModelVer pojo again from the json
209 var modelVer = this.getAaiObject(json, ModelVer.class);
211 this.inventoryResponseItems.add(modelVer);
215 private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
217 return jaxbContext.createUnmarshaller().unmarshal(json, classOfResponse).getValue();
218 } catch (JAXBException e) {
219 LOGGER.error("JAXBCOntext error", e);
224 public List<Serializable> getInventoryResponseItems() {
225 return inventoryResponseItems;
228 public void setInventoryResponseItems(List<Serializable> inventoryResponseItems) {
229 this.inventoryResponseItems = inventoryResponseItems;
233 * Get list of A&AI objects in the custom query.
235 * @param classOfResponse Class of the type of A&AI objects to be returned
236 * @return List A&AI objects matching the class
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);
246 return returnItemList;
251 * Get Service Instance.
253 * @return Service Instance
255 public ServiceInstance getServiceInstance() {
256 ServiceInstance serviceInstance = null;
257 for (Serializable i : this.inventoryResponseItems) {
258 if (i.getClass() == ServiceInstance.class) {
259 serviceInstance = (ServiceInstance) i;
262 return serviceInstance;
271 public Tenant getDefaultTenant() {
272 Tenant tenant = null;
273 for (Serializable i : this.inventoryResponseItems) {
274 if (i.getClass() == Tenant.class) {
285 * @return Cloud Region
287 public CloudRegion getDefaultCloudRegion() {
288 CloudRegion cloudRegion = null;
289 for (Serializable i : this.inventoryResponseItems) {
290 if (i.getClass() == CloudRegion.class) {
291 cloudRegion = (CloudRegion) i;
299 * Get Generic Vnfs in the custom query.
301 * @return List of generic Vnf
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);
310 return genericVnfList;
315 * Returns a generic Vnf matching vnf name.
317 * @param vnfName Name of the vnf to match
318 * @return generic Vnf
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);
329 for (GenericVnf genVnf : genericVnfList) {
330 if (vnfName.equals(genVnf.getVnfName())) {
340 * Returns a generic Vnf matching model invariant ID.
342 * @param modelInvariantId Name of the vnf to match
343 * @return generic Vnf
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);
354 for (GenericVnf genVnf : genericVnfList) {
355 if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
365 * Returns a generic Vnf of a given VF Module ID.
367 * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
368 * @return generic Vnf
370 public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
371 List<GenericVnf> genericVnfList = this.getGenericVnfs();
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)) {
386 * Returns the VNF given the vnf-id.
388 * @param vnfId The vnf-id
389 * @return generic Vnf
391 public GenericVnf getGenericVnfByVnfId(String vnfId) {
392 List<GenericVnf> genericVnfList = this.getGenericVnfs();
394 for (GenericVnf genVnf : genericVnfList) {
395 if (vnfId.equals(genVnf.getVnfId())) {
404 * Get the generic vnf associated with the vserver in the custom query.
406 * @return Generic VNF
408 public GenericVnf getDefaultGenericVnf() {
409 GenericVnf genericVnf = null;
411 // Get the vserver associated with the query
412 var vserver = this.getVserver();
414 // Get the relationships of the vserver
415 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
417 // Find the relationship of the genericVNF
418 var genericVnfId = "";
419 List<RelationshipData> relationshipData = null;
421 // Iterate through the list of relationships and get generic vnf
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();
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();
438 // Get the list of generic vnfs
439 List<GenericVnf> genericVnfList = this.getGenericVnfs();
441 for (GenericVnf genVnf : genericVnfList) {
442 if (genericVnfId.equals(genVnf.getVnfId())) {
451 * Get Vf Module associated with the vserver in the custom query.
455 public VfModule getDefaultVfModule() {
456 GenericVnf genericVnf = null;
457 VfModule vfModule = null;
459 // Get the vserver associated with the query
460 var vserver = this.getVserver();
462 // Get the relationships of the vserver
463 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
465 // Find the relationship of VfModule
467 List<RelationshipData> relationshipData = null;
469 // Iterate through the list of relationships and get vf module
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();
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();
486 // Get the generic VNF associated with this vserver query
487 genericVnf = this.getDefaultGenericVnf();
489 // Get the list of VFmodules associated with this generic Vnf
490 List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
492 for (VfModule vfMod : vfModuleList) {
493 if (vfModuleId.equals(vfMod.getVfModuleId())) {
502 * Get vf modules in the custom query.
504 * @return List of VfModule
506 public List<VfModule> getAllVfModules() {
507 List<VfModule> vfModuleList = new ArrayList<>();
509 for (GenericVnf genVnf : this.getGenericVnfs()) {
510 vfModuleList.addAll(genVnf.getVfModules().getVfModule());
517 * Get Vf Module matching a specific VF module name.
521 public VfModule getVfModuleByVfModuleName(String vfModuleName) {
522 VfModule vfModule = null;
524 for (VfModule vfMod : this.getAllVfModules()) {
525 if (vfModuleName.equals(vfMod.getVfModuleName())) {
534 * Get Vf Module matching a specific VF model invariant ID.
538 public VfModule getVfModuleByVfModelInvariantId(String vfModelInvariantId) {
539 VfModule vfModule = null;
541 for (VfModule vfMod : this.getAllVfModules()) {
542 if (vfMod.getModelInvariantId() != null && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
551 * Get verver in the custom query.
555 public Vserver getVserver() {
556 Vserver vserver = null;
558 while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
561 vserver = (Vserver) this.inventoryResponseItems.get(index);
567 * Get Model Versions in the custom query.
569 * @return List of model Versions
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);
582 * Get ModelVer matching a specific version id.
586 public ModelVer getModelVerByVersionId(String versionId) {
587 ModelVer modelVer = null;
589 for (ModelVer modVersion : this.getAllModelVer()) {
590 if (versionId.equals(modVersion.getModelVersionId())) {
591 modelVer = modVersion;
599 * Get the count of vfModules matching customizationId, InvariantId and VersionId.
601 * @param custId ModelCustomizationId
602 * @param invId ModelInvariantId
603 * @param verId ModelVersionId
604 * @return Returns the count of vf modules
606 public int getVfModuleCount(String custId, String invId, String verId) {
607 List<VfModule> vfModuleList = this.getAllVfModules();
609 for (VfModule vfModule : vfModuleList) {
610 if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
611 || vfModule.getModelVersionId() == null) {
615 if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
616 && vfModule.getModelVersionId().equals(verId)) {