2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.StringReader;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.LinkedList;
28 import java.util.List;
30 import javax.xml.bind.JAXBContext;
31 import javax.xml.bind.JAXBException;
32 import javax.xml.bind.Unmarshaller;
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 {
52 private static final String GENERIC_VNF = "generic-vnf";
53 private static final String VF_MODULE = "vf-module";
54 private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponse.class);
55 private static JAXBContext jaxbContext;
56 private static Unmarshaller unmarshaller;
60 Map<String, Object> properties = new HashMap<>();
61 properties.put(JAXBContextProperties.MEDIA_TYPE, "application/json");
62 properties.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
63 // Define JAXB context
68 new Class[] {Vserver.class, GenericVnf.class, VfModule.class,
69 CloudRegion.class, ServiceInstance.class, Tenant.class, ModelVer.class},
71 unmarshaller = jaxbContext.createUnmarshaller();
72 } catch (JAXBException e) {
73 LOGGER.error("Could not initialize JAXBContext", e);
74 LOGGER.info("Problem initiatlizing JAXBContext", e);
78 @SerializedName("results")
79 private List<Object> inventoryResponseItems = new LinkedList<>();
82 * Constructor creates a custom query response from a valid json string.
85 * A&AI Custom Query response JSON string
87 public AaiCqResponse(String jsonString) {
89 // Read JSON String and add all AaiObjects
90 JSONObject responseObj = new JSONObject(jsonString);
91 JSONArray resultsArray = new JSONArray();
92 if (responseObj.has("results")) {
93 resultsArray = (JSONArray) responseObj.get("results");
95 for (int i = 0; i < resultsArray.length(); i++) {
96 // Object is a vserver
97 if (resultsArray.getJSONObject(i).has("vserver")) {
99 // Create the StreamSource by creating StringReader using the
101 StreamSource json = new StreamSource(new StringReader(
102 resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
104 // Getting the vserver pojo again from the json
105 Vserver vserver = this.getAaiObject(json, Vserver.class);
106 this.inventoryResponseItems.add(vserver);
109 // Object is a Generic VNF
110 if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
111 // Create the StreamSource by creating StringReader using the
113 StreamSource json = new StreamSource(new StringReader(
114 resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
116 // Getting the generic vnf pojo again from the json
117 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
119 this.inventoryResponseItems.add(genericVnf);
122 // Object is a Service Instance
123 if (resultsArray.getJSONObject(i).has("service-instance")) {
125 // Create the StreamSource by creating StringReader using the
127 StreamSource json = new StreamSource(new StringReader(
128 resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
130 // Getting the employee pojo again from the json
131 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
133 this.inventoryResponseItems.add(serviceInstance);
136 // Object is a VF Module
137 if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
138 // Create the StreamSource by creating StringReader using the
140 StreamSource json = new StreamSource(new StringReader(
141 resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
143 // Getting the vf module pojo again from the json
144 VfModule vfModule = this.getAaiObject(json, VfModule.class);
146 this.inventoryResponseItems.add(vfModule);
149 // Object is a CloudRegion
150 if (resultsArray.getJSONObject(i).has("cloud-region")) {
151 // Create the StreamSource by creating StringReader using the
153 StreamSource json = new StreamSource(new StringReader(
154 resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
156 // Getting the cloud region pojo again from the json
157 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
159 this.inventoryResponseItems.add(cloudRegion);
162 // Object is a Tenant
163 if (resultsArray.getJSONObject(i).has("tenant")) {
164 // Create the StreamSource by creating StringReader using the
166 StreamSource json = new StreamSource(new StringReader(
167 resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
169 // Getting the tenant pojo again from the json
170 Tenant tenant = this.getAaiObject(json, Tenant.class);
172 this.inventoryResponseItems.add(tenant);
175 // Object is a ModelVer
176 if (resultsArray.getJSONObject(i).has("model-ver")) {
177 // Create the StreamSource by creating StringReader using the
179 StreamSource json = new StreamSource(new StringReader(
180 resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
182 // Getting the ModelVer pojo again from the json
183 ModelVer modelVer = this.getAaiObject(json, ModelVer.class);
185 this.inventoryResponseItems.add(modelVer);
192 private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
194 return unmarshaller.unmarshal(json, classOfResponse).getValue();
195 } catch (JAXBException e) {
196 LOGGER.error("JAXBCOntext error", e);
201 public List<Object> getInventoryResponseItems() {
202 return inventoryResponseItems;
205 public void setInventoryResponseItems(List<Object> inventoryResponseItems) {
206 this.inventoryResponseItems = inventoryResponseItems;
210 * Get list of A&AI objects in the custom query.
212 * @param classOfResponse
213 * Class of the type of A&AI objects to be returned
214 * @return List A&AI objects matching the class
216 @SuppressWarnings("unchecked")
217 public <T> List<T> getItemListByType(Class<T> classOfResponse) {
218 List<T> returnItemList = new ArrayList<>();
219 for (Object i : this.inventoryResponseItems) {
220 if (i.getClass() == classOfResponse) {
221 returnItemList.add((T) i);
224 return returnItemList;
229 * Get Service Instance.
231 * @return Service Instance
233 public ServiceInstance getServiceInstance() {
234 ServiceInstance serviceInstance = null;
235 for (Object i : this.inventoryResponseItems) {
236 if (i.getClass() == ServiceInstance.class) {
237 serviceInstance = (ServiceInstance) i;
240 return serviceInstance;
249 public Tenant getDefaultTenant() {
250 Tenant tenant = null;
251 for (Object i : this.inventoryResponseItems) {
252 if (i.getClass() == Tenant.class) {
263 * @return Cloud Region
265 public CloudRegion getDefaultCloudRegion() {
266 CloudRegion cloudRegion = null;
267 for (Object i : this.inventoryResponseItems) {
268 if (i.getClass() == CloudRegion.class) {
269 cloudRegion = (CloudRegion) i;
277 * Get Generic Vnfs in the custom query.
279 * @return List of generic Vnf
281 public List<GenericVnf> getGenericVnfs() {
282 List<GenericVnf> genericVnfList = new ArrayList<>();
283 for (Object i : this.inventoryResponseItems) {
284 if (i.getClass() == GenericVnf.class) {
285 genericVnfList.add((GenericVnf) i);
288 return genericVnfList;
293 * Returns a generic Vnf matching vnf name.
296 * Name of the vnf to match
297 * @return generic Vnf
299 public GenericVnf getGenericVnfByVnfName(String vnfName) {
300 List<GenericVnf> genericVnfList = new ArrayList<>();
301 GenericVnf genericVnf = null;
302 for (Object i : this.inventoryResponseItems) {
303 if (i.getClass() == GenericVnf.class) {
304 genericVnfList.add((GenericVnf) i);
308 for (GenericVnf genVnf : genericVnfList) {
309 if (vnfName.equals(genVnf.getVnfName())) {
319 * Returns a generic Vnf matching model invariant ID.
321 * @param modelInvariantId
322 * Name of the vnf to match
323 * @return generic Vnf
325 public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
326 List<GenericVnf> genericVnfList = new ArrayList<>();
327 GenericVnf genericVnf = null;
328 for (Object i : this.inventoryResponseItems) {
329 if (i.getClass() == GenericVnf.class) {
330 genericVnfList.add((GenericVnf) i);
334 for (GenericVnf genVnf : genericVnfList) {
335 if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
345 * Returns a generic Vnf of a given VF Module ID.
347 * @param vfModuleModelInvariantId
348 * of the vf module for which vnf is to be returned
349 * @return generic Vnf
351 public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
352 List<GenericVnf> genericVnfList = this.getGenericVnfs();
354 for (GenericVnf genVnf : genericVnfList) {
355 // Iterate through all the vfModules of that generic Vnf
356 for (VfModule vfMod : genVnf.getVfModules().getVfModule()) {
357 if (vfMod.getModelInvariantId() != null
358 && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) {
367 * Get the generic vnf associated with the vserver in the custom query.
369 * @return Generic VNF
371 public GenericVnf getDefaultGenericVnf() {
372 GenericVnf genericVnf = null;
374 // Get the vserver associated with the query
375 Vserver vserver = this.getVserver();
377 // Get the relationships of the vserver
378 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
380 // Find the relationship of the genericVNF
381 String genericVnfId = "";
382 List<RelationshipData> relationshipData = null;
384 // Iterate through the list of relationships and get generic vnf
386 for (Relationship r : relations) {
387 // Get the name of generic-vnf related to this server
388 if (GENERIC_VNF.equals(r.getRelatedTo())) {
389 relationshipData = r.getRelationshipData();
393 // Iterate through relationship data, and get vnf-id
394 for (RelationshipData rd : relationshipData) {
395 // Get the id of the generic-vnf
396 if ("generic-vnf.vnf-id".equals(rd.getRelationshipKey())) {
397 genericVnfId = rd.getRelationshipValue();
401 // Get the list of generic vnfs
402 List<GenericVnf> genericVnfList = this.getGenericVnfs();
404 for (GenericVnf genVnf : genericVnfList) {
405 if (genericVnfId.equals(genVnf.getVnfId())) {
414 * Get Vf Module associated with the vserver in the custom query.
418 public VfModule getDefaultVfModule() {
419 GenericVnf genericVnf = null;
420 VfModule vfModule = null;
422 // Get the vserver associated with the query
423 Vserver vserver = this.getVserver();
425 // Get the relationships of the vserver
426 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
428 // Find the relationship of VfModule
429 String vfModuleId = "";
430 List<RelationshipData> relationshipData = null;
432 // Iterate through the list of relationships and get vf module
434 for (Relationship r : relations) {
435 // Get relationship data of vfmodule related to this server
436 if (VF_MODULE.equals(r.getRelatedTo())) {
437 relationshipData = r.getRelationshipData();
441 // Iterate through relationship data, and get vf-module-id
442 for (RelationshipData rd : relationshipData) {
443 // Get the id of the vf-module
444 if ("vf-module.vf-module-id".equals(rd.getRelationshipKey())) {
445 vfModuleId = rd.getRelationshipValue();
449 // Get the generic VNF associated with this vserver query
450 genericVnf = this.getDefaultGenericVnf();
452 // Get the list of VFmodules associated with this generic Vnf
453 List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
455 for (VfModule vfMod : vfModuleList) {
456 if (vfModuleId.equals(vfMod.getVfModuleId())) {
465 * Get vf modules in the custom query.
467 * @return List of VfModule
469 public List<VfModule> getAllVfModules() {
470 List<VfModule> vfModuleList = new ArrayList<>();
472 for (GenericVnf genVnf : this.getGenericVnfs()) {
473 vfModuleList.addAll(genVnf.getVfModules().getVfModule());
480 * Get Vf Module matching a specific VF module name.
484 public VfModule getVfModuleByVfModuleName(String vfModuleName) {
485 VfModule vfModule = null;
487 for (VfModule vfMod : this.getAllVfModules()) {
488 if (vfModuleName.equals(vfMod.getVfModuleName())) {
497 * Get Vf Module matching a specific VF model invariant ID.
501 public VfModule getVfModuleByVfModelInvariantId(String vfModelInvariantId) {
502 VfModule vfModule = null;
504 for (VfModule vfMod : this.getAllVfModules()) {
505 if (vfMod.getModelInvariantId() != null
506 && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
515 * Get verver in the custom query.
519 public Vserver getVserver() {
520 Vserver vserver = null;
522 while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
525 vserver = (Vserver) this.inventoryResponseItems.get(index);
531 * Get Model Versions in the custom query.
533 * @return List of model Versions
535 public List<ModelVer> getAllModelVer() {
536 List<ModelVer> modelVerList = new ArrayList<>();
537 for (Object i : this.inventoryResponseItems) {
538 if (i.getClass() == ModelVer.class) {
539 modelVerList.add((ModelVer) i);
546 * Get ModelVer matching a specific version id.
550 public ModelVer getModelVerByVersionId(String versionId) {
551 ModelVer modelVer = null;
553 for (ModelVer modVersion : this.getAllModelVer()) {
554 if (versionId.equals(modVersion.getModelVersionId())) {
555 modelVer = modVersion;
563 * Get the count of vfModules matching customizationId, InvariantId and
567 * ModelCustomizationId
572 * @return Returns the count of vf modules
574 public int getVfModuleCount(String custId, String invId, String verId) {
575 List<VfModule> vfModuleList = this.getAllVfModules();
577 for (VfModule vfModule : vfModuleList) {
578 if (vfModule.getModelCustomizationId() != null && vfModule.getModelInvariantId() != null
579 && vfModule.getModelVersionId() != null) {
580 if (vfModule.getModelCustomizationId().equals(custId)
581 && vfModule.getModelInvariantId().equals(invId)
582 && vfModule.getModelVersionId().equals(verId)) {