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.Relationship;
41 import org.onap.aai.domain.yang.RelationshipData;
42 import org.onap.aai.domain.yang.ServiceInstance;
43 import org.onap.aai.domain.yang.Tenant;
44 import org.onap.aai.domain.yang.VfModule;
45 import org.onap.aai.domain.yang.Vserver;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
49 public class AaiCqResponse {
51 private static final String GENERIC_VNF = "generic-vnf";
52 private static final String VF_MODULE = "vf-module";
53 private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponse.class);
54 private static JAXBContext jaxbContext;
55 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
65 jaxbContext = JAXBContextFactory.createContext(new Class[] {Vserver.class, GenericVnf.class, VfModule.class,
66 CloudRegion.class, ServiceInstance.class, Tenant.class}, properties);
67 unmarshaller = jaxbContext.createUnmarshaller();
68 } catch (JAXBException e) {
69 LOGGER.error("Could not initialize JAXBContext", e);
70 LOGGER.info("Problem initiatlizing JAXBContext", e);
74 @SerializedName("results")
75 private List<Object> inventoryResponseItems = new LinkedList<>();
79 * Constructor creates a custom query response from a valid json string.
81 * @param jsonString A&AI Custom Query response JSON string
83 public AaiCqResponse(String jsonString) {
85 // Read JSON String and add all AaiObjects
86 JSONObject responseObj = new JSONObject(jsonString);
87 JSONArray resultsArray = new JSONArray();
88 if (responseObj.has("results")) {
89 resultsArray = (JSONArray) responseObj.get("results");
91 for (int i = 0; i < resultsArray.length(); i++) {
92 // Object is a vserver
93 if (resultsArray.getJSONObject(i).has("vserver")) {
95 // Create the StreamSource by creating StringReader using the
97 StreamSource json = new StreamSource(
98 new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
100 // Getting the vserver pojo again from the json
101 Vserver vserver = this.getAaiObject(json, Vserver.class);
102 this.inventoryResponseItems.add(vserver);
105 // Object is a Generic VNF
106 if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
107 // Create the StreamSource by creating StringReader using the
109 StreamSource json = new StreamSource(
110 new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
112 // Getting the generic vnf pojo again from the json
113 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
115 this.inventoryResponseItems.add(genericVnf);
118 // Object is a Service Instance
119 if (resultsArray.getJSONObject(i).has("service-instance")) {
121 // Create the StreamSource by creating StringReader using the
123 StreamSource json = new StreamSource(
124 new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
126 // Getting the employee pojo again from the json
127 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
129 this.inventoryResponseItems.add(serviceInstance);
132 // Object is a VF Module
133 if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
134 // Create the StreamSource by creating StringReader using the
136 StreamSource json = new StreamSource(
137 new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
139 // Getting the vf module pojo again from the json
140 VfModule vfModule = this.getAaiObject(json, VfModule.class);
142 this.inventoryResponseItems.add(vfModule);
145 // Object is a CloudRegion
146 if (resultsArray.getJSONObject(i).has("cloud-region")) {
147 // Create the StreamSource by creating StringReader using the
149 StreamSource json = new StreamSource(
150 new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
152 // Getting the cloud region pojo again from the json
153 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
155 this.inventoryResponseItems.add(cloudRegion);
158 // Object is a Tenant
159 if (resultsArray.getJSONObject(i).has("tenant")) {
160 // Create the StreamSource by creating StringReader using the
162 StreamSource json = new StreamSource(
163 new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
165 // Getting the tenant pojo again from the json
166 Tenant tenant = this.getAaiObject(json, Tenant.class);
168 this.inventoryResponseItems.add(tenant);
179 private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
181 return unmarshaller.unmarshal(json, classOfResponse).getValue();
182 } catch (JAXBException e) {
183 LOGGER.error("JAXBCOntext error", e);
188 public List<Object> getInventoryResponseItems() {
189 return inventoryResponseItems;
192 public void setInventoryResponseItems(List<Object> inventoryResponseItems) {
193 this.inventoryResponseItems = inventoryResponseItems;
197 * Get list of A&AI objects in the custom query.
199 * @param classOfResponse Class of the type of A&AI objects to be returned
200 * @return List A&AI objects matching the class
202 @SuppressWarnings("unchecked")
203 public <T> List<T> getItemListByType(Class<T> classOfResponse) {
204 List<T> returnItemList = new ArrayList<>();
205 for (Object i : this.inventoryResponseItems) {
206 if (i.getClass() == classOfResponse) {
207 returnItemList.add((T) i);
210 return returnItemList;
215 * Get Service Instance.
217 * @return Service Instance
219 public ServiceInstance getServiceInstance() {
220 ServiceInstance serviceInstance = null;
221 for (Object i : this.inventoryResponseItems) {
222 if (i.getClass() == ServiceInstance.class) {
223 serviceInstance = (ServiceInstance) i;
226 return serviceInstance;
235 public Tenant getDefaultTenant() {
236 Tenant tenant = null;
237 for (Object i : this.inventoryResponseItems) {
238 if (i.getClass() == Tenant.class) {
249 * @return Cloud Region
251 public CloudRegion getDefaultCloudRegion() {
252 CloudRegion cloudRegion = null;
253 for (Object i : this.inventoryResponseItems) {
254 if (i.getClass() == CloudRegion.class) {
255 cloudRegion = (CloudRegion) i;
263 * Get Generic Vnfs in the custom query.
265 * @return List of generic Vnf
267 public List<GenericVnf> getGenericVnfs() {
268 List<GenericVnf> genericVnfList = new ArrayList<>();
269 for (Object i : this.inventoryResponseItems) {
270 if (i.getClass() == GenericVnf.class) {
271 genericVnfList.add((GenericVnf) i);
274 return genericVnfList;
280 * Returns a generic Vnf matching vnf name.
282 * @param vnfName Name of the vnf to match
283 * @return generic Vnf
285 public GenericVnf getGenericVnfByVnfName(String vnfName) {
286 List<GenericVnf> genericVnfList = new ArrayList<>();
287 GenericVnf genericVnf = null;
288 for (Object i : this.inventoryResponseItems) {
289 if (i.getClass() == GenericVnf.class) {
290 genericVnfList.add((GenericVnf) i);
294 for (GenericVnf genVnf : genericVnfList) {
295 if (vnfName.equals(genVnf.getVnfName())) {
305 * Returns a generic Vnf matching model invariant ID.
307 * @param modelInvariantId Name of the vnf to match
308 * @return generic Vnf
310 public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
311 List<GenericVnf> genericVnfList = new ArrayList<>();
312 GenericVnf genericVnf = null;
313 for (Object i : this.inventoryResponseItems) {
314 if (i.getClass() == GenericVnf.class) {
315 genericVnfList.add((GenericVnf) i);
319 for (GenericVnf genVnf : genericVnfList) {
320 if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
332 * Get the generic vnf associated with the vserver in the custom query.
334 * @return Generic VNF
336 public GenericVnf getDefaultGenericVnf() {
337 GenericVnf genericVnf = null;
339 // Get the vserver associated with the query
340 Vserver vserver = this.getVserver();
342 // Get the relationships of the vserver
343 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
345 // Find the relationship of the genericVNF
346 String genericVnfId = "";
347 List<RelationshipData> relationshipData = null;
349 // Iterate through the list of relationships and get generic vnf relationship data
350 for (Relationship r : relations) {
351 // Get the name of generic-vnf related to this server
352 if (GENERIC_VNF.equals(r.getRelatedTo())) {
353 relationshipData = r.getRelationshipData();
357 // Iterate through relationship data, and get vnf-id
358 for (RelationshipData rd : relationshipData) {
359 // Get the id of the generic-vnf
360 if ("generic-vnf.vnf-id".equals(rd.getRelationshipKey())) {
361 genericVnfId = rd.getRelationshipValue();
365 // Get the list of generic vnfs
366 List<GenericVnf> genericVnfList = this.getGenericVnfs();
368 for (GenericVnf genVnf : genericVnfList) {
369 if (genericVnfId.equals(genVnf.getVnfId())) {
379 * Get Vf Module associated with the vserver in the custom query.
383 public VfModule getDefaultVfModule() {
384 GenericVnf genericVnf = null;
385 VfModule vfModule = null;
387 // Get the vserver associated with the query
388 Vserver vserver = this.getVserver();
390 // Get the relationships of the vserver
391 List<Relationship> relations = vserver.getRelationshipList().getRelationship();
393 // Find the relationship of VfModule
394 String vfModuleId = "";
395 List<RelationshipData> relationshipData = null;
397 // Iterate through the list of relationships and get vf module relationship data
398 for (Relationship r : relations) {
399 // Get relationship data of vfmodule related to this server
400 if (VF_MODULE.equals(r.getRelatedTo())) {
401 relationshipData = r.getRelationshipData();
405 // Iterate through relationship data, and get vf-module-id
406 for (RelationshipData rd : relationshipData) {
407 // Get the id of the vf-module
408 if ("vf-module.vf-module-id".equals(rd.getRelationshipKey())) {
409 vfModuleId = rd.getRelationshipValue();
413 // Get the generic VNF associated with this vserver query
414 genericVnf = this.getDefaultGenericVnf();
416 // Get the list of VFmodules associated with this generic Vnf
417 List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
419 for (VfModule vfMod : vfModuleList) {
420 if (vfModuleId.equals(vfMod.getVfModuleId())) {
430 * Get vf modules in the custom query.
432 * @return List of VfModule
434 public List<VfModule> getAllVfModules() {
435 List<VfModule> vfModuleList = new ArrayList<>();
437 for (GenericVnf genVnf : this.getGenericVnfs()) {
438 vfModuleList.addAll(genVnf.getVfModules().getVfModule());
445 * Get Vf Module matching a specific VF module name.
449 public VfModule getVfModuleByVfModuleName(String vfModuleName) {
450 VfModule vfModule = null;
452 for (VfModule vfMod : this.getAllVfModules()) {
453 if (vfModuleName.equals(vfMod.getVfModuleName())) {
462 * Get verver in the custom query.
466 public Vserver getVserver() {
467 Vserver vserver = null;
469 while (this.inventoryResponseItems.get(index).getClass() != Vserver.class) {
472 vserver = (Vserver) this.inventoryResponseItems.get(index);