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.adapter.impl.AdapterResourceManager;
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.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.UrlConstant;
28 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.csm.inf.InterfaceVnfMgr;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
32 import net.sf.json.JSONArray;
33 import net.sf.json.JSONException;
34 import net.sf.json.JSONObject;
37 * create or terminate VNF to M
41 * @version VFC 1.0 Aug 24, 2016
43 public class VnfMgrVnfm implements InterfaceVnfMgr {
45 private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
47 private static final int PARAM_ZERO = 0;
49 private static final int PARAM_ONE = 1;
52 public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
53 LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
55 JSONObject queryVms = ResultRequestUtil.call(vnfmObject,
56 String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId), Constant.GET, null, Constant.CERTIFICATE);
57 LOG.info("function=scaleVnf, msg=query vms result=" + queryVms);
59 JSONObject restJson = new JSONObject();
60 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
61 String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
63 int scaleType = getScaleType(vnfObject.getString("type"));
64 // build request json object
65 JSONObject paramJson = new JSONObject();
66 JSONObject scaleInfo = new JSONObject();
67 JSONArray vduList = new JSONArray();
68 JSONObject vdu = new JSONObject();
70 if(vnfObject.containsKey("configedVduType")) {
71 vdu.put("vdu_type", vnfObject.getString("configedVduType"));
73 vdu.put("vdu_type", this.getVduType(vnfmObject, queryVms));
75 vdu.put("h_steps", vnfObject.get("numberOfSteps"));
77 scaleInfo.put("vnf_id", vnfInstanceId);
78 scaleInfo.put("scale_pattern", "without_plan");
79 scaleInfo.put("scale_type", PARAM_ZERO);
80 scaleInfo.put("scale_action", scaleType);
81 scaleInfo.put("scale_step", PARAM_ZERO);
82 scaleInfo.put("scale_step_value", PARAM_ONE);
83 scaleInfo.put("scale_group", vdu.getString("vdu_type"));
84 scaleInfo.put("vdu_list", vduList);
85 if(scaleType == PARAM_ZERO) {
87 JSONArray vmList = new JSONArray();
89 // JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
90 // vmList = additionalParam.getJSONArray("vm_list");
91 vmList = ScaleManager.beforeScaleIn(queryVms, vnfInstanceId);
92 if(vmList.isEmpty()) {
93 vmList = AdapterResourceManager.readScaleInVmIdFromJson().getJSONArray("vm_list");
95 } catch(JSONException e) {
96 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it", e);
98 if(null != vmList && !vmList.isEmpty()) {
99 scaleInfo.put("vm_list", vmList);
101 } else if(scaleType == PARAM_ONE) {
102 ScaleManager.beforeScaleOut(queryVms, vnfInstanceId);
104 paramJson.put("scale_info", scaleInfo);
105 JSONObject queryResult =
106 ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(), Constant.CERTIFICATE);
107 LOG.info("SCALE execute result:" + queryResult.toString());
109 int statusCode = queryResult.getInt(Constant.RETCODE);
111 if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
112 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
113 // restJson.put("data",
114 // queryResult.getJSONObject("data").getJSONObject("scale_info"));
115 JSONObject appInfo = new JSONObject();
117 appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("scale_info");
118 } catch(JSONException e) {
119 LOG.error("function=scaleVnf, msg=csm return obj is not a json, e={}.", e);
121 JSONObject resultObj = new JSONObject();
122 // resultObj.put(Constant.JOBID, vnfInstanceId + "_" + Constant.PUT);
123 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.PUT);
124 restJson.put("data", resultObj);
126 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
129 } catch(JSONException e) {
130 LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
136 private String getVduType(JSONObject vnfmObject, JSONObject queryResult) {
139 vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
140 } catch(Exception e) {
141 LOG.error("get vdu_type failed.", e);
143 LOG.info("vdu_type=" + vduType);
147 private int getScaleType(String type) {
148 if("SCALE_OUT".equalsIgnoreCase(type)) {
150 } else if("SCALE_IN".equalsIgnoreCase(type)) {
157 public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
158 LOG.info("function=createVnf, msg=enter to create a vnf");
159 LOG.info("createVnf csm request body :" + subJsonObject);
160 JSONObject restJson = new JSONObject();
161 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
162 String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
164 JSONObject queryResult =
165 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
166 LOG.info("createVnf csm response content:" + queryResult);
168 int statusCode = queryResult.getInt(Constant.RETCODE);
170 if(statusCode == Constant.HTTP_CREATED) {
171 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
172 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
173 JSONObject resultObj = new JSONObject();
174 // resultObj.put("vnfInstanceId", appInfo.getString("id"));
175 // resultObj.put(Constant.JOBID, appInfo.getString("id") + "_" + Constant.POST);
176 String vnfInstanceId = appInfo.getString("id");
177 handleResponse(resultObj, appInfo, vnfInstanceId, Constant.POST);
178 restJson.put("data", resultObj);
180 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
183 } catch(JSONException e) {
184 LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
190 private void handleResponse(JSONObject result, JSONObject returnObj, String vnfInstanceId, String type) {
192 if(returnObj.containsKey("job_id")) {
193 jobId = returnObj.getString("job_id") + ":job";
195 jobId = vnfInstanceId + "_" + type + ":no";
197 result.put("vnfInstanceId", vnfInstanceId);
198 result.put(Constant.JOBID, jobId);
202 public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
203 LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
204 JSONObject restJson = new JSONObject();
205 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
207 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
208 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
209 Constant.CERTIFICATE);
211 int statusCode = queryResult.getInt(Constant.RETCODE);
213 if(statusCode == Constant.HTTP_NOCONTENT || statusCode == Constant.HTTP_OK) {
214 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
215 // restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
216 JSONObject appInfo = new JSONObject();
217 if(queryResult.containsKey("data") && StringUtils.isNotEmpty(queryResult.getString("data")))
219 appInfo = JSONObject.fromObject(queryResult.getString("data"));
221 JSONObject resultObj = new JSONObject();
222 // resultObj.put(Constant.JOBID, vnfId + "_" + Constant.DELETE);
223 handleResponse(resultObj, appInfo, vnfId, Constant.DELETE);
224 restJson.put("data", resultObj);
226 LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
233 public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
234 LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
235 JSONObject restJson = new JSONObject();
236 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
238 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
239 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
241 int statusCode = queryResult.getInt(Constant.RETCODE);
243 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
244 if(null == (queryResult.get("data"))) {
245 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
248 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
249 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
251 LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
257 public JSONObject getIp(JSONObject vnfmObject, String vnfId) throws IOException {
258 LOG.warn("function=getIp, msg=enter to getIp: {}", vnfId);
259 JSONObject restJson = new JSONObject();
260 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
262 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
263 String.format(ParamConstants.VNF_CONFIGURATION_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
265 int statusCode = queryResult.getInt(Constant.RETCODE);
267 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
268 if(null == (queryResult.get("data"))) {
269 LOG.warn("function=getIp, msg=query is null {}", queryResult.get("data"));
272 JSONObject config = JSONObject.fromObject(queryResult.getString("data"));
273 LOG.info("function=getIp, query configuration result: {}", config);
274 JSONObject vnfInfo = config.getJSONArray("configuration").getJSONObject(0);
275 JSONObject result = new JSONObject();
276 result.put("vnf_id", vnfInfo.getString("vnf_id"));
277 result.put("vnf_type", vnfInfo.getString("vnf_type"));
278 JSONArray inputs = vnfInfo.getJSONArray("inputs");
280 ClassLoader classLoader = getClass().getClassLoader();
281 String ipConfig = IOUtils.toString(classLoader.getResourceAsStream("ipConfig.json"));
282 LOG.info("ipConfig: {}", ipConfig);
283 JSONObject ipCon = JSONObject.fromObject(ipConfig);
284 String vnfType = vnfInfo.getString("vnf_type");
285 if(ipCon.containsKey(vnfType)) {
286 String ipKey = ipCon.getString(vnfInfo.getString("vnf_type"));
287 LOG.info("ipKey: {}", ipKey);
289 for(int i = 0; i < inputs.size(); i++) {
290 JSONObject obj = inputs.getJSONObject(i);
291 if(obj.getString("key_name").equals(ipKey)) {
292 ip = obj.getString("value");
296 result.put("ip", ip);
297 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
298 restJson.put("data", result);
302 LOG.error("function=getIp, msg=send get vnf msg to csm get wrong status: {}", statusCode);
309 public JSONObject getJob(JSONObject vnfmObject, String jobId) {
310 LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
311 JSONObject restJson = new JSONObject();
312 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
314 String vnfId = jobId.split("_")[0];
315 JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
316 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
317 null, Constant.CERTIFICATE);
319 int statusCode = queryResult.getInt(Constant.RETCODE);
321 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
323 if((queryResult.get("data")) == null) {
324 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
327 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
328 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
330 LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
342 * @param vnfInstanceId
346 public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
347 LOG.info("healVnf request body :" + jsonObject);
348 JSONObject restJson = new JSONObject();
349 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
351 String action = jsonObject.getString("action");
352 JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
353 String vmId = getNativeVmId(affectedVm.getString("vmid"), vnfmObjcet, vnfInstanceId);
354 String path = String.format(ParamConstants.HEAL_VNF, vmId);
356 JSONObject subJsonObject = new JSONObject();
357 subJsonObject.put("type", "hard");
358 subJsonObject.put("boot_mode", "");
359 if("vmReset".equalsIgnoreCase(action)) {
360 subJsonObject.put("action", "reset");
362 LOG.info("healVnf subJsonObject :" + subJsonObject);
363 JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
364 Constant.CERTIFICATE);
366 int statusCode = healResult.getInt(Constant.RETCODE);
367 if(statusCode == Constant.HTTP_OK) {
368 LOG.info("healResult:{}", healResult);
369 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
371 LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
377 private String getNativeVmId(String vimVmId, JSONObject vnfmObjcet, String vnfInstanceId) {
378 JSONObject queryVms = ResultRequestUtil.call(vnfmObjcet,
379 String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId), Constant.GET, null, Constant.CERTIFICATE);
380 LOG.info("function=getNativeVmId, msg=query vms result=" + queryVms);
381 JSONArray vms = queryVms.getJSONObject("data").getJSONArray("vms");
382 for(int i = 0; i < vms.size(); i++) {
383 JSONObject obj = vms.getJSONObject(i);
384 String tmpVimVmId = obj.getString("backend_id");
385 if(vimVmId.equalsIgnoreCase(tmpVimVmId)) {
386 return obj.getString("id");
389 LOG.error("function=getNativeVmId, msg=get native vmId failed.");
393 public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
394 LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
396 JSONObject restJson = new JSONObject();
397 restJson.put(Constant.RETCODE, Constant.REST_FAIL);
399 JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
400 Constant.GET, null, Constant.CERTIFICATE);
402 int statusCode = queryResult.getInt(Constant.RETCODE);
403 if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
404 if((queryResult.get("data")) == null) {
405 LOG.warn("function=getJobFromVnfm, msg=query is null {}", queryResult.get("data"));
408 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
409 restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
411 LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);