2 * Copyright 2016 Huawei Technologies Co., Ltd.
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.
17 package org.onap.vfc.nfvo.resmanagement.service.entity;
19 import java.io.Serializable;
21 import org.apache.commons.lang.StringUtils;
22 import org.onap.vfc.nfvo.resmanagement.common.util.JsonUtil;
24 import net.sf.json.JSONArray;
25 import net.sf.json.JSONObject;
31 public class VmEntity implements Serializable {
37 private String vmName;
40 private String vmStatus;
43 private String vnfInstanceId;
45 private String resourceVersion;
47 private static final long serialVersionUID = 1L;
49 public String getVmId() {
53 public void setVmId(String vmId) {
57 public String getVmName() {
61 public void setVmName(String vmName) {
65 public String getVmStatus() {
69 public void setVmStatus(String vmStatus) {
70 this.vmStatus = vmStatus;
73 public String getVnfInstanceId() {
77 public void setVnfInstanceId(String vnfInstanceId) {
78 this.vnfInstanceId = vnfInstanceId;
81 public String getResourceVersion() {
82 return resourceVersion;
85 public void setResourceVersion(String resourceVersion) {
86 this.resourceVersion = resourceVersion;
89 public static VmEntity toEntity(JSONObject jsonObject) {
90 VmEntity vmEntity = new VmEntity();
91 vmEntity.setVmId(JsonUtil.getJsonFieldStr(jsonObject, "vmId"));
92 vmEntity.setVmName(JsonUtil.getJsonFieldStr(jsonObject, "vmName"));
93 vmEntity.setVmStatus(JsonUtil.getJsonFieldStr(jsonObject, "vmStatus"));
94 vmEntity.setVnfInstanceId(JsonUtil.getJsonFieldStr(jsonObject, "vnfInstanceId"));
99 public String toString() {
100 JSONObject vmResJson = new JSONObject();
101 vmResJson.put("vmId", StringUtils.trimToEmpty(this.getVmId()));
102 vmResJson.put("vmName", StringUtils.trimToEmpty(this.getVmName()));
103 vmResJson.put("vmStatus", StringUtils.trimToEmpty(this.getVmStatus()));
104 vmResJson.put("vnfInstanceId", StringUtils.trimToEmpty(this.getVnfInstanceId()));
105 return vmResJson.toString();
108 public String toStringForAai() {
109 JSONObject vmResJson = new JSONObject();
110 vmResJson.put("vserver-id", StringUtils.trimToEmpty(this.getVmId()));
111 vmResJson.put("vserver-name", StringUtils.trimToEmpty(this.getVmName()));
112 vmResJson.put("prov-status", StringUtils.trimToEmpty(this.getVmStatus()));
113 vmResJson.put("vserver-selflink", "");
114 vmResJson.put("resource-version", StringUtils.trimToEmpty(this.getResourceVersion()));
116 if(!StringUtils.isEmpty(this.getVnfInstanceId())) {
117 JSONArray relationshipData = new JSONArray();
118 JSONObject relationshipDataEntry = new JSONObject();
119 relationshipDataEntry.put("relationship-key", "generic-vnf.vnf-id");
120 relationshipDataEntry.put("relationship-value", this.getVnfInstanceId());
121 relationshipData.add(relationshipDataEntry);
123 JSONArray relationship = new JSONArray();
124 JSONObject relationshipEntry = new JSONObject();
125 relationshipEntry.put("related-to", "generic-vnf");
126 relationshipEntry.put("relationship-data", relationshipData);
127 relationship.add(relationshipEntry);
129 JSONObject relation = new JSONObject();
130 relation.put("relationship", relationship);
132 vmResJson.put("relationship-list", relation);
134 return vmResJson.toString();
137 public static VmEntity toEntityFromAai(JSONObject jsonObject) {
138 VmEntity vmEntity = new VmEntity();
139 vmEntity.setVmId(JsonUtil.getJsonFieldStr(jsonObject, "vserver-id"));
140 vmEntity.setVmName(JsonUtil.getJsonFieldStr(jsonObject, "vserver-name"));
141 vmEntity.setVmStatus(JsonUtil.getJsonFieldStr(jsonObject, "prov-status"));
142 vmEntity.setResourceVersion(JsonUtil.getJsonFieldStr(jsonObject, "resource-version"));