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.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 private static final String GENERIC_VNF = "generic-vnf";
54 private static final String VF_MODULE = "vf-module";
55 private static final Logger LOGGER = LoggerFactory.getLogger(AaiCqResponse.class);
56 private static JAXBContext jaxbContext;
57 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
67 jaxbContext = JAXBContextFactory.createContext(new Class[] {
72 ServiceInstance.class,
77 unmarshaller = jaxbContext.createUnmarshaller();
78 } catch (JAXBException e) {
79 LOGGER.error("Could not initialize JAXBContext", e);
80 LOGGER.info("Problem initiatlizing JAXBContext", e);
84 @SerializedName("results")
85 private List<Serializable> inventoryResponseItems = new LinkedList<>();
88 * Constructor creates a custom query response from a valid json string.
90 * @param jsonString A&AI Custom Query response JSON string
92 public AaiCqResponse(String jsonString) {
94 // Read JSON String and add all AaiObjects
95 JSONObject responseObj = new JSONObject(jsonString);
96 JSONArray resultsArray = new JSONArray();
97 if (responseObj.has("results")) {
98 resultsArray = (JSONArray) responseObj.get("results");
100 for (int i = 0; i < resultsArray.length(); i++) {
101 // Object is a vserver
102 if (resultsArray.getJSONObject(i).has("vserver")) {
104 // Create the StreamSource by creating StringReader using the
106 StreamSource json = new StreamSource(
107 new StringReader(resultsArray.getJSONObject(i).getJSONObject("vserver").toString()));
109 // Getting the vserver pojo again from the json
110 Vserver vserver = this.getAaiObject(json, Vserver.class);
111 this.inventoryResponseItems.add(vserver);
114 // Object is a Generic VNF
115 if (resultsArray.getJSONObject(i).has(GENERIC_VNF)) {
116 // Create the StreamSource by creating StringReader using the
118 StreamSource json = new StreamSource(
119 new StringReader(resultsArray.getJSONObject(i).getJSONObject(GENERIC_VNF).toString()));
121 // Getting the generic vnf pojo again from the json
122 GenericVnf genericVnf = this.getAaiObject(json, GenericVnf.class);
124 this.inventoryResponseItems.add(genericVnf);
127 // Object is a Service Instance
128 if (resultsArray.getJSONObject(i).has("service-instance")) {
130 // Create the StreamSource by creating StringReader using the
132 StreamSource json = new StreamSource(
133 new StringReader(resultsArray.getJSONObject(i).getJSONObject("service-instance").toString()));
135 // Getting the employee pojo again from the json
136 ServiceInstance serviceInstance = this.getAaiObject(json, ServiceInstance.class);
138 this.inventoryResponseItems.add(serviceInstance);
141 // Object is a VF Module
142 if (resultsArray.getJSONObject(i).has(VF_MODULE)) {
143 // Create the StreamSource by creating StringReader using the
145 StreamSource json = new StreamSource(
146 new StringReader(resultsArray.getJSONObject(i).getJSONObject(VF_MODULE).toString()));
148 // Getting the vf module pojo again from the json
149 VfModule vfModule = this.getAaiObject(json, VfModule.class);
151 this.inventoryResponseItems.add(vfModule);
154 // Object is a CloudRegion
155 if (resultsArray.getJSONObject(i).has("cloud-region")) {
156 // Create the StreamSource by creating StringReader using the
158 StreamSource json = new StreamSource(
159 new StringReader(resultsArray.getJSONObject(i).getJSONObject("cloud-region").toString()));
161 // Getting the cloud region pojo again from the json
162 CloudRegion cloudRegion = this.getAaiObject(json, CloudRegion.class);
164 this.inventoryResponseItems.add(cloudRegion);
167 // Object is a Tenant
168 if (resultsArray.getJSONObject(i).has("tenant")) {
169 // Create the StreamSource by creating StringReader using the
171 StreamSource json = new StreamSource(
172 new StringReader(resultsArray.getJSONObject(i).getJSONObject("tenant").toString()));
174 // Getting the tenant pojo again from the json
175 Tenant tenant = this.getAaiObject(json, Tenant.class);
177 this.inventoryResponseItems.add(tenant);
180 // Object is a ModelVer
181 if (resultsArray.getJSONObject(i).has("model-ver")) {
182 // Create the StreamSource by creating StringReader using the
184 StreamSource json = new StreamSource(
185 new StringReader(resultsArray.getJSONObject(i).getJSONObject("model-ver").toString()));
187 // Getting the ModelVer pojo again from the json
188 ModelVer modelVer = this.getAaiObject(json, ModelVer.class);
190 this.inventoryResponseItems.add(modelVer);
197 private <T> T getAaiObject(StreamSource json, final Class<T> classOfResponse) {
199 return unmarshaller.unmarshal(json, classOfResponse).getValue();
200 } catch (JAXBException e) {
201 LOGGER.error("JAXBCOntext error", e);
206 public List<Serializable> getInventoryResponseItems() {
207 return inventoryResponseItems;
210 public void setInventoryResponseItems(List<Serializable> inventoryResponseItems) {
211 this.inventoryResponseItems = inventoryResponseItems;
215 * Get list of A&AI objects in the custom query.
217 * @param classOfResponse Class of the type of A&AI objects to be returned
218 * @return List A&AI objects matching the class
220 @SuppressWarnings("unchecked")
221 public <T> List<T> getItemListByType(Class<T> classOfResponse) {
222 List<T> returnItemList = new ArrayList<>();
223 for (Serializable i : this.inventoryResponseItems) {
224 if (i.getClass() == classOfResponse) {
225 returnItemList.add((T) i);
228 return returnItemList;
233 * Get Service Instance.
235 * @return Service Instance
237 public ServiceInstance getServiceInstance() {
238 ServiceInstance serviceInstance = null;
239 for (Serializable i : this.inventoryResponseItems) {
240 if (i.getClass() == ServiceInstance.class) {
241 serviceInstance = (ServiceInstance) i;
244 return serviceInstance;
253 public Tenant getDefaultTenant() {
254 Tenant tenant = null;
255 for (Serializable i : this.inventoryResponseItems) {
256 if (i.getClass() == Tenant.class) {
267 * @return Cloud Region
269 public CloudRegion getDefaultCloudRegion() {
270 CloudRegion cloudRegion = null;
271 for (Serializable i : this.inventoryResponseItems) {
272 if (i.getClass() == CloudRegion.class) {
273 cloudRegion = (CloudRegion) i;
281 * Get Generic Vnfs in the custom query.
283 * @return List of generic Vnf
285 public List<GenericVnf> getGenericVnfs() {
286 List<GenericVnf> genericVnfList = new ArrayList<>();
287 for (Serializable i : this.inventoryResponseItems) {
288 if (i.getClass() == GenericVnf.class) {
289 genericVnfList.add((GenericVnf) i);
292 return genericVnfList;
297 * Returns a generic Vnf matching vnf name.
299 * @param vnfName Name of the vnf to match
300 * @return generic Vnf
302 public GenericVnf getGenericVnfByVnfName(String vnfName) {
303 List<GenericVnf> genericVnfList = new ArrayList<>();
304 GenericVnf genericVnf = null;
305 for (Serializable i : this.inventoryResponseItems) {
306 if (i.getClass() == GenericVnf.class) {
307 genericVnfList.add((GenericVnf) i);
311 for (GenericVnf genVnf : genericVnfList) {
312 if (vnfName.equals(genVnf.getVnfName())) {
322 * Returns a generic Vnf matching model invariant ID.
324 * @param modelInvariantId Name of the vnf to match
325 * @return generic Vnf
327 public GenericVnf getGenericVnfByModelInvariantId(String modelInvariantId) {
328 List<GenericVnf> genericVnfList = new ArrayList<>();
329 GenericVnf genericVnf = null;
330 for (Serializable i : this.inventoryResponseItems) {
331 if (i.getClass() == GenericVnf.class) {
332 genericVnfList.add((GenericVnf) i);
336 for (GenericVnf genVnf : genericVnfList) {
337 if (modelInvariantId.equals(genVnf.getModelInvariantId())) {
347 * Returns a generic Vnf of a given VF Module ID.
349 * @param vfModuleModelInvariantId of the vf module for which vnf is to be returned
350 * @return generic Vnf
352 public GenericVnf getGenericVnfByVfModuleModelInvariantId(String vfModuleModelInvariantId) {
353 List<GenericVnf> genericVnfList = this.getGenericVnfs();
355 for (GenericVnf genVnf : genericVnfList) {
356 // Iterate through all the vfModules of that generic Vnf
357 for (VfModule vfMod : genVnf.getVfModules().getVfModule()) {
358 if (vfMod.getModelInvariantId() != null
359 && 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
387 for (Relationship r : relations) {
388 // Get the name of generic-vnf related to this server
389 if (GENERIC_VNF.equals(r.getRelatedTo())) {
390 relationshipData = r.getRelationshipData();
394 // Iterate through relationship data, and get vnf-id
395 for (RelationshipData rd : relationshipData) {
396 // Get the id of the generic-vnf
397 if ("generic-vnf.vnf-id".equals(rd.getRelationshipKey())) {
398 genericVnfId = rd.getRelationshipValue();
402 // Get the list of generic vnfs
403 List<GenericVnf> genericVnfList = this.getGenericVnfs();
405 for (GenericVnf genVnf : genericVnfList) {
406 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
435 for (Relationship r : relations) {
436 // Get relationship data of vfmodule related to this server
437 if (VF_MODULE.equals(r.getRelatedTo())) {
438 relationshipData = r.getRelationshipData();
442 // Iterate through relationship data, and get vf-module-id
443 for (RelationshipData rd : relationshipData) {
444 // Get the id of the vf-module
445 if ("vf-module.vf-module-id".equals(rd.getRelationshipKey())) {
446 vfModuleId = rd.getRelationshipValue();
450 // Get the generic VNF associated with this vserver query
451 genericVnf = this.getDefaultGenericVnf();
453 // Get the list of VFmodules associated with this generic Vnf
454 List<VfModule> vfModuleList = genericVnf.getVfModules().getVfModule();
456 for (VfModule vfMod : vfModuleList) {
457 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())) {
498 * Get Vf Module matching a specific VF model invariant ID.
502 public VfModule getVfModuleByVfModelInvariantId(String vfModelInvariantId) {
503 VfModule vfModule = null;
505 for (VfModule vfMod : this.getAllVfModules()) {
506 if (vfMod.getModelInvariantId() != null && 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 (Serializable 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 VersionId.
565 * @param custId ModelCustomizationId
566 * @param invId ModelInvariantId
567 * @param verId ModelVersionId
568 * @return Returns the count of vf modules
570 public int getVfModuleCount(String custId, String invId, String verId) {
571 List<VfModule> vfModuleList = this.getAllVfModules();
573 for (VfModule vfModule : vfModuleList) {
574 if (vfModule.getModelCustomizationId() == null || vfModule.getModelInvariantId() == null
575 || vfModule.getModelVersionId() == null) {
579 if (vfModule.getModelCustomizationId().equals(custId) && vfModule.getModelInvariantId().equals(invId)
580 && vfModule.getModelVersionId().equals(verId)) {