33c1942e3a82baf579cd0edfdb0729ca6c56ba96
[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.openo.nfvo.resmanagement.service.entity;
18
19 import org.apache.commons.lang3.StringUtils;
20 import org.openo.nfvo.resmanagement.common.util.JsonUtil;
21 import org.openo.nfvo.resmanagement.common.util.StringUtil;
22
23 import net.sf.json.JSONObject;
24
25 /**
26  *
27  * Sites entity class.<br>
28  * <p>
29  * </p>
30  *
31  * @author
32  * @version     NFVO 0.5  Sep 10, 2016
33  */
34 public class SitesEntity {
35
36     private String id;
37
38     private String name;
39
40     private String location;
41
42     private String country;
43
44     private String vimId;
45
46     private String vimName;
47
48     private String status;
49
50     private String totalCPU = "0";
51
52     private String totalMemory = "0";
53
54     private String totalDisk = "0";
55
56     private String usedCPU = "0";
57
58     private String usedMemory = "0";
59
60     private String usedDisk = "0";
61
62     public String getId() {
63         return id;
64     }
65
66     public void setId(String id) {
67         this.id = id == null ? null : id.trim();
68     }
69
70     public String getName() {
71         return name;
72     }
73
74     public void setName(String name) {
75         this.name = name == null ? null : name.trim();
76     }
77
78     public String getLocation() {
79         return location;
80     }
81
82     public void setLocation(String location) {
83         this.location = location == null ? null : location.trim();
84     }
85
86     public String getCountry() {
87         return country;
88     }
89
90     public void setCountry(String country) {
91         this.country = country == null ? null : country.trim();
92     }
93
94     public String getVimId() {
95         return vimId;
96     }
97
98     public void setVimId(String siteVimId) {
99         this.vimId = siteVimId == null ? null : siteVimId.trim();
100     }
101
102     public String getVimName() {
103         return vimName;
104     }
105
106     public void setVimName(String siteVimName) {
107         this.vimName = siteVimName == null ? null : siteVimName.trim();
108     }
109
110     public String getStatus() {
111         return status;
112     }
113
114     public void setStatus(String siteStatus) {
115         this.status = siteStatus == null ? null : siteStatus.trim().toLowerCase();
116     }
117
118     /**
119      * @return Returns the totalCPU.
120      */
121     public String getTotalCPU() {
122         return totalCPU;
123     }
124
125     /**
126      * @param totalCPU The totalCPU to set.
127      */
128     public void setTotalCPU(String totalCPU) {
129         this.totalCPU = totalCPU == null ? null : totalCPU.trim();
130     }
131
132     /**
133      * @return Returns the totalMemory.
134      */
135     public String getTotalMemory() {
136         return totalMemory;
137     }
138
139     /**
140      * @param totalMemory The totalMemory to set.
141      */
142     public void setTotalMemory(String totalMemory) {
143         this.totalMemory = totalMemory == null ? null : totalMemory.trim();
144     }
145
146     /**
147      * @return Returns the totalDisk.
148      */
149     public String getTotalDisk() {
150         return totalDisk;
151     }
152
153     /**
154      * @param totalDisk The totalDisk to set.
155      */
156     public void setTotalDisk(String totalDisk) {
157         this.totalDisk = totalDisk == null ? null : totalDisk.trim();
158     }
159
160     /**
161      * @return Returns the usedCPU.
162      */
163     public String getUsedCPU() {
164         return usedCPU;
165     }
166
167     /**
168      * @param usedCPU The usedCPU to set.
169      */
170     public void setUsedCPU(String usedCPU) {
171         this.usedCPU = usedCPU == null ? null : usedCPU.trim();
172     }
173
174     /**
175      * @return Returns the usedMemory.
176      */
177     public String getUsedMemory() {
178         return usedMemory;
179     }
180
181     /**
182      * @param usedMemory The usedMemory to set.
183      */
184     public void setUsedMemory(String usedMemory) {
185         this.usedMemory = usedMemory == null ? null : usedMemory.trim();
186     }
187
188     /**
189      * @return Returns the usedDisk.
190      */
191     public String getUsedDisk() {
192         return usedDisk;
193     }
194
195     /**
196      * @param usedDisk The usedDisk to set.
197      */
198     public void setUsedDisk(String usedDisk) {
199         this.usedDisk = usedDisk == null ? null : usedDisk.trim();
200     }
201
202     /**
203      *
204      * To Entity.<br>
205      *
206      * @param jsonObject
207      * @return
208      * @since  NFVO 0.5
209      */
210     public static SitesEntity toEntity(JSONObject jsonObject) {
211         SitesEntity sitesEntity = new SitesEntity();
212         sitesEntity.setId(JsonUtil.getJsonFieldStr(jsonObject, "id"));
213         sitesEntity.setName(JsonUtil.getJsonFieldStr(jsonObject, "name"));
214         sitesEntity.setStatus(JsonUtil.getJsonFieldStr(jsonObject, "status"));
215         sitesEntity.setLocation(JsonUtil.getJsonFieldStr(jsonObject, "location"));
216         sitesEntity.setCountry(JsonUtil.getJsonFieldStr(jsonObject, "country"));
217         sitesEntity.setVimId(JsonUtil.getJsonFieldStr(jsonObject, "vimId"));
218         sitesEntity.setVimName(JsonUtil.getJsonFieldStr(jsonObject, "vimName"));
219         sitesEntity.setTotalCPU(JsonUtil.getJsonFieldStr(jsonObject, "totalCPU"));
220         sitesEntity.setTotalMemory(JsonUtil.getJsonFieldStr(jsonObject, "totalMemory"));
221         sitesEntity.setTotalDisk(JsonUtil.getJsonFieldStr(jsonObject, "totalDisk"));
222         sitesEntity.setUsedCPU(JsonUtil.getJsonFieldStr(jsonObject, "usedCPU"));
223         sitesEntity.setUsedMemory(JsonUtil.getJsonFieldStr(jsonObject, "usedMemory"));
224         sitesEntity.setUsedDisk(JsonUtil.getJsonFieldStr(jsonObject, "usedDisk"));
225         return sitesEntity;
226     }
227
228     @Override
229     public String toString() {
230         JSONObject siteResJson = new JSONObject();
231         siteResJson.put("id", StringUtils.trimToEmpty(this.getId()));
232         siteResJson.put("name", StringUtils.trimToEmpty(this.getName()));
233         siteResJson.put("status", StringUtils.trimToEmpty(this.getStatus()));
234         siteResJson.put("location", StringUtils.trimToEmpty(this.getLocation()));
235         siteResJson.put("country", StringUtils.trimToEmpty(this.getCountry()));
236         siteResJson.put("vimId", StringUtils.trimToEmpty(this.getVimId()));
237         siteResJson.put("vimName", StringUtils.trimToEmpty(this.getVimName()));
238         siteResJson.put("totalCPU", StringUtils.trimToEmpty(this.getTotalCPU()));
239         siteResJson.put("totalMemory", StringUtils.trimToEmpty(this.getTotalMemory()));
240         siteResJson.put("totalDisk", StringUtils.trimToEmpty(this.getTotalDisk()));
241         siteResJson.put("usedCPU", StringUtils.trimToEmpty(this.getUsedCPU()));
242         siteResJson.put("usedMemory", StringUtils.trimToEmpty(this.getUsedMemory()));
243         siteResJson.put("usedDisk", StringUtils.trimToEmpty(this.getUsedDisk()));
244
245         return siteResJson.toString();
246     }
247
248     /**
249      *
250      * Check whther the resource is zero.<br>
251      *
252      * @param siteEntity
253      * @return
254      * @since  NFVO 0.5
255      */
256     public static boolean checkResourceIsZero(SitesEntity siteEntity) {
257
258         if(StringUtil.isAnyLargeThanZero(siteEntity.getUsedCPU(), siteEntity.getUsedMemory(),
259                 siteEntity.getUsedDisk())) {
260             return false;
261         }
262
263         return true;
264     }
265
266     /**
267      *
268      * Check resource.<br>
269      *
270      * @param siteEntity
271      * @return
272      * @since  NFVO 0.5
273      */
274     public static boolean checkResource(SitesEntity siteEntity) {
275         String cpu = siteEntity.getTotalCPU();
276         String memory = siteEntity.getTotalMemory();
277         String disk = siteEntity.getTotalDisk();
278         String cpuUsed = siteEntity.getUsedCPU();
279         String memoryUsed = siteEntity.getUsedMemory();
280         String diskUsed = siteEntity.getUsedDisk();
281
282         if(!StringUtil.isInteger(cpu, cpuUsed)) {
283             return false;
284         }
285
286         if(!StringUtil.isNumeric(memory, disk, memoryUsed, diskUsed)) {
287             return false;
288         }
289         return true;
290     }
291
292     /**
293      *
294      * Format data.<br>
295      *
296      * @param siteEntity
297      * @since  NFVO 0.5
298      */
299     public static void dataFramat(SitesEntity siteEntity) {
300         siteEntity.setTotalCPU(StringUtil.numFormat(siteEntity.getTotalCPU()));
301         siteEntity.setTotalMemory(StringUtil.numFormat(siteEntity.getTotalMemory()));
302         siteEntity.setTotalDisk(StringUtil.numFormat(siteEntity.getTotalDisk()));
303         siteEntity.setUsedCPU(StringUtil.numFormat(siteEntity.getUsedCPU()));
304         siteEntity.setUsedMemory(StringUtil.numFormat(siteEntity.getUsedMemory()));
305         siteEntity.setUsedDisk(StringUtil.numFormat(siteEntity.getUsedDisk()));
306
307     }
308
309 }