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 public static final String OPERATION = "CustomQuery";
55 private static final String GENERIC_VNF = "generic-vnf";
56 private static final String VF_MODULE = "vf-module";
57 private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponse.class);
58 private static JAXBContext jaxbContext;
59 private static Unmarshaller unmarshaller;
63 Map<String, Object> properties = new HashMap<>();
64 properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
65 properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
66 // Define JAXB context
69 jaxbContext = JAXBContextFactory.createContext(new Class[] {
74 ServiceInstance.class,
79 unmarshaller = jaxbContext.createUnmarshaller();
80 } catch (JAXBException e) {
81 LOGGER.error("Could not initialize JAXBContext", e);
82 LOGGER.info("Problem initiatlizing JAXBContext", e);
86 @SerializedName("results")
87 private List<Serializable> inventoryResponseItems = new LinkedList<>();
90 * Constructor creates a custom query response from a valid json string.
92 * @param jsonString A&AI Custom Query response JSON string
94 public AaiCqResponse(String jsonString) {
96 // Read JSON String and add all AaiObjects
97 JSONObject responseObj = new JSONObject(jsonString);
98 JSONArray resultsArray = new JSONArray();
99 if (responseObj.has("results")) {
100 resultsArray = (JSONArray) responseObj.get("results");
102 for (int i = 0; i < resultsArray.length(); i++) {
103 // Object is a vserver
104 if (resultsArray.getJSONObject(i).has("vserver")) {
106 // Create the StreamSource by creating StringReader using the
108 StreamSource json = new StreamSource(
109 new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
111 // Getting the vserver pojo again from the json
112 Vserver vserver = this.getAaiObject(json, Vserver.class);
113 this.inventoryResponseItems.add(vserver);
116 // Object is a Generic VNF
117 if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
118 // Create the StreamSource by creating StringReader using the
120 StreamSource json = new StreamSource(
121 new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
123 // Getting the generic vnf pojo again from the json
124 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
126 this.inventoryResponseItems.add(genericVnf);
129 // Object is a Service Instance
130 if (resultsArray.getJSONObject(i).has("service-instance")) {
132 // Create the StreamSource by creating StringReader using the
134 StreamSource json = new StreamSource(
135 new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
137 // Getting the employee pojo again from the json
138 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
140 this.inventoryResponseItems.add(serviceInstance);
143 // Object is a VF Module
144 if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
145 // Create the StreamSource by creating StringReader using the
147 StreamSource json = new StreamSource(
148 new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
150 // Getting the vf module pojo again from the json
151 VfModule vfModule = this.getAaiObject(json, VfModule.class);
153 this.inventoryResponseItems.add(vfModule);
156 // Object is a CloudRegion
157 if (resultsArray.getJSONObject(i).has("cloud-region")) {
158 // Create the StreamSource by creating StringReader using the
160 StreamSource json = new StreamSource(
161 new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
163 // Getting the cloud region pojo again from the json
164 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
166 this.inventoryResponseItems.add(cloudRegion);
169 // Object is a Tenant
170 if (resultsArray.getJSONObject(i).has("tenant")) {
171 // Create the StreamSource by creating StringReader using the
173 StreamSource json = new StreamSource(
174 new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
176 // Getting the tenant pojo again from the json
177 Tenant tenant = this.getAaiObject(json, Tenant.class);
179 this.inventoryResponseItems.add(tenant);
182 // Object is a ModelVer
183 if (resultsArray.getJSONObject(i).has("model-ver")) {
184 // Create the StreamSource by creating StringReader using the
186 StreamSource json = new StreamSource(
187 new StringReader(resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
189 // Getting the ModelVer pojo again from the json
190 ModelVer modelVer = this.getAaiObject(json, ModelVer.class);
192 this.inventoryResponseItems.add(modelVer);
199 private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
201 return unmarshaller.unmarshal(json, classOfResponse).getValue();
202 } catch (JAXBException e) {
203 LOGGER.error("JAXBCOntext error", e);
208 public List<Serializable> getInventoryResponseItems() {
209 return inventoryResponseItems;
212 public void setInventoryResponseItems(List<Serializable> inventoryResponseItems) {
213 this.inventoryResponseItems = inventoryResponseItems;
217 * Get list of A&AI objects in the custom query.
219 * @param classOfResponse Class of the type of A&AI objects to be returned
220 * @return List A&AI objects matching the class
222 @SuppressWarnings("unchecked")
223 public <T> List<T> getItemListByType(Class<T> classOfResponse) {
224 List<T> returnItemList = new ArrayList<>();
225 for (Serializable i : this.inventoryResponseItems) {
226 if (i.getClass() == classOfResponse) {
227 returnItemList.add((T) i);
230 return returnItemList;
235 * Get Service Instance.
237 * @return Service Instance
239 public ServiceInstance getServiceInstance() {
240 ServiceInstance serviceInstance = null;
241 for (Serializable i : this.inventoryResponseItems) {
242 if (i.getClass() == ServiceInstance.class) {
243 serviceInstance = (ServiceInstance) i;
246 return serviceInstance;
255 public Tenant getDefaultTenant() {
256 Tenant tenant = null;
257 for (Serializable i : this.inventoryResponseItems) {
258 if (i.getClass() == Tenant.class) {
269 * @return Cloud Region
271 public CloudRegion getDefaultCloudRegion() {
272 CloudRegion cloudRegion = null;
273 for (Serializable i : this.inventoryResponseItems) {
274 if (i.getClass() == CloudRegion.class) {
275 cloudRegion = (CloudRegion) i;
283 * Get Generic Vnfs in the custom query.
285 * @return List of generic Vnf
287 public List<GenericVnf> getGenericVnfs() {
288 List<GenericVnf> genericVnfList = new ArrayList<>();
289 for (Serializable i : this.inventoryResponseItems) {
290 if (i.getClass() == GenericVnf.class) {
291 genericVnfList.add((GenericVnf) i);
294 return genericVnfList;
299 * Returns a generic Vnf matching vnf name.
301 * @param vnfName Name of the vnf to match
302 * @return generic Vnf
304 public GenericVnf getGenericVnfByVnfName(String vnfName) {
305 List<GenericVnf> genericVnfList = new ArrayList<>();
306 GenericVnf genericVnf = null;
307 for (Serializable i : this.inventoryResponseItems) {
308 if (i.getClass() == GenericVnf.class) {
309 genericVnfList.add((GenericVnf) i);
313 for (GenericVnf genVnf : genericVnfList) {
314 if (vnfName.equals(genVnf.getVnfName())) {
324 * Returns a generic Vnf matching model invariant ID.
326 * @param modelInvariantId Name of the vnf to match
327 * @return generic Vnf
329 public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
330 List<GenericVnf> genericVnfList = new ArrayList<>();
331 GenericVnf genericVnf = null;
332 for (Serializable i : this.inventoryResponseItems) {
333 if (i.getClass() == GenericVnf.class) {
334 genericVnfList.add((GenericVnf) i);
338 for (GenericVnf genVnf : genericVnfList) {
339 if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
349 * Returns a generic Vnf of a given VF Module ID.
351 * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
352 * @return generic Vnf
354 public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
355 List<GenericVnf> genericVnfList = this.getGenericVnfs();
357 for (GenericVnf genVnf : genericVnfList) {
358 // Iterate through all the vfModules of that generic Vnf
359 for (VfModule vfMod : genVnf.getVfModules().getVfModule()) {
360 if (vfMod.getModelInvariantId() != null
361 && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) {
370 * Get the generic vnf associated with the vserver in the custom query.
372 * @return Generic VNF
374 public GenericVnf getDefaultGenericVnf() {
375 GenericVnf genericVnf = null;
377 // Get the vserver associated with the query
378 Vserver vserver = this.getVserver();
380 // Get the relationships of the vserver
381 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
383 // Find the relationship of the genericVNF
384 String genericVnfId = "";
385 List<RelationshipData> relationshipData = null;
387 // Iterate through the list of relationships and get generic vnf
389 for (Relationship r : relations) {
390 // Get the name of generic-vnf related to this server
391 if (GENERIC_VNF.equals(r.getRelatedTo())) {
392 relationshipData = r.getRelationshipData();
396 // Iterate through relationship data, and get vnf-id
397 for (RelationshipData rd : relationshipData) {
398 // Get the id of the generic-vnf
399 if ("generic-vnf.vnf-id".equals(rd.getRelationshipKey())) {
400 genericVnfId = rd.getRelationshipValue();
404 // Get the list of generic vnfs
405 List<GenericVnf> genericVnfList = this.getGenericVnfs();
407 for (GenericVnf genVnf : genericVnfList) {
408 if (genericVnfId.equals(genVnf.getVnfId())) {
417 * Get Vf Module associated with the vserver in the custom query.
421 public VfModule getDefaultVfModule() {
422 GenericVnf genericVnf = null;
423 VfModule vfModule = null;
425 // Get the vserver associated with the query
426 Vserver vserver = this.getVserver();
428 // Get the relationships of the vserver
429 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
431 // Find the relationship of VfModule
432 String vfModuleId = "";
433 List<RelationshipData> relationshipData = null;
435 // Iterate through the list of relationships and get vf module
437 for (Relationship r : relations) {
438 // Get relationship data of vfmodule related to this server
439 if (VF_MODULE.equals(r.getRelatedTo())) {
440 relationshipData = r.getRelationshipData();
444 // Iterate through relationship data, and get vf-module-id
445 for (RelationshipData rd : relationshipData) {
446 // Get the id of the vf-module
447 if ("vf-module.vf-module-id".equals(rd.getRelationshipKey())) {
448 vfModuleId = rd.getRelationshipValue();
452 // Get the generic VNF associated with this vserver query
453 genericVnf = this.getDefaultGenericVnf();
455 // Get the list of VFmodules associated with this generic Vnf
456 List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
458 for (VfModule vfMod : vfModuleList) {
459 if (vfModuleId.equals(vfMod.getVfModuleId())) {
468 * Get vf modules in the custom query.
470 * @return List of VfModule
472 public List<VfModule> getAllVfModules() {
473 List<VfModule> vfModuleList = new ArrayList<>();
475 for (GenericVnf genVnf : this.getGenericVnfs()) {
476 vfModuleList.addAll(genVnf.getVfModules().getVfModule());
483 * Get Vf Module matching a specific VF module name.
487 public VfModule getVfModuleByVfModuleName(String vfModuleName) {
488 VfModule vfModule = null;
490 for (VfModule vfMod : this.getAllVfModules()) {
491 if (vfModuleName.equals(vfMod.getVfModuleName())) {
500 * Get Vf Module matching a specific VF model invariant ID.
504 public VfModule getVfModuleByVfModelInvariantId(String vfModelInvariantId) {
505 VfModule vfModule = null;
507 for (VfModule vfMod : this.getAllVfModules()) {
508 if (vfMod.getModelInvariantId() != null && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
517 * Get verver in the custom query.
521 public Vserver getVserver() {
522 Vserver vserver = null;
524 while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
527 vserver = (Vserver) this.inventoryResponseItems.get(index);
533 * Get Model Versions in the custom query.
535 * @return List of model Versions
537 public List<ModelVer> getAllModelVer() {
538 List<ModelVer> modelVerList = new ArrayList<>();
539 for (Serializable i : this.inventoryResponseItems) {
540 if (i.getClass() == ModelVer.class) {
541 modelVerList.add((ModelVer) i);
548 * Get ModelVer matching a specific version id.
552 public ModelVer getModelVerByVersionId(String versionId) {
553 ModelVer modelVer = null;
555 for (ModelVer modVersion : this.getAllModelVer()) {
556 if (versionId.equals(modVersion.getModelVersionId())) {
557 modelVer = modVersion;
565 * Get the count of vfModules matching customizationId, InvariantId and VersionId.
567 * @param custId ModelCustomizationId
568 * @param invId ModelInvariantId
569 * @param verId ModelVersionId
570 * @return Returns the count of vf modules
572 public int getVfModuleCount(String custId, String invId, String verId) {
573 List<VfModule> vfModuleList = this.getAllVfModules();
575 for (VfModule vfModule : vfModuleList) {
576 if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
577 || vfModule.getModelVersionId() == null) {
581 if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
582 && vfModule.getModelVersionId().equals(verId)) {