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 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;
 
  27 import net.sf.json.JSONArray;
 
  28 import net.sf.json.JSONException;
 
  29 import net.sf.json.JSONObject;
 
  32  * create or terminate VNF to M
 
  36  * @version VFC 1.0 Aug 24, 2016
 
  38 public class VnfMgrVnfm implements InterfaceVnfMgr {
 
  40     private static final Logger LOG = LoggerFactory.getLogger(VnfMgrVnfm.class);
 
  42     private static final int PARAM_ZERO = 0;
 
  44     private static final int PARAM_ONE = 1;
 
  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);
 
  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"));
 
  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) {
 
  72             JSONArray vmList = new JSONArray();
 
  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);
 
  79             scaleInfo.put("vm_list", vmList);
 
  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());
 
  86             int statusCode = queryResult.getInt(Constant.RETCODE);
 
  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"));
 
  92                 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
 
  95         } catch(JSONException e) {
 
  96             LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
 
 102     private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
 
 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);
 
 113         LOG.info("vdu_type=" + vduType);
 
 117     private int getScaleType(String type) {
 
 118         if("SCALE_OUT".equalsIgnoreCase(type)) {
 
 120         } else if("SCALE_IN".equalsIgnoreCase(type)) {
 
 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;
 
 134         JSONObject queryResult =
 
 135                 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
 
 136         LOG.info("createVnf csm response content:" + queryResult);
 
 138             int statusCode = queryResult.getInt(Constant.RETCODE);
 
 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);
 
 148                 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
 
 151         } catch(JSONException e) {
 
 152             LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
 
 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);
 
 164         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
 
 165                 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
 
 166                 Constant.CERTIFICATE);
 
 168         int statusCode = queryResult.getInt(Constant.RETCODE);
 
 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")));
 
 174             LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
 
 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);
 
 186         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
 
 187                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId), Constant.GET, null, Constant.CERTIFICATE);
 
 189         int statusCode = queryResult.getInt(Constant.RETCODE);
 
 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"));
 
 196             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 197             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("vnf_list"));
 
 199             LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
 
 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);
 
 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);
 
 216         int statusCode = queryResult.getInt(Constant.RETCODE);
 
 218         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
 
 220             if((queryResult.get("data")) == null) {
 
 221                 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
 
 224             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 225             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
 
 227             LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
 
 239      * @param vnfInstanceId
 
 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);
 
 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);
 
 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");
 
 259         LOG.info("healVnf subJsonObject :" + subJsonObject);
 
 260         JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
 
 261                 Constant.CERTIFICATE);
 
 263         int statusCode = healResult.getInt(Constant.RETCODE);
 
 264         if(statusCode == Constant.HTTP_OK) {
 
 266             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 268             LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
 
 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);
 
 279         JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
 
 280                 Constant.GET, null, Constant.CERTIFICATE);
 
 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"));
 
 288             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 289             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
 
 291             LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);