Define a constant instead of duplicating literal
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / process / VnfResourceMgr.java
1 /*
2  * Copyright 2016-2017 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.vnfm.svnfm.vnfmadapter.service.process;
18
19 import java.util.HashMap;
20 import java.util.Map;
21
22 import org.apache.commons.lang.StringUtils;
23 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import net.sf.json.JSONArray;
31 import net.sf.json.JSONException;
32 import net.sf.json.JSONObject;
33
34 /**
35  * Provide function of resource for VNFM.
36  * <br/>
37  * <p>
38  * </p>
39  *
40  * @author
41  * @version VFC 1.0 Aug 25, 2016
42  */
43 public class VnfResourceMgr {
44
45     private static final Logger LOG = LoggerFactory.getLogger(VnfResourceMgr.class);
46
47     /**
48      * Provide function of grant resource for VNFM.
49      * <br/>
50      *
51      * @param vnfObj
52      * @param vnfId
53      * @param vnfmId
54      * @return
55      * @since VFC 1.0
56      */
57     public JSONObject grantVnfResource(JSONObject vnfObj, String vnfId, String vnfmId) {
58         LOG.warn("function=grantVnfResource, msg=enter to grant vnf resource, params: {}", vnfObj);
59         JSONObject resultJson = new JSONObject();
60         resultJson.put(Constant.RETCODE, Constant.REST_FAIL);
61         try {
62             String type = vnfObj.getString("type");
63             String requestType = vnfObj.getString("operation_right");
64             String vnfName = vnfObj.getString("vnf_name");
65
66             if(StringUtils.isEmpty(type) || StringUtils.isEmpty(requestType) || StringUtils.isEmpty(vnfName)
67                     || StringUtils.isEmpty(vnfId)) {
68                 LOG.error("function=grantVnfResource, msg=grant basic params error");
69                 resultJson.put(Constant.ERRORMSG, "basic params error");
70                 return resultJson;
71             }
72
73             JSONArray vmList = vnfObj.getJSONArray("vm_list");
74
75             Map<String, Integer> resMap = calculateGrantRes(vmList);
76
77             if(null == resMap) {
78                 LOG.error("function=grantVnfResource, msg=grant resource params error");
79                 resultJson.put(Constant.ERRORMSG, "resource params error");
80                 return resultJson;
81             }
82
83             JSONObject grantObj = new JSONObject();
84             grantObj.put(Constant.VIMID, vnfObj.getString("vim_id"));
85             grantObj.put("vnfId", vnfId);
86             grantObj.put("vnfName", vnfName);
87             grantObj.put(Constant.VNFMID, vnfmId);
88             String action = getGrantAction(type, requestType);
89             grantObj.put(Constant.ACTION, action);
90
91             JSONObject grantParam = parseGrantParam(resMap, grantObj);
92             resultJson = sendGrantToResmgr(grantParam);
93             LOG.error("function=grantVnfResource, resultJson={}.", resultJson);
94         } catch(JSONException e) {
95             LOG.error("function=grantVnfResource, msg=parse params occoured JSONException e={}.", e);
96             resultJson.put(Constant.ERRORMSG, "params parse exception");
97         }
98
99         return resultJson;
100     }
101
102     /**
103      * <br>
104      *
105      * @param grantParam
106      * @return
107      * @since VFC 1.0
108      */
109     private JSONObject sendGrantToResmgr(JSONObject grantParam) {
110         RestfulResponse rsp = VnfmRestfulUtil.getRemoteResponse(ParamConstants.GRANT_RES_URL, VnfmRestfulUtil.TYPE_PUT,
111                 grantParam.toString());
112         if(rsp == null || rsp.getStatus() != Constant.HTTP_OK) {
113             return null;
114         }
115         LOG.error("funtion=sendGrantToResmgr, status={}", rsp.getStatus());
116         return JSONObject.fromObject(rsp.getResponseContent());
117     }
118
119     /**
120      * <br>
121      *
122      * @param resMap
123      * @param grantParam
124      * @return
125      * @since VFC 1.0
126      */
127     private JSONObject parseGrantParam(Map<String, Integer> resMap, JSONObject grantParam) {
128         JSONObject result = new JSONObject();
129         result.put("vnfInstanceId", grantParam.getString("vnfId"));
130         result.put(Constant.VIMID, grantParam.getString(Constant.VIMID));
131
132         JSONArray resource = new JSONArray();
133         JSONObject resourceObj = new JSONObject();
134         resourceObj.put("type", "vdu");
135         JSONObject resourceTemplate = new JSONObject();
136         JSONObject storage = new JSONObject();
137         storage.put("sizeOfStorage", resMap.get("diskNum"));
138         storage.put("typeOfStorage", "");
139         storage.put("swImageDescriptor", "");
140         JSONObject compute = new JSONObject();
141         JSONObject virtualMemory = new JSONObject();
142         virtualMemory.put("virtualMemSize", resMap.get("memNum"));
143         JSONObject virtualCpu = new JSONObject();
144         virtualCpu.put("numVirtualCpu", resMap.get("cpuNum"));
145         compute.put("virtualMemory", virtualMemory);
146         compute.put("virtualCpu", virtualCpu);
147         resourceTemplate.put("virtualStorageDescriptor", storage);
148         resourceTemplate.put("virtualComputeDescriptor", compute);
149         resourceObj.put("resourceTemplate", resourceTemplate);
150         resourceObj.put("resourceDefinitionId", "");
151         resourceObj.put("vdu", grantParam.getString("vnfName"));
152         resource.add(resourceObj);
153
154         if("online".equals(grantParam.getString(Constant.ACTION))
155                 || "scaleOut".equals(grantParam.getString(Constant.ACTION))) {
156             result.put("addResource", resource);
157         } else {
158             result.put("removeResource", resource);
159         }
160
161         JSONObject additionalParam = new JSONObject();
162         additionalParam.put(Constant.VNFMID, grantParam.getString(Constant.VNFMID));
163         additionalParam.put(Constant.VIMID, grantParam.getString(Constant.VIMID));
164         additionalParam.put("tenant", "");
165         result.put("additionalParam", additionalParam);
166         LOG.info("funtion=parseGrantParam, result={}", result);
167         return result;
168     }
169
170     private Map<String, Integer> calculateGrantRes(JSONArray vmList) {
171         Map<String, Integer> resMap = new HashMap<>(Constant.DEFAULT_COLLECTION_SIZE);
172         int vmSize = vmList.size();
173         int cpuNum = 0;
174         int memNum = 0;
175         int diskNum = 0;
176         int diskSize = 0;
177         int cpuTmp = 0;
178         int memTmp = 0;
179         int diskTmp = 0;
180         int initNum = 0;
181
182         try {
183             for(int i = 0; i < vmSize; i++) {
184                 JSONObject resInfo = vmList.getJSONObject(i);
185                 JSONObject vmFlavor = resInfo.getJSONObject("vm_flavor");
186                 initNum = Integer.parseInt(resInfo.getString("init_number"));
187
188                 if(initNum == 0) {
189                     continue;
190                 }
191
192                 JSONArray volumList = vmFlavor.getJSONArray("storage");
193                 diskSize = volumList.size();
194
195                 for(int j = 0; j < diskSize; j++) {
196                     JSONObject volumeInfo = volumList.getJSONObject(j);
197                     diskTmp += getDiskQuantity(volumeInfo);
198                 }
199
200                 cpuTmp = Integer.parseInt(vmFlavor.getString("num_cpus"));
201                 memTmp = Integer.parseInt(vmFlavor.getString("mem_size"));
202
203                 cpuNum += cpuTmp * initNum;
204                 memNum += memTmp * initNum;
205                 diskNum += diskTmp * initNum;
206
207                 diskTmp = 0;
208
209             }
210         } catch(JSONException e) {
211             LOG.error("function=calculateGrantRes, msg=parse params occoured JSONException e={}.", e);
212             return null;
213         }
214
215         resMap.put("cpuNum", cpuNum);
216         resMap.put("memNum", memNum);
217         resMap.put("diskNum", diskNum);
218         return resMap;
219     }
220
221     private String getGrantAction(String type, String requestType) {
222         String action = "unknown";
223
224         if(("increase").equals(requestType)) {
225             if(("instantiation").equals(type)) {
226                 action = "online";
227             } else if(("scale").equals(type)) {
228                 action = "scaleOut";
229             }
230
231         } else if(("decrease").equals(requestType)) {
232             if(("instantiation").equals(type)) {
233                 action = "offline";
234             } else if(("scale").equals(type)) {
235                 action = "scaleIn";
236             }
237         }
238
239         return action;
240     }
241
242     private int getDiskQuantity(JSONObject volumeObj) {
243         int disk = 0;
244         if(volumeObj.containsKey("vol_type")) {
245             if("local_volume".equals(volumeObj.getString("vol_type"))) {
246                 disk = Integer.parseInt(volumeObj.getString("vol_size"));
247             }
248         } else if(volumeObj.containsKey("storage_type") && "local_image".equals(volumeObj.getString("storage_type"))) {
249
250             disk = Integer.parseInt(volumeObj.getString("disk_size"));
251
252         }
253         return disk;
254     }
255 }