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;
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
66 jaxbContext = JAXBContextFactory.createContext(new Class[] {Vserver.class, GenericVnf.class, VfModule.class,
67 CloudRegion.class, ServiceInstance.class, Tenant.class, ModelVer.class}, properties);
68 unmarshaller = jaxbContext.createUnmarshaller();
69 } catch (JAXBException e) {
70 LOGGER.error("Could not initialize JAXBContext", e);
71 LOGGER.info("Problem initiatlizing JAXBContext", e);
75 @SerializedName("results")
76 private List<Object> inventoryResponseItems = new LinkedList<>();
80 * Constructor creates a custom query response from a valid json string.
82 * @param jsonString A&AI Custom Query response JSON string
84 public AaiCqResponse(String jsonString) {
86 // Read JSON String and add all AaiObjects
87 JSONObject responseObj = new JSONObject(jsonString);
88 JSONArray resultsArray = new JSONArray();
89 if (responseObj.has("results")) {
90 resultsArray = (JSONArray) responseObj.get("results");
92 for (int i = 0; i < resultsArray.length(); i++) {
93 // Object is a vserver
94 if (resultsArray.getJSONObject(i).has("vserver")) {
96 // Create the StreamSource by creating StringReader using the
98 StreamSource json = new StreamSource(
99 new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
101 // Getting the vserver pojo again from the json
102 Vserver vserver = this.getAaiObject(json, Vserver.class);
103 this.inventoryResponseItems.add(vserver);
106 // Object is a Generic VNF
107 if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
108 // Create the StreamSource by creating StringReader using the
110 StreamSource json = new StreamSource(
111 new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
113 // Getting the generic vnf pojo again from the json
114 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
116 this.inventoryResponseItems.add(genericVnf);
119 // Object is a Service Instance
120 if (resultsArray.getJSONObject(i).has("service-instance")) {
122 // Create the StreamSource by creating StringReader using the
124 StreamSource json = new StreamSource(
125 new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
127 // Getting the employee pojo again from the json
128 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
130 this.inventoryResponseItems.add(serviceInstance);
133 // Object is a VF Module
134 if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
135 // Create the StreamSource by creating StringReader using the
137 StreamSource json = new StreamSource(
138 new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
140 // Getting the vf module pojo again from the json
141 VfModule vfModule = this.getAaiObject(json, VfModule.class);
143 this.inventoryResponseItems.add(vfModule);
146 // Object is a CloudRegion
147 if (resultsArray.getJSONObject(i).has("cloud-region")) {
148 // Create the StreamSource by creating StringReader using the
150 StreamSource json = new StreamSource(
151 new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
153 // Getting the cloud region pojo again from the json
154 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
156 this.inventoryResponseItems.add(cloudRegion);
159 // Object is a Tenant
160 if (resultsArray.getJSONObject(i).has("tenant")) {
161 // Create the StreamSource by creating StringReader using the
163 StreamSource json = new StreamSource(
164 new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
166 // Getting the tenant pojo again from the json
167 Tenant tenant = this.getAaiObject(json, Tenant.class);
169 this.inventoryResponseItems.add(tenant);
172 // Object is a ModelVer
173 if (resultsArray.getJSONObject(i).has("model-ver")) {
174 // Create the StreamSource by creating StringReader using the
176 StreamSource json = new StreamSource(
177 new StringReader(resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
179 // Getting the ModelVer pojo again from the json
180 ModelVer modelVer = this.getAaiObject(json, ModelVer.class);
182 this.inventoryResponseItems.add(modelVer);
193 private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
195 return unmarshaller.unmarshal(json, classOfResponse).getValue();
196 } catch (JAXBException e) {
197 LOGGER.error("JAXBCOntext error", e);
202 public List<Object> getInventoryResponseItems() {
203 return inventoryResponseItems;
206 public void setInventoryResponseItems(List<Object> inventoryResponseItems) {
207 this.inventoryResponseItems = inventoryResponseItems;
211 * Get list of A&AI objects in the custom query.
213 * @param classOfResponse 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;
294 * Returns a generic Vnf matching vnf name.
296 * @param vnfName 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 Name of the vnf to match
322 * @return generic Vnf
324 public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
325 List<GenericVnf> genericVnfList = new ArrayList<>();
326 GenericVnf genericVnf = null;
327 for (Object i : this.inventoryResponseItems) {
328 if (i.getClass() == GenericVnf.class) {
329 genericVnfList.add((GenericVnf) i);
333 for (GenericVnf genVnf : genericVnfList) {
334 if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
345 * Returns a generic Vnf of a given VF Module ID.
347 * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
348 * @return generic Vnf
350 public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
351 List<GenericVnf> genericVnfList = this.getGenericVnfs();
353 for (GenericVnf genVnf : genericVnfList) {
354 // Iterate through all the vfModules of that generic Vnf
355 for (VfModule vfMod : genVnf.getVfModules().getVfModule()) {
356 if (vfMod.getModelInvariantId() != null
357 && vfMod.getModelInvariantId().equals(vfModuleModelInvariantId)) {
368 * Get the generic vnf associated with the vserver in the custom query.
370 * @return Generic VNF
372 public GenericVnf getDefaultGenericVnf() {
373 GenericVnf genericVnf = null;
375 // Get the vserver associated with the query
376 Vserver vserver = this.getVserver();
378 // Get the relationships of the vserver
379 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
381 // Find the relationship of the genericVNF
382 String genericVnfId = "";
383 List<RelationshipData> relationshipData = null;
385 // Iterate through the list of relationships and get generic vnf relationship data
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())) {
415 * Get Vf Module associated with the vserver in the custom query.
419 public VfModule getDefaultVfModule() {
420 GenericVnf genericVnf = null;
421 VfModule vfModule = null;
423 // Get the vserver associated with the query
424 Vserver vserver = this.getVserver();
426 // Get the relationships of the vserver
427 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
429 // Find the relationship of VfModule
430 String vfModuleId = "";
431 List<RelationshipData> relationshipData = null;
433 // Iterate through the list of relationships and get vf module relationship data
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())) {
466 * Get vf modules in the custom query.
468 * @return List of VfModule
470 public List<VfModule> getAllVfModules() {
471 List<VfModule> vfModuleList = new ArrayList<>();
473 for (GenericVnf genVnf : this.getGenericVnfs()) {
474 vfModuleList.addAll(genVnf.getVfModules().getVfModule());
481 * Get Vf Module matching a specific VF module name.
485 public VfModule getVfModuleByVfModuleName(String vfModuleName) {
486 VfModule vfModule = null;
488 for (VfModule vfMod : this.getAllVfModules()) {
489 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 (Object i : this.inventoryResponseItems) {
539 if (i.getClass() == ModelVer.class) {
540 modelVerList.add((ModelVer) i);
549 * Get ModelVer matching a specific version id.
553 public ModelVer getModelVerByVersionId(String versionId) {
554 ModelVer modelVer = null;
556 for (ModelVer modVersion : this.getAllModelVer()) {
557 if (versionId.equals(modVersion.getModelVersionId())) {
558 modelVer = modVersion;