2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.vnf;
19 import java.io.IOException;
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;
31 import net.sf.json.JSONArray;
32 import net.sf.json.JSONException;
33 import net.sf.json.JSONObject;
36 * create or terminate VNF to M
40 * @version VFC 1.0 Aug 24, 2016
42 public class VnfMgrVnfm implements InterfaceVnfMgr {
44 private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
46 private static final int PARAM_ZERO = 0;
48 private static final int PARAM_ONE = 1;
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);
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"));
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) {
76 JSONArray vmList = new JSONArray();
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);
83 scaleInfo.put("vm_list", vmList);
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());
90 int statusCode = queryResult.getInt(Constant.RETCODE);
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);
102 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
105 } catch(JSONException e) {
106 LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
112 private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
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);
123 LOG.info("vdu_type=" + vduType);
127 private int getScaleType(String type) {
128 if("SCALE_OUT".equalsIgnoreCase(type)) {
130 } else if("SCALE_IN".equalsIgnoreCase(type)) {
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;
144 JSONObject queryResult =
145 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
146 LOG.info("createVnf csm response content:" + queryResult);
148 int statusCode = queryResult.getInt(Constant.RETCODE);
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);
160 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
163 } catch(JSONException e) {
164 LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
170 private void handleResponse(JSONObject result, JSONObject returnObj, String vnfInstanceId, String type) {
172 if(returnObj.containsKey("job_id")) {
173 jobId = returnObj.getString("job_id") + ":job";
175 jobId = vnfInstanceId + "_" + type + ":no";
177 result.put("vnfInstanceId", vnfInstanceId);
178 result.put(Constant.JOBID, jobId);
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);
187 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
188 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
189 Constant.CERTIFICATE);
191 int statusCode = queryResult.getInt(Constant.RETCODE);
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")))
199 appInfo = JSONObject.fromObject(queryResult.getString("data"));
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);
206 LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
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);
218 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
219 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
221 int statusCode = queryResult.getInt(Constant.RETCODE);
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"));
228 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
229 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
231 LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
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);
242 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
243 String.format(ParamConstants.VNF_CONFIGURATION_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
245 int statusCode = queryResult.getInt(Constant.RETCODE);
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"));
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");
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);
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");
276 result.put("ip", ip);
277 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
278 restJson.put("data", result);
282 LOG.error("function=getIp, msg=send get vnf msg to csm get wrong status: {}", statusCode);
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);
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);
299 int statusCode = queryResult.getInt(Constant.RETCODE);
301 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
303 if((queryResult.get("data")) == null) {
304 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
307 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
308 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
310 LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
322 * @param vnfInstanceId
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);
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);
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");
342 LOG.info("healVnf subJsonObject :" + subJsonObject);
343 JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
344 Constant.CERTIFICATE);
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);
351 LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
357 public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
358 LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
360 JSONObject restJson = new JSONObject();
361 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
363 JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
364 Constant.GET, null, Constant.CERTIFICATE);
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"));
372 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
373 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
375 LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);