Add healing VNF interface
[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 org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.ResultRequestUtil;
20 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
21 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.ParamConstants;
22 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import net.sf.json.JSONArray;
27 import net.sf.json.JSONException;
28 import net.sf.json.JSONObject;
29
30 /**
31  * create or terminate VNF to M
32  * <br/>
33  *
34  * @author
35  * @version NFVO 0.5 Aug 24, 2016
36  */
37 public class VnfMgrVnfm implements InterfaceVnfMgr {
38
39     private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
40
41     @Override
42     public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
43         LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
44         JSONObject restJson = new JSONObject();
45         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
46         String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
47
48         int scaleType = getScaleType(vnfObject.getString("type"));
49         // build request json object
50         JSONObject paramJson = new JSONObject();
51         JSONObject scaleInfo = new JSONObject();
52         JSONArray vduList = new JSONArray();
53         JSONObject vdu = new JSONObject();
54         vdu.put("vdu_type", this.getVduType(vnfmObject, vnfInstanceId));// TODO:set vdu_type
55         vdu.put("h_steps", vnfObject.get("numberOfSteps"));
56         vduList.add(vdu);
57         scaleInfo.put("vnf_id", vnfInstanceId);
58         scaleInfo.put("scale_type", 0);
59         scaleInfo.put("scale_action", scaleType);
60         scaleInfo.put("vdu_list", vduList);
61         if(scaleType == 0) {// scale_in
62             JSONArray vmList = new JSONArray();
63             try {
64                 JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
65                 vmList = additionalParam.getJSONArray("vm_list");
66             } catch(JSONException e) {
67                 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it", e);
68             }
69             scaleInfo.put("vm_list", vmList);
70         }
71         paramJson.put("scale_info", scaleInfo);
72         JSONObject queryResult =
73                 ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(), Constant.CERTIFICATE);
74         LOG.info("SCALE execute result:" + queryResult.toString());
75         try {
76             int statusCode = queryResult.getInt(Constant.RETCODE);
77
78             if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
79                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
80                 JSONObject resultObj = new JSONObject();
81                 resultObj.put("jobId", vnfInstanceId + "_" + Constant.PUT);
82                 restJson.put("data", resultObj);
83             } else {
84                 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
85             }
86
87         } catch(JSONException e) {
88             LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
89         }
90
91         return restJson;
92     }
93
94     private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
95         String vduType = "";
96         try {
97             JSONObject queryResult =
98                     ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
99                             Constant.GET, null, Constant.CERTIFICATE);
100             LOG.info("getVduType result=" + queryResult);
101             vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
102         } catch(Exception e) {
103             LOG.error("get vdu_type failed.", e);
104         }
105         LOG.info("vdu_type=" + vduType);
106         return vduType;
107     }
108
109     private int getScaleType(String type) {
110         if("SCALE_OUT".equalsIgnoreCase(type)) {
111             return 1;
112         } else if("SCALE_IN".equalsIgnoreCase(type)) {
113             return 0;
114         }
115         return -1;
116     }
117
118     @Override
119     public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
120         LOG.info("function=createVnf, msg=enter to create a vnf");
121         LOG.info("createVnf csm request body :" + subJsonObject);
122         JSONObject restJson = new JSONObject();
123         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
124         String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
125
126         JSONObject queryResult =
127                 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
128         LOG.info("createVnf csm response content:" + queryResult);
129         try {
130             int statusCode = queryResult.getInt(Constant.RETCODE);
131
132             if(statusCode == Constant.HTTP_CREATED) {
133                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
134                 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
135                 JSONObject resultObj = new JSONObject();
136                 resultObj.put("vnfInstanceId", appInfo.getString("id"));
137                 resultObj.put("jobId", appInfo.getString("id") + "_" + Constant.POST);
138                 restJson.put("data", resultObj);
139             } else {
140                 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
141             }
142
143         } catch(JSONException e) {
144             LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
145         }
146
147         return restJson;
148     }
149
150     @Override
151     public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
152         LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
153         JSONObject restJson = new JSONObject();
154         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
155
156         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
157                 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
158                 Constant.CERTIFICATE);
159
160         int statusCode = queryResult.getInt(Constant.RETCODE);
161
162         if(statusCode == Constant.HTTP_NOCONTENT) {
163             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
164             JSONObject resultObj = new JSONObject();
165             resultObj.put("jobId", vnfId + "_" + Constant.DELETE);
166             restJson.put("data", resultObj);
167         } else {
168             LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
169         }
170
171         return restJson;
172     }
173
174     @Override
175     public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
176         LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
177         JSONObject restJson = new JSONObject();
178         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
179
180         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
181                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
182                 null, Constant.CERTIFICATE);
183
184         int statusCode = queryResult.getInt("retCode");
185
186         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
187             if(null == (queryResult.get("data"))) {
188                 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
189                 return restJson;
190             }
191             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
192             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
193         } else {
194             LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
195         }
196
197         return restJson;
198     }
199
200     @Override
201     public JSONObject getJob(JSONObject vnfmObject, String jobId) {
202         LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
203         JSONObject restJson = new JSONObject();
204         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
205
206         String vnfId = jobId.split("_")[0];
207         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
208                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
209                 null, Constant.CERTIFICATE);
210
211         int statusCode = queryResult.getInt("retCode");
212
213         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
214
215             if((queryResult.get("data")) == null) {
216                 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
217                 return restJson;
218             }
219             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
220             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
221         } else {
222             LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
223         }
224
225         return restJson;
226     }
227
228     /**
229      * <br>
230      * 
231      * @param jsonObject
232      * @param vnfmObjcet
233      * @param vnfmId
234      * @param vnfInstanceId
235      * @return
236      * @since NFVO 0.5
237      */
238     public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
239         LOG.info("healVnf request body :" + jsonObject);
240         JSONObject restJson = new JSONObject();
241         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
242
243         String action = jsonObject.getString("action");
244         JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
245         String vmId = affectedVm.getString("vmid");
246         String path = String.format(ParamConstants.HEAL_VNF, vmId);
247
248         JSONObject subJsonObject = new JSONObject();
249         subJsonObject.put("type", "hard");
250         subJsonObject.put("boot_mode", null);
251         if("vmReset".equals(action)) {
252             subJsonObject.put("action", "reset");
253         }
254
255         JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
256                 Constant.CERTIFICATE);
257
258         int statusCode = healResult.getInt("retCode");
259         if(statusCode == Constant.HTTP_OK) {
260
261             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
262         } else {
263             LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
264         }
265
266         return restJson;
267     }
268 }