e57d23f40838d35cc5c7b9816f1dcc508a154d0a
[vfc/nfvo/driver/vnfm/svnfm.git] / huawei / vnfmadapter / VnfmadapterService / service / src / main / java / org / onap / vfc / nfvo / vnfm / svnfm / vnfmadapter / service / csm / vnf / VnfMgrVnfm.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.csm.vnf;
18
19 import java.io.IOException;
20
21 import org.apache.commons.io.IOUtils;
22 import org.apache.commons.lang3.StringUtils;
23 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.ResultRequestUtil;
24 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
25 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
26 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
27 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import net.sf.json.JSONArray;
32 import net.sf.json.JSONException;
33 import net.sf.json.JSONObject;
34
35 /**
36  * create or terminate VNF to M
37  * <br/>
38  *
39  * @author
40  * @version VFC 1.0 Aug 24, 2016
41  */
42 public class VnfMgrVnfm implements InterfaceVnfMgr {
43
44     private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
45
46     private static final int PARAM_ZERO = 0;
47
48     private static final int PARAM_ONE = 1;
49
50     @Override
51     public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
52         LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
53         JSONObject restJson = new JSONObject();
54         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
55         String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
56
57         int scaleType = getScaleType(vnfObject.getString("type"));
58         // build request json object
59         JSONObject paramJson = new JSONObject();
60         JSONObject scaleInfo = new JSONObject();
61         JSONArray vduList = new JSONArray();
62         JSONObject vdu = new JSONObject();
63         vdu.put("vdu_type", this.getVduType(vnfmObject, vnfInstanceId));
64         vdu.put("h_steps", vnfObject.get("numberOfSteps"));
65         vduList.add(vdu);
66         scaleInfo.put("vnf_id", vnfInstanceId);
67         scaleInfo.put("scale_pattern", "without_plan");
68         scaleInfo.put("scale_type", PARAM_ZERO);
69         scaleInfo.put("scale_action", scaleType);
70         scaleInfo.put("scale_step", PARAM_ZERO);
71         scaleInfo.put("scale_step_value", PARAM_ONE);
72         scaleInfo.put("scale_group", vdu.getString("vdu_type"));
73         scaleInfo.put("vdu_list", vduList);
74         if(scaleType == PARAM_ZERO) {
75             // scale_in
76             JSONArray vmList = new JSONArray();
77             try {
78                 JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
79                 vmList = additionalParam.getJSONArray("vm_list");
80             } catch(JSONException e) {
81                 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it", e);
82             }
83             scaleInfo.put("vm_list", vmList);
84         }
85         paramJson.put("scale_info", scaleInfo);
86         JSONObject queryResult =
87                 ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(), Constant.CERTIFICATE);
88         LOG.info("SCALE execute result:" + queryResult.toString());
89         try {
90             int statusCode = queryResult.getInt(Constant.RETCODE);
91
92             if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
93                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
94                 // restJson.put("data",
95                 // queryResult.getJSONObject("data").getJSONObject("scale_info"));
96                 JSONObject appInfo = queryResult.getJSONObject("data").getJSONObject("scale_info");
97                 JSONObject resultObj = new JSONObject();
98                 // resultObj.put(Constant.JOBID, vnfInstanceId + "_" + Constant.PUT);
99                 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.PUT);
100                 restJson.put("data", resultObj);
101             } else {
102                 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
103             }
104
105         } catch(JSONException e) {
106             LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
107         }
108
109         return restJson;
110     }
111
112     private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
113         String vduType = "";
114         try {
115             JSONObject queryResult =
116                     ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
117                             Constant.GET, null, Constant.CERTIFICATE);
118             LOG.info("getVduType result=" + queryResult);
119             vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
120         } catch(Exception e) {
121             LOG.error("get vdu_type failed.", e);
122         }
123         LOG.info("vdu_type=" + vduType);
124         return vduType;
125     }
126
127     private int getScaleType(String type) {
128         if("SCALE_OUT".equalsIgnoreCase(type)) {
129             return 1;
130         } else if("SCALE_IN".equalsIgnoreCase(type)) {
131             return 0;
132         }
133         return -1;
134     }
135
136     @Override
137     public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
138         LOG.info("function=createVnf, msg=enter to create a vnf");
139         LOG.info("createVnf csm request body :" + subJsonObject);
140         JSONObject restJson = new JSONObject();
141         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
142         String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
143
144         JSONObject queryResult =
145                 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
146         LOG.info("createVnf csm response content:" + queryResult);
147         try {
148             int statusCode = queryResult.getInt(Constant.RETCODE);
149
150             if(statusCode == Constant.HTTP_CREATED) {
151                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
152                 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
153                 JSONObject resultObj = new JSONObject();
154                 // resultObj.put("vnfInstanceId", appInfo.getString("id"));
155                 // resultObj.put(Constant.JOBID, appInfo.getString("id") + "_" + Constant.POST);
156                 String vnfInstanceId = appInfo.getString("id");
157                 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.POST);
158                 restJson.put("data", resultObj);
159             } else {
160                 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
161             }
162
163         } catch(JSONException e) {
164             LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
165         }
166
167         return restJson;
168     }
169
170     private void handleResponse(JSONObject result, JSONObject returnObj, String vnfInstanceId, String type) {
171         String jobId = "";
172         if(returnObj.containsKey("job_id")) {
173             jobId = returnObj.getString("job_id") + ":job";
174         } else {
175             jobId = vnfInstanceId + "_" + type + ":no";
176         }
177         result.put("vnfInstanceId", vnfInstanceId);
178         result.put(Constant.JOBID, jobId);
179     }
180
181     @Override
182     public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
183         LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
184         JSONObject restJson = new JSONObject();
185         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
186
187         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
188                 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
189                 Constant.CERTIFICATE);
190
191         int statusCode = queryResult.getInt(Constant.RETCODE);
192
193         if(statusCode == Constant.HTTP_NOCONTENT || statusCode == Constant.HTTP_OK) {
194             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
195             // restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
196             JSONObject appInfo = new JSONObject();
197             if(queryResult.containsKey("data") && StringUtils.isNotEmpty(queryResult.getString("data")))
198             {
199                 appInfo = JSONObject.fromObject(queryResult.getString("data"));
200             }
201             JSONObject resultObj = new JSONObject();
202             // resultObj.put(Constant.JOBID, vnfId + "_" + Constant.DELETE);
203             handleResponse(resultObj, appInfo, vnfId, Constant.DELETE);
204             restJson.put("data", resultObj);
205         } else {
206             LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
207         }
208
209         return restJson;
210     }
211
212     @Override
213     public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
214         LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
215         JSONObject restJson = new JSONObject();
216         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
217
218         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
219                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
220
221         int statusCode = queryResult.getInt(Constant.RETCODE);
222
223         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
224             if(null == (queryResult.get("data"))) {
225                 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
226                 return restJson;
227             }
228             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
229             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
230         } else {
231             LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
232         }
233
234         return restJson;
235     }
236
237     public JSONObject getIp(JSONObject vnfmObject, String vnfId) throws IOException {
238         LOG.warn("function=getIp, msg=enter to getIp: {}", vnfId);
239         JSONObject restJson = new JSONObject();
240         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
241
242         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
243                 String.format(ParamConstants.VNF_CONFIGURATION_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
244
245         int statusCode = queryResult.getInt(Constant.RETCODE);
246
247         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
248             if(null == (queryResult.get("data"))) {
249                 LOG.warn("function=getIp, msg=query is null {}", queryResult.get("data"));
250                 return restJson;
251             }
252             JSONObject config = JSONObject.fromObject(queryResult.getString("data"));
253             LOG.info("function=getIp, query configuration result: {}", config);
254             JSONObject vnfInfo = config.getJSONArray("configuration").getJSONObject(0);
255             JSONObject result = new JSONObject();
256             result.put("vnf_id", vnfInfo.getString("vnf_id"));
257             result.put("vnf_type", vnfInfo.getString("vnf_type"));
258             JSONArray inputs = vnfInfo.getJSONArray("inputs");
259
260             ClassLoader classLoader = getClass().getClassLoader();
261             String ipConfig = IOUtils.toString(classLoader.getResourceAsStream("ipConfig.json"));
262             LOG.info("ipConfig: {}", ipConfig);
263             JSONObject ipCon = JSONObject.fromObject(ipConfig);
264             String vnfType = vnfInfo.getString("vnf_type");
265             if(ipCon.containsKey(vnfType)) {
266                 String ipKey = ipCon.getString(vnfInfo.getString("vnf_type"));
267                 LOG.info("ipKey: {}", ipKey);
268                 String ip = "";
269                 for(int i = 0; i < inputs.size(); i++) {
270                     JSONObject obj = inputs.getJSONObject(i);
271                     if(obj.getString("key_name").equals(ipKey)) {
272                         ip = obj.getString("value");
273                         break;
274                     }
275                 }
276                 result.put("ip", ip);
277                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
278                 restJson.put("data", result);
279             }
280
281         } else {
282             LOG.error("function=getIp, msg=send get vnf msg to csm get wrong status: {}", statusCode);
283         }
284
285         return restJson;
286     }
287
288     @Override
289     public JSONObject getJob(JSONObject vnfmObject, String jobId) {
290         LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
291         JSONObject restJson = new JSONObject();
292         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
293
294         String vnfId = jobId.split("_")[0];
295         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
296                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
297                 null, Constant.CERTIFICATE);
298
299         int statusCode = queryResult.getInt(Constant.RETCODE);
300
301         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
302
303             if((queryResult.get("data")) == null) {
304                 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
305                 return restJson;
306             }
307             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
308             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
309         } else {
310             LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
311         }
312
313         return restJson;
314     }
315
316     /**
317      * <br>
318      * 
319      * @param jsonObject
320      * @param vnfmObjcet
321      * @param vnfmId
322      * @param vnfInstanceId
323      * @return
324      * @since VFC 1.0
325      */
326     public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
327         LOG.info("healVnf request body :" + jsonObject);
328         JSONObject restJson = new JSONObject();
329         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
330
331         String action = jsonObject.getString("action");
332         JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
333         String vmId = affectedVm.getString("vmid");
334         String path = String.format(ParamConstants.HEAL_VNF, vmId);
335
336         JSONObject subJsonObject = new JSONObject();
337         subJsonObject.put("type", "hard");
338         subJsonObject.put("boot_mode", "");
339         if("vmReset".equals(action)) {
340             subJsonObject.put("action", "reset");
341         }
342         LOG.info("healVnf subJsonObject :" + subJsonObject);
343         JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
344                 Constant.CERTIFICATE);
345
346         int statusCode = healResult.getInt(Constant.RETCODE);
347         if(statusCode == Constant.HTTP_OK) {
348             LOG.info("healResult:{}", healResult);
349             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
350         } else {
351             LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
352         }
353
354         return restJson;
355     }
356
357     public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
358         LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
359
360         JSONObject restJson = new JSONObject();
361         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
362
363         JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
364                 Constant.GET, null, Constant.CERTIFICATE);
365
366         int statusCode = queryResult.getInt(Constant.RETCODE);
367         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
368             if((queryResult.get("data")) == null) {
369                 LOG.warn("function=getJobFromVnfm, msg=query is null {}", queryResult.get("data"));
370                 return restJson;
371             }
372             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
373             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
374         } else {
375             LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);
376         }
377
378         return restJson;
379     }
380
381 }