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