ebf3f67ca890c9c70bca4672b35b28c6f0efc31f
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.resmanagement.service.entity;
18
19 import java.io.Serializable;
20
21 import org.apache.commons.lang.StringUtils;
22 import org.onap.vfc.nfvo.resmanagement.common.util.JsonUtil;
23
24 import net.sf.json.JSONArray;
25 import net.sf.json.JSONObject;
26
27 /**
28  * @author l00345485
29  * @date 2016-10-28
30  */
31 public class VmEntity implements Serializable {
32
33     /**  */
34     private String vmId;
35
36     /**  */
37     private String vmName;
38
39     /**  */
40     private String vmStatus;
41
42     /**  */
43     private String vnfInstanceId;
44
45     private String resourceVersion;
46
47     private static final long serialVersionUID = 1L;
48
49     public String getVmId() {
50         return vmId;
51     }
52
53     public void setVmId(String vmId) {
54         this.vmId = vmId;
55     }
56
57     public String getVmName() {
58         return vmName;
59     }
60
61     public void setVmName(String vmName) {
62         this.vmName = vmName;
63     }
64
65     public String getVmStatus() {
66         return vmStatus;
67     }
68
69     public void setVmStatus(String vmStatus) {
70         this.vmStatus = vmStatus;
71     }
72
73     public String getVnfInstanceId() {
74         return vnfInstanceId;
75     }
76
77     public void setVnfInstanceId(String vnfInstanceId) {
78         this.vnfInstanceId = vnfInstanceId;
79     }
80
81     public String getResourceVersion() {
82         return resourceVersion;
83     }
84
85     public void setResourceVersion(String resourceVersion) {
86         this.resourceVersion = resourceVersion;
87     }
88
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"));
95         return vmEntity;
96     }
97
98     @Override
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();
106     }
107
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()));
115
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);
122
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);
128
129             JSONObject relation = new JSONObject();
130             relation.put("relationship", relationship);
131
132             vmResJson.put("relationship-list", relation);
133         }
134         return vmResJson.toString();
135     }
136
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"));
143         return vmEntity;
144
145     }
146 }