2 * Copyright 2017 ZTE Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.holmes.common.aai;
18 import com.alibaba.fastjson.JSON;
19 import com.alibaba.fastjson.JSONArray;
20 import com.alibaba.fastjson.JSONObject;
21 import java.io.IOException;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import org.jvnet.hk2.annotations.Service;
26 import org.onap.holmes.common.aai.entity.RelationshipList;
27 import org.onap.holmes.common.aai.entity.RelationshipList.RelatedToProperty;
28 import org.onap.holmes.common.aai.entity.RelationshipList.Relationship;
29 import org.onap.holmes.common.aai.entity.RelationshipList.RelationshipData;
30 import org.onap.holmes.common.aai.entity.VmEntity;
31 import org.onap.holmes.common.aai.entity.VmResourceLink;
32 import org.onap.holmes.common.aai.entity.VnfEntity;
35 public class AaiResponseUtil {
37 public static final String RELATIONSHIP_LIST = "relationship-list";
39 public List<VmResourceLink> convertJsonToVmResourceLink(String responseJson)
41 List<VmResourceLink> vmResourceLinkList = new ArrayList<>();
42 String resultDataKey = "result-data";
43 JSONObject jsonNode = JSON.parseObject(responseJson);
44 if (jsonNode != null && jsonNode.get(resultDataKey) != null) {
45 JSONArray resultData = jsonNode.getJSONArray(resultDataKey);
46 vmResourceLinkList = convertResultDataList(resultData);
48 return vmResourceLinkList;
51 public VmEntity convertJsonToVmEntity(String responseJson) {
52 JSONObject jsonObject = JSON.parseObject(responseJson);
53 if (jsonObject == null ||jsonObject.isEmpty()) {
56 VmEntity vmEntity = new VmEntity();
57 vmEntity.setInMaint(getBooleanElementByNode(jsonObject, "in-maint"));
58 vmEntity.setClosedLoopDisable(
59 getBooleanElementByNode(jsonObject, "is-closed-loop-disabled"));
60 vmEntity.setProvStatus(getTextElementByNode(jsonObject, "prov-status"));
61 vmEntity.setResourceVersion(getTextElementByNode(jsonObject, "resource-version"));
62 vmEntity.setVserverId(getTextElementByNode(jsonObject, "vserver-id"));
63 vmEntity.setVserverName(getTextElementByNode(jsonObject, "vserver-name"));
64 vmEntity.setVserverName2(getTextElementByNode(jsonObject, "vserver-name2"));
65 vmEntity.setVserverSelflink(getTextElementByNode(jsonObject, "vserver-selflink"));
67 setRelationShips(jsonObject, vmEntity.getRelationshipList());
68 if (vmEntity.getRelationshipList().getRelationships() == null) {
69 vmEntity.getRelationshipList().setRelationships(Collections.emptyList());
74 public VnfEntity convertJsonToVnfEntity(String responseJson) {
75 JSONObject jsonObject = JSON.parseObject(responseJson);
77 if (jsonObject.isEmpty()) {
81 VnfEntity vnfEntity = new VnfEntity();
82 vnfEntity.setInMaint(getBooleanElementByNode(jsonObject, "in-maint"));
83 vnfEntity.setClosedLoopDisabled(
84 getBooleanElementByNode(jsonObject, "is-closed-loop-disabled"));
85 vnfEntity.setOrchestrationStatus(getTextElementByNode(jsonObject, "orchestration-status"));
86 vnfEntity.setProvStatus(getTextElementByNode(jsonObject, "prov-status"));
87 vnfEntity.setResourceVersion(getTextElementByNode(jsonObject, "resource-version"));
88 vnfEntity.setServiceId(getTextElementByNode(jsonObject, "service-id"));
89 vnfEntity.setVnfId(getTextElementByNode(jsonObject, "vnf-id"));
90 vnfEntity.setVnfName(getTextElementByNode(jsonObject, "vnf-name"));
91 vnfEntity.setVnfType(getTextElementByNode(jsonObject, "vnf-type"));
93 setRelationShips(jsonObject, vnfEntity.getRelationshipList());
94 if (vnfEntity.getRelationshipList().getRelationships() == null) {
95 vnfEntity.getRelationshipList().setRelationships(Collections.emptyList());
100 private void setRelationShips(JSONObject jsonObject, RelationshipList relationshipList) {
101 if (jsonObject.get(RELATIONSHIP_LIST) != null) {
102 JSONObject relationshipListNode = jsonObject.getJSONObject(RELATIONSHIP_LIST);
103 String relationship = "relationship";
104 if (relationshipListNode.get(relationship) != null) {
105 JSONArray relationshipNode = relationshipListNode.getJSONArray(relationship);
107 .setRelationships(convertRelationships(relationshipNode));
112 private List<VmResourceLink> convertResultDataList(JSONArray resultData) {
113 List<VmResourceLink> vmResourceLinkList = new ArrayList<>();
114 String resourceLink = "resource-link";
115 String resourceType = "resource-type";
116 for (int i = 0; i < resultData.size(); i++) {
117 JSONObject jsonObject = resultData.getJSONObject(i);
118 if (jsonObject.get(resourceLink) != null
119 && jsonObject.get(resourceType) != null) {
120 VmResourceLink vmResourceLink = new VmResourceLink();
121 vmResourceLink.setResourceLink(getTextElementByNode(jsonObject, resourceLink));
122 vmResourceLink.setResourceType(getTextElementByNode(jsonObject, resourceType));
123 vmResourceLinkList.add(vmResourceLink);
126 return vmResourceLinkList;
129 private List<Relationship> convertRelationships(JSONArray relationshipNode) {
130 List<Relationship> relationshipList = new ArrayList<>();
131 for (int i = 0; i < relationshipNode.size(); i++) {
132 Relationship relationship = new Relationship();
133 JSONObject jsonObject = relationshipNode.getJSONObject(i);
135 relationship.setRelatedLink(getTextElementByNode(jsonObject, "related-link"));
136 relationship.setRelatedTo(getTextElementByNode(jsonObject, "related-to"));
137 if (jsonObject.get("related-to-property") != null) {
138 JSONArray relatedToPropertyNode = jsonObject.getJSONArray("related-to-property");
139 relationship.setRelatedToPropertyList(
140 convertRelatedToProperty(relatedToPropertyNode));
142 relationship.setRelatedToPropertyList(Collections.emptyList());
144 if (jsonObject.get("relationship-data") != null) {
145 JSONArray relationshipDataNode = jsonObject.getJSONArray("relationship-data");
147 .setRelationshipDataList(convertRelationshipDate(relationshipDataNode));
149 relationship.setRelationshipDataList(Collections.emptyList());
151 relationshipList.add(relationship);
154 return relationshipList;
157 private List<RelationshipData> convertRelationshipDate(JSONArray relationshipDataNode) {
158 List<RelationshipData> relationshipDataList = new ArrayList<>();
159 for (int i = 0; i < relationshipDataNode.size(); i++) {
160 JSONObject jsonObject = relationshipDataNode.getJSONObject(i);
161 RelationshipData relationshipData = new RelationshipData();
162 relationshipData.setRelationshipKey(
163 getTextElementByNode(jsonObject, "relationship-key"));
164 relationshipData.setRelationshipValue(
165 getTextElementByNode(jsonObject, "relationship-value"));
166 relationshipDataList.add(relationshipData);
167 relationshipDataList.add(relationshipData);
170 return relationshipDataList;
173 private List<RelatedToProperty> convertRelatedToProperty(JSONArray relatedToPropertyNode) {
174 List<RelatedToProperty> relatedToPropertyList = new ArrayList<>();
175 for (int i = 0; i < relatedToPropertyNode.size(); i++) {
176 JSONObject jsonObject = relatedToPropertyNode.getJSONObject(i);
177 RelatedToProperty relatedToProperty = new RelatedToProperty();
179 .setPropertyKey(getTextElementByNode(jsonObject, "property-key"));
180 relatedToProperty.setPropertyValue(
181 getTextElementByNode(jsonObject, "property-value"));
182 relatedToPropertyList.add(relatedToProperty);
184 return relatedToPropertyList;
187 private String getTextElementByNode(JSONObject jsonNode, String name) {
188 if (jsonNode.get(name) != null) {
189 return jsonNode.getString(name);
194 private Boolean getBooleanElementByNode(JSONObject jsonNode, String name) {
195 if (jsonNode.get(name) != null) {
196 return jsonNode.getBoolean(name);