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);
 
  43     public JSONObject scaleVnf(JSONObject vnfObject, JSONObject vnfmObject, String vnfmId, String vnfInstanceId) {
 
  44         LOG.warn("function=scaleVnf, msg=enter to scale a vnf");
 
  45         JSONObject restJson = new JSONObject();
 
  46         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
 
  47         String path = String.format(ParamConstants.VNF_SCALE, vnfInstanceId);
 
  49         int scaleType = getScaleType(vnfObject.getString("type"));
 
  50         // build request json object
 
  51         JSONObject paramJson = new JSONObject();
 
  52         JSONObject scaleInfo = new JSONObject();
 
  53         JSONArray vduList = new JSONArray();
 
  54         JSONObject vdu = new JSONObject();
 
  55         vdu.put("vdu_type", this.getVduType(vnfmObject, vnfInstanceId));
 
  56         vdu.put("h_steps", vnfObject.get("numberOfSteps"));
 
  58         scaleInfo.put("vnf_id", vnfInstanceId);
 
  59         scaleInfo.put("scale_type", 0);
 
  60         scaleInfo.put("scale_action", scaleType);
 
  61         scaleInfo.put("vdu_list", vduList);
 
  62         if(scaleType == 0) {// scale_in
 
  63             JSONArray vmList = new JSONArray();
 
  65                 JSONObject additionalParam = vnfObject.getJSONObject("additionalParam");
 
  66                 vmList = additionalParam.getJSONArray("vm_list");
 
  67             } catch(JSONException e) {
 
  68                 LOG.error("the param 'additionalParam' or 'vm_list' not found,please check it", e);
 
  70             scaleInfo.put("vm_list", vmList);
 
  72         paramJson.put("scale_info", scaleInfo);
 
  73         JSONObject queryResult =
 
  74                 ResultRequestUtil.call(vnfmObject, path, Constant.PUT, paramJson.toString(), Constant.CERTIFICATE);
 
  75         LOG.info("SCALE execute result:" + queryResult.toString());
 
  77             int statusCode = queryResult.getInt(Constant.RETCODE);
 
  79             if(statusCode == Constant.HTTP_CREATED || statusCode == Constant.HTTP_OK) {
 
  80                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
  81                 JSONObject resultObj = new JSONObject();
 
  82                 resultObj.put(Constant.JOBID, vnfInstanceId + "_" + Constant.PUT);
 
  83                 restJson.put("data", resultObj);
 
  85                 LOG.error("function=scaleVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
 
  88         } catch(JSONException e) {
 
  89             LOG.error("function=scaleVnf, msg=parse scale vnf return data occoured JSONException, e={}.", e);
 
  95     private String getVduType(JSONObject vnfmObject, String vnfInstanceId) {
 
  98             JSONObject queryResult =
 
  99                     ResultRequestUtil.call(vnfmObject, String.format(ParamConstants.VNF_GET_VMINFO, vnfInstanceId),
 
 100                             Constant.GET, null, Constant.CERTIFICATE);
 
 101             LOG.info("getVduType result=" + queryResult);
 
 102             vduType = queryResult.getJSONObject("data").getJSONArray("vms").getJSONObject(0).getString("vdu_type");
 
 103         } catch(Exception e) {
 
 104             LOG.error("get vdu_type failed.", e);
 
 106         LOG.info("vdu_type=" + vduType);
 
 110     private int getScaleType(String type) {
 
 111         if("SCALE_OUT".equalsIgnoreCase(type)) {
 
 113         } else if("SCALE_IN".equalsIgnoreCase(type)) {
 
 120     public JSONObject createVnf(JSONObject subJsonObject, JSONObject vnfmObject) {
 
 121         LOG.info("function=createVnf, msg=enter to create a vnf");
 
 122         LOG.info("createVnf csm request body :" + subJsonObject);
 
 123         JSONObject restJson = new JSONObject();
 
 124         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
 
 125         String path = ParamConstants.VNF_INSTANCE + Constant.ROARAND;
 
 127         JSONObject queryResult =
 
 128                 ResultRequestUtil.call(vnfmObject, path, Constant.POST, subJsonObject.toString(), Constant.CERTIFICATE);
 
 129         LOG.info("createVnf csm response content:" + queryResult);
 
 131             int statusCode = queryResult.getInt(Constant.RETCODE);
 
 133             if(statusCode == Constant.HTTP_CREATED) {
 
 134                 restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 135                 JSONObject appInfo = JSONObject.fromObject(queryResult.getString("data")).getJSONObject("app_info");
 
 136                 JSONObject resultObj = new JSONObject();
 
 137                 resultObj.put("vnfInstanceId", appInfo.getString("id"));
 
 138                 resultObj.put(Constant.JOBID, appInfo.getString("id") + "_" + Constant.POST);
 
 139                 restJson.put("data", resultObj);
 
 141                 LOG.error("function=createVnf, msg=send create vnf msg to csm get wrong status: " + statusCode);
 
 144         } catch(JSONException e) {
 
 145             LOG.error("function=createVnf, msg=parse create vnf return data occoured JSONException, e={}.", e);
 
 152     public JSONObject removeVnf(JSONObject vnfmObject, String vnfId, JSONObject vnfObject) {
 
 153         LOG.warn("function=removeVnf, msg=enter to remove a vnf: {}", vnfId);
 
 154         JSONObject restJson = new JSONObject();
 
 155         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
 
 157         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
 
 158                 String.format(ParamConstants.VNF_INSTANCE_DEL, vnfId) + Constant.ROARAND, Constant.DELETE, null,
 
 159                 Constant.CERTIFICATE);
 
 161         int statusCode = queryResult.getInt(Constant.RETCODE);
 
 163         if(statusCode == Constant.HTTP_NOCONTENT || statusCode == Constant.HTTP_OK) {
 
 164             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 165             JSONObject resultObj = new JSONObject();
 
 166             resultObj.put(Constant.JOBID, vnfId + "_" + Constant.DELETE);
 
 167             restJson.put("data", resultObj);
 
 169             LOG.error("function=removeVnf, msg=send remove vnf msg to csm get wrong status: {}", statusCode);
 
 176     public JSONObject getVnf(JSONObject vnfmObject, String vnfId) {
 
 177         LOG.warn("function=getVnf, msg=enter to get a vnf: {}", vnfId);
 
 178         JSONObject restJson = new JSONObject();
 
 179         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
 
 181         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
 
 182                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
 
 183                 null, Constant.CERTIFICATE);
 
 185         int statusCode = queryResult.getInt(Constant.RETCODE);
 
 187         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
 
 188             if(null == (queryResult.get("data"))) {
 
 189                 LOG.warn("function=getVnf, msg=query is null {}", queryResult.get("data"));
 
 192             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 193             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
 
 195             LOG.error("function=getVnf, msg=send get vnf msg to csm get wrong status: {}", statusCode);
 
 202     public JSONObject getJob(JSONObject vnfmObject, String jobId) {
 
 203         LOG.warn("function=getJob, msg=enter to get a job: {}", jobId);
 
 204         JSONObject restJson = new JSONObject();
 
 205         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
 
 207         String vnfId = jobId.split("_")[0];
 
 208         JSONObject queryResult = ResultRequestUtil.call(vnfmObject,
 
 209                 String.format(ParamConstants.VNF_INSTANCE_GET, vnfId) + Constant.ROARAND + "&type=status", Constant.GET,
 
 210                 null, Constant.CERTIFICATE);
 
 212         int statusCode = queryResult.getInt(Constant.RETCODE);
 
 214         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
 
 216             if((queryResult.get("data")) == null) {
 
 217                 LOG.warn("function=getJob, msg=query is null {}", queryResult.get("data"));
 
 220             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 221             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")).getJSONArray("basic"));
 
 223             LOG.error("function=getJob, msg=send get vnf msg to csm get wrong status: {}", statusCode);
 
 235      * @param vnfInstanceId
 
 239     public JSONObject healVnf(JSONObject jsonObject, JSONObject vnfmObjcet, String vnfmId, String vnfInstanceId) {
 
 240         LOG.info("healVnf request body :" + jsonObject);
 
 241         JSONObject restJson = new JSONObject();
 
 242         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
 
 244         String action = jsonObject.getString("action");
 
 245         JSONObject affectedVm = jsonObject.getJSONObject("affectedvm");
 
 246         String vmId = affectedVm.getString("vmid");
 
 247         String path = String.format(ParamConstants.HEAL_VNF, vmId);
 
 249         JSONObject subJsonObject = new JSONObject();
 
 250         subJsonObject.put("type", "hard");
 
 251         subJsonObject.put("boot_mode", "");
 
 252         if("vmReset".equals(action)) {
 
 253             subJsonObject.put("action", "reset");
 
 255         LOG.info("healVnf subJsonObject :" + subJsonObject);
 
 256         JSONObject healResult = ResultRequestUtil.callSouth(vnfmObjcet, path, Constant.PUT, subJsonObject.toString(),
 
 257                 Constant.CERTIFICATE);
 
 259         int statusCode = healResult.getInt(Constant.RETCODE);
 
 260         if(statusCode == Constant.HTTP_OK) {
 
 262             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 264             LOG.error("function=healVnf, msg=send heal vnf msg to csm get wrong status: {}", statusCode);
 
 270     public JSONObject getJobFromVnfm(JSONObject vnfmObjcet, String jobId) {
 
 271         LOG.warn("function=getJobFromVnfm, jobId: {}", jobId);
 
 272         JSONObject restJson = new JSONObject();
 
 273         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
 
 275         JSONObject queryResult = ResultRequestUtil.call(vnfmObjcet, String.format(UrlConstant.URL_JOBSTATUS_GET, jobId),
 
 276                 Constant.GET, null, Constant.CERTIFICATE);
 
 278         int statusCode = queryResult.getInt(Constant.RETCODE);
 
 279         if(statusCode == Constant.HTTP_OK || statusCode == Constant.HTTP_CREATED) {
 
 280             if((queryResult.get("data")) == null) {
 
 281                 LOG.warn("function=getJobFromVnfm, msg=query is null {}", queryResult.get("data"));
 
 284             restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 285             restJson.put("data", JSONObject.fromObject(queryResult.getString("data")));
 
 287             LOG.error("function=getJobFromVnfm, msg=query job from vnfm wrong status: {}", statusCode);