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
66 jaxbContext = JAXBContextFactory.createContext(new Class[] {
71 ServiceInstance.class,
76 unmarshaller = jaxbContext.createUnmarshaller();
77 } catch (JAXBException e) {
78 LOGGER.error("Could not initialize JAXBContext", e);
79 LOGGER.info("Problem initiatlizing JAXBContext", e);
83 @SerializedName("results")
84 private List<Object> inventoryResponseItems = new LinkedList<>();
87 * Constructor creates a custom query response from a valid json string.
89 * @param jsonString A&AI Custom Query response JSON string
91 public AaiCqResponse(String jsonString) {
93 // Read JSON String and add all AaiObjects
94 JSONObject responseObj = new JSONObject(jsonString);
95 JSONArray resultsArray = new JSONArray();
96 if (responseObj.has("results")) {
97 resultsArray = (JSONArray) responseObj.get("results");
99 for (int i = 0; i < resultsArray.length(); i++) {
100 // Object is a vserver
101 if (resultsArray.getJSONObject(i).has("vserver")) {
103 // Create the StreamSource by creating StringReader using the
105 StreamSource json = new StreamSource(
106 new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
108 // Getting the vserver pojo again from the json
109 Vserver vserver = this.getAaiObject(json, Vserver.class);
110 this.inventoryResponseItems.add(vserver);
113 // Object is a Generic VNF
114 if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
115 // Create the StreamSource by creating StringReader using the
117 StreamSource json = new StreamSource(
118 new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
120 // Getting the generic vnf pojo again from the json
121 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
123 this.inventoryResponseItems.add(genericVnf);
126 // Object is a Service Instance
127 if (resultsArray.getJSONObject(i).has("service-instance")) {
129 // Create the StreamSource by creating StringReader using the
131 StreamSource json = new StreamSource(
132 new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
134 // Getting the employee pojo again from the json
135 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
137 this.inventoryResponseItems.add(serviceInstance);
140 // Object is a VF Module
141 if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
142 // Create the StreamSource by creating StringReader using the
144 StreamSource json = new StreamSource(
145 new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
147 // Getting the vf module pojo again from the json
148 VfModule vfModule = this.getAaiObject(json, VfModule.class);
150 this.inventoryResponseItems.add(vfModule);
153 // Object is a CloudRegion
154 if (resultsArray.getJSONObject(i).has("cloud-region")) {
155 // Create the StreamSource by creating StringReader using the
157 StreamSource json = new StreamSource(
158 new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
160 // Getting the cloud region pojo again from the json
161 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
163 this.inventoryResponseItems.add(cloudRegion);
166 // Object is a Tenant
167 if (resultsArray.getJSONObject(i).has("tenant")) {
168 // Create the StreamSource by creating StringReader using the
170 StreamSource json = new StreamSource(
171 new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
173 // Getting the tenant pojo again from the json
174 Tenant tenant = this.getAaiObject(json, Tenant.class);
176 this.inventoryResponseItems.add(tenant);
179 // Object is a ModelVer
180 if (resultsArray.getJSONObject(i).has("model-ver")) {
181 // Create the StreamSource by creating StringReader using the
183 StreamSource json = new StreamSource(
184 new StringReader(resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
186 // Getting the ModelVer pojo again from the json
187 ModelVer modelVer = this.getAaiObject(json, ModelVer.class);
189 this.inventoryResponseItems.add(modelVer);
196 private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
198 return unmarshaller.unmarshal(json, classOfResponse).getValue();
199 } catch (JAXBException e) {
200 LOGGER.error("JAXBCOntext error", e);
205 public List<Object> getInventoryResponseItems() {
206 return inventoryResponseItems;
209 public void setInventoryResponseItems(List<Object> inventoryResponseItems) {
210 this.inventoryResponseItems = inventoryResponseItems;
214 * Get list of A&AI objects in the custom query.
216 * @param classOfResponse Class of the type of A&AI objects to be returned
217 * @return List A&AI objects matching the class
219 @SuppressWarnings("unchecked")
220 public <T> List<T> getItemListByType(Class<T> classOfResponse) {
221 List<T> returnItemList = new ArrayList<>();
222 for (Object i : this.inventoryResponseItems) {
223 if (i.getClass() == classOfResponse) {
224 returnItemList.add((T) i);
227 return returnItemList;
232 * Get Service Instance.
234 * @return Service Instance
236 public ServiceInstance getServiceInstance() {
237 ServiceInstance serviceInstance = null;
238 for (Object i : this.inventoryResponseItems) {
239 if (i.getClass() == ServiceInstance.class) {
240 serviceInstance = (ServiceInstance) i;
243 return serviceInstance;
252 public Tenant getDefaultTenant() {
253 Tenant tenant = null;
254 for (Object i : this.inventoryResponseItems) {
255 if (i.getClass() == Tenant.class) {
266 * @return Cloud Region
268 public CloudRegion getDefaultCloudRegion() {
269 CloudRegion cloudRegion = null;
270 for (Object i : this.inventoryResponseItems) {
271 if (i.getClass() == CloudRegion.class) {
272 cloudRegion = (CloudRegion) i;
280 * Get Generic Vnfs in the custom query.
282 * @return List of generic Vnf
284 public List<GenericVnf> getGenericVnfs() {
285 List<GenericVnf> genericVnfList = new ArrayList<>();
286 for (Object i : this.inventoryResponseItems) {
287 if (i.getClass() == GenericVnf.class) {
288 genericVnfList.add((GenericVnf) i);
291 return genericVnfList;
296 * Returns a generic Vnf matching vnf name.
298 * @param vnfName Name of the vnf to match
299 * @return generic Vnf
301 public GenericVnf getGenericVnfByVnfName(String vnfName) {
302 List<GenericVnf> genericVnfList = new ArrayList<>();
303 GenericVnf genericVnf = null;
304 for (Object i : this.inventoryResponseItems) {
305 if (i.getClass() == GenericVnf.class) {
306 genericVnfList.add((GenericVnf) i);
310 for (GenericVnf genVnf : genericVnfList) {
311 if (vnfName.equals(genVnf.getVnfName())) {
321 * Returns a generic Vnf matching model invariant ID.
323 * @param modelInvariantId Name of the vnf to match
324 * @return generic Vnf
326 public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
327 List<GenericVnf> genericVnfList = new ArrayList<>();
328 GenericVnf genericVnf = null;
329 for (Object i : this.inventoryResponseItems) {
330 if (i.getClass() == GenericVnf.class) {
331 genericVnfList.add((GenericVnf) i);
335 for (GenericVnf genVnf : genericVnfList) {
336 if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
346 * Returns a generic Vnf of a given VF Module ID.
348 * @param vfModuleModelInvariantId 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 && vfModelInvariantId.equals(vfMod.getModelInvariantId())) {
514 * Get verver in the custom query.
518 public Vserver getVserver() {
519 Vserver vserver = null;
521 while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
524 vserver = (Vserver) this.inventoryResponseItems.get(index);
530 * Get Model Versions in the custom query.
532 * @return List of model Versions
534 public List<ModelVer> getAllModelVer() {
535 List<ModelVer> modelVerList = new ArrayList<>();
536 for (Object i : this.inventoryResponseItems) {
537 if (i.getClass() == ModelVer.class) {
538 modelVerList.add((ModelVer) i);
545 * Get ModelVer matching a specific version id.
549 public ModelVer getModelVerByVersionId(String versionId) {
550 ModelVer modelVer = null;
552 for (ModelVer modVersion : this.getAllModelVer()) {
553 if (versionId.equals(modVersion.getModelVersionId())) {
554 modelVer = modVersion;
562 * Get the count of vfModules matching customizationId, InvariantId and VersionId.
564 * @param custId ModelCustomizationId
565 * @param invId ModelInvariantId
566 * @param verId ModelVersionId
567 * @return Returns the count of vf modules
569 public int getVfModuleCount(String custId, String invId, String verId) {
570 List<VfModule> vfModuleList = this.getAllVfModules();
572 for (VfModule vfModule : vfModuleList) {
573 if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
574 || vfModule.getModelVersionId() == null) {
578 if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
579 && vfModule.getModelVersionId().equals(verId)) {