2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.bind.Unmarshaller;
34 import javax.xml.transform.stream.StreamSource;
35 import org.eclipse.persistence.jaxb.JAXBContextFactory;
36 import org.eclipse.persistence.jaxb.JAXBContextProperties;
37 import org.json.JSONArray;
38 import org.json.JSONObject;
39 import org.onap.aai.domain.yang.CloudRegion;
40 import org.onap.aai.domain.yang.GenericVnf;
41 import org.onap.aai.domain.yang.ModelVer;
42 import org.onap.aai.domain.yang.Relationship;
43 import org.onap.aai.domain.yang.RelationshipData;
44 import org.onap.aai.domain.yang.ServiceInstance;
45 import org.onap.aai.domain.yang.Tenant;
46 import org.onap.aai.domain.yang.VfModule;
47 import org.onap.aai.domain.yang.Vserver;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class AaiCqResponse implements Serializable {
52 private static final long serialVersionUID = 1L;
53 public static final String CONTEXT_KEY = AaiConstants.CONTEXT_PREFIX + "AaiCqResponse";
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 private static Unmarshaller unmarshaller;
62 Map<String, Object> properties = new HashMap<>();
63 properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
64 properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
65 // Define JAXB context
68 jaxbContext = JAXBContextFactory.createContext(new Class[] {
73 ServiceInstance.class,
78 unmarshaller = jaxbContext.createUnmarshaller();
79 } catch (JAXBException e) {
80 LOGGER.error("Could not initialize JAXBContext", e);
81 LOGGER.info("Problem initiatlizing JAXBContext", e);
85 @SerializedName("results")
86 private List<Serializable> inventoryResponseItems = new LinkedList<>();
89 * Constructor creates a custom query response from a valid json string.
91 * @param jsonString A&AI Custom Query response JSON string
93 public AaiCqResponse(String jsonString) {
95 // Read JSON String and add all AaiObjects
96 JSONObject responseObj = new JSONObject(jsonString);
97 JSONArray resultsArray = new JSONArray();
98 if (responseObj.has("results")) {
99 resultsArray = (JSONArray) responseObj.get("results");
101 for (int i = 0; i < resultsArray.length(); i++) {
102 // Object is a vserver
103 if (resultsArray.getJSONObject(i).has("vserver")) {
105 // Create the StreamSource by creating StringReader using the
107 StreamSource json = new StreamSource(
108 new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
110 // Getting the vserver pojo again from the json
111 Vserver vserver = this.getAaiObject(json, Vserver.class);
112 this.inventoryResponseItems.add(vserver);
115 // Object is a Generic VNF
116 if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
117 // Create the StreamSource by creating StringReader using the
119 StreamSource json = new StreamSource(
120 new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
122 // Getting the generic vnf pojo again from the json
123 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
125 this.inventoryResponseItems.add(genericVnf);
128 // Object is a Service Instance
129 if (resultsArray.getJSONObject(i).has("service-instance")) {
131 // Create the StreamSource by creating StringReader using the
133 StreamSource json = new StreamSource(
134 new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
136 // Getting the employee pojo again from the json
137 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
139 this.inventoryResponseItems.add(serviceInstance);
142 // Object is a VF Module
143 if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
144 // Create the StreamSource by creating StringReader using the
146 StreamSource json = new StreamSource(
147 new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
149 // Getting the vf module pojo again from the json
150 VfModule vfModule = this.getAaiObject(json, VfModule.class);
152 this.inventoryResponseItems.add(vfModule);
155 // Object is a CloudRegion
156 if (resultsArray.getJSONObject(i).has("cloud-region")) {
157 // Create the StreamSource by creating StringReader using the
159 StreamSource json = new StreamSource(
160 new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
162 // Getting the cloud region pojo again from the json
163 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
165 this.inventoryResponseItems.add(cloudRegion);
168 // Object is a Tenant
169 if (resultsArray.getJSONObject(i).has("tenant")) {
170 // Create the StreamSource by creating StringReader using the
172 StreamSource json = new StreamSource(
173 new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
175 // Getting the tenant pojo again from the json
176 Tenant tenant = this.getAaiObject(json, Tenant.class);
178 this.inventoryResponseItems.add(tenant);
181 // Object is a ModelVer
182 if (resultsArray.getJSONObject(i).has("model-ver")) {
183 // Create the StreamSource by creating StringReader using the
185 StreamSource json = new StreamSource(
186 new StringReader(resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
188 // Getting the ModelVer pojo again from the json
189 ModelVer modelVer = this.getAaiObject(json, ModelVer.class);
191 this.inventoryResponseItems.add(modelVer);
198 private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
200 return unmarshaller.unmarshal(json, classOfResponse).getValue();
201 } catch (JAXBException e) {
202 LOGGER.error("JAXBCOntext error", e);
207 public List<Serializable> getInventoryResponseItems() {
208 return inventoryResponseItems;
211 public void setInventoryResponseItems(List<Serializable> inventoryResponseItems) {
212 this.inventoryResponseItems = inventoryResponseItems;
216 * Get list of A&AI objects in the custom query.
218 * @param classOfResponse Class of the type of A&AI objects to be returned
219 * @return List A&AI objects matching the class
221 @SuppressWarnings("unchecked")
222 public <T> List<T> getItemListByType(Class<T> classOfResponse) {
223 List<T> returnItemList = new ArrayList<>();
224 for (Serializable i : this.inventoryResponseItems) {
225 if (i.getClass() == classOfResponse) {
226 returnItemList.add((T) i);
229 return returnItemList;
234 * Get Service Instance.
236 * @return Service Instance
238 public ServiceInstance getServiceInstance() {
239 ServiceInstance serviceInstance = null;
240 for (Serializable i : this.inventoryResponseItems) {
241 if (i.getClass() == ServiceInstance.class) {
242 serviceInstance = (ServiceInstance) i;
245 return serviceInstance;
254 public Tenant getDefaultTenant() {
255 Tenant tenant = null;
256 for (Serializable i : this.inventoryResponseItems) {
257 if (i.getClass() == Tenant.class) {
268 * @return Cloud Region
270 public CloudRegion getDefaultCloudRegion() {
271 CloudRegion cloudRegion = null;
272 for (Serializable i : this.inventoryResponseItems) {
273 if (i.getClass() == CloudRegion.class) {
274 cloudRegion = (CloudRegion) i;
282 * Get Generic Vnfs in the custom query.
284 * @return List of generic Vnf
286 public List<GenericVnf> getGenericVnfs() {
287 List<GenericVnf> genericVnfList = new ArrayList<>();
288 for (Serializable i : this.inventoryResponseItems) {
289 if (i.getClass() == GenericVnf.class) {
290 genericVnfList.add((GenericVnf) i);
293 return genericVnfList;
298 * Returns a generic Vnf matching vnf name.
300 * @param vnfName Name of the vnf to match
301 * @return generic Vnf
303 public GenericVnf getGenericVnfByVnfName(String vnfName) {
304 List<GenericVnf> genericVnfList = new ArrayList<>();
305 GenericVnf genericVnf = null;
306 for (Serializable i : this.inventoryResponseItems) {
307 if (i.getClass() == GenericVnf.class) {
308 genericVnfList.add((GenericVnf) i);
312 for (GenericVnf genVnf : genericVnfList) {
313 if (vnfName.equals(genVnf.getVnfName())) {
323 * Returns a generic Vnf matching model invariant ID.
325 * @param modelInvariantId Name of the vnf to match
326 * @return generic Vnf
328 public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
329 List<GenericVnf> genericVnfList = new ArrayList<>();
330 GenericVnf genericVnf = null;
331 for (Serializable i : this.inventoryResponseItems) {
332 if (i.getClass() == GenericVnf.class) {
333 genericVnfList.add((GenericVnf) i);
337 for (GenericVnf genVnf : genericVnfList) {
338 if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
348 * Returns a generic Vnf of a given VF Module ID.
350 * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
351 * @return generic Vnf
353 public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
354 List<GenericVnf> genericVnfList = this.getGenericVnfs();
356 for (GenericVnf genVnf : genericVnfList) {
357 // Iterate through all the vfModules of that generic Vnf
358 for (VfModule vfMod : genVnf.getVfModules().getVfModule()) {
359 if (vfMod.getModelInvariantId() != null
360 && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) {
369 * Get the generic vnf associated with the vserver in the custom query.
371 * @return Generic VNF
373 public GenericVnf getDefaultGenericVnf() {
374 GenericVnf genericVnf = null;
376 // Get the vserver associated with the query
377 Vserver vserver = this.getVserver();
379 // Get the relationships of the vserver
380 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
382 // Find the relationship of the genericVNF
383 String genericVnfId = "";
384 List<RelationshipData> relationshipData = null;
386 // Iterate through the list of relationships and get generic vnf
388 for (Relationship r : relations) {
389 // Get the name of generic-vnf related to this server
390 if (GENERIC_VNF.equals(r.getRelatedTo())) {
391 relationshipData = r.getRelationshipData();
395 // Iterate through relationship data, and get vnf-id
396 for (RelationshipData rd : relationshipData) {
397 // Get the id of the generic-vnf
398 if ("generic-vnf.vnf-id".equals(rd.getRelationshipKey())) {
399 genericVnfId = rd.getRelationshipValue();
403 // Get the list of generic vnfs
404 List<GenericVnf> genericVnfList = this.getGenericVnfs();
406 for (GenericVnf genVnf : genericVnfList) {
407 if (genericVnfId.equals(genVnf.getVnfId())) {
416 * Get Vf Module associated with the vserver in the custom query.
420 public VfModule getDefaultVfModule() {
421 GenericVnf genericVnf = null;
422 VfModule vfModule = null;
424 // Get the vserver associated with the query
425 Vserver vserver = this.getVserver();
427 // Get the relationships of the vserver
428 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
430 // Find the relationship of VfModule
431 String vfModuleId = "";
432 List<RelationshipData> relationshipData = null;
434 // Iterate through the list of relationships and get vf module
436 for (Relationship r : relations) {
437 // Get relationship data of vfmodule related to this server
438 if (VF_MODULE.equals(r.getRelatedTo())) {
439 relationshipData = r.getRelationshipData();
443 // Iterate through relationship data, and get vf-module-id
444 for (RelationshipData rd : relationshipData) {
445 // Get the id of the vf-module
446 if ("vf-module.vf-module-id".equals(rd.getRelationshipKey())) {
447 vfModuleId = rd.getRelationshipValue();
451 // Get the generic VNF associated with this vserver query
452 genericVnf = this.getDefaultGenericVnf();
454 // Get the list of VFmodules associated with this generic Vnf
455 List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
457 for (VfModule vfMod : vfModuleList) {
458 if (vfModuleId.equals(vfMod.getVfModuleId())) {
467 * Get vf modules in the custom query.
469 * @return List of VfModule
471 public List<VfModule> getAllVfModules() {
472 List<VfModule> vfModuleList = new ArrayList<>();
474 for (GenericVnf genVnf : this.getGenericVnfs()) {
475 vfModuleList.addAll(genVnf.getVfModules().getVfModule());
482 * Get Vf Module matching a specific VF module name.
486 public VfModule getVfModuleByVfModuleName(String vfModuleName) {
487 VfModule vfModule = null;
489 for (VfModule vfMod : this.getAllVfModules()) {
490 if (vfModuleName.equals(vfMod.getVfModuleName())) {
499 * Get Vf Module matching a specific VF model invariant ID.
503 public VfModule getVfModuleByVfModelInvariantId(String vfModelInvariantId) {
504 VfModule vfModule = null;
506 for (VfModule vfMod : this.getAllVfModules()) {
507 if (vfMod.getModelInvariantId() != null && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
516 * Get verver in the custom query.
520 public Vserver getVserver() {
521 Vserver vserver = null;
523 while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
526 vserver = (Vserver) this.inventoryResponseItems.get(index);
532 * Get Model Versions in the custom query.
534 * @return List of model Versions
536 public List<ModelVer> getAllModelVer() {
537 List<ModelVer> modelVerList = new ArrayList<>();
538 for (Serializable i : this.inventoryResponseItems) {
539 if (i.getClass() == ModelVer.class) {
540 modelVerList.add((ModelVer) i);
547 * Get ModelVer matching a specific version id.
551 public ModelVer getModelVerByVersionId(String versionId) {
552 ModelVer modelVer = null;
554 for (ModelVer modVersion : this.getAllModelVer()) {
555 if (versionId.equals(modVersion.getModelVersionId())) {
556 modelVer = modVersion;
564 * Get the count of vfModules matching customizationId, InvariantId and VersionId.
566 * @param custId ModelCustomizationId
567 * @param invId ModelInvariantId
568 * @param verId ModelVersionId
569 * @return Returns the count of vf modules
571 public int getVfModuleCount(String custId, String invId, String verId) {
572 List<VfModule> vfModuleList = this.getAllVfModules();
574 for (VfModule vfModule : vfModuleList) {
575 if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
576 || vfModule.getModelVersionId() == null) {
580 if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
581 && vfModule.getModelVersionId().equals(verId)) {