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.rest;
 
  19 import java.io.IOException;
 
  21 import javax.servlet.http.HttpServletRequest;
 
  22 import javax.ws.rs.Consumes;
 
  23 import javax.ws.rs.PUT;
 
  24 import javax.ws.rs.Path;
 
  25 import javax.ws.rs.PathParam;
 
  26 import javax.ws.rs.Produces;
 
  27 import javax.ws.rs.core.Context;
 
  28 import javax.ws.rs.core.MediaType;
 
  30 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.VnfmJsonUtil;
 
  31 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.restclient.RestfulResponse;
 
  32 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.common.servicetoken.VnfmRestfulUtil;
 
  33 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.adapter.impl.AdapterResourceManager;
 
  34 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.constant.Constant;
 
  35 import org.onap.vfc.nfvo.vnfm.svnfm.vnfmadapter.service.process.VnfResourceMgr;
 
  36 import org.slf4j.Logger;
 
  37 import org.slf4j.LoggerFactory;
 
  39 import net.sf.json.JSONArray;
 
  40 import net.sf.json.JSONObject;
 
  43  * Provide interfaces of resource for VNFM.
 
  47  * @version VFC 1.0 Aug 24, 2016
 
  49 @Path("/rest/vnfmmed/csm/v2/vapps")
 
  50 @Consumes(MediaType.APPLICATION_JSON)
 
  51 @Produces(MediaType.APPLICATION_JSON)
 
  52 public class VnfResourceRoa {
 
  54     private static final Logger LOG = LoggerFactory.getLogger(VnfResourceRoa.class);
 
  56     private VnfResourceMgr vnfResourceMgr;
 
  58     public void setVnfResourceMgr(VnfResourceMgr vnfResourceMgr) {
 
  59         this.vnfResourceMgr = vnfResourceMgr;
 
  63      * Provide function of grant resource.
 
  72     @Path("/instances/{vnfId}/grant")
 
  73     public String grantVnfRes(@Context HttpServletRequest context, @PathParam("vnfId") String vnfId) {
 
  74         LOG.info("function=grantVnfRes, msg=enter to grant vnf resource.");
 
  75         JSONObject restJson = new JSONObject();
 
  76         restJson.put(Constant.RETCODE, Constant.REST_FAIL);
 
  78         JSONObject dataObject = VnfmJsonUtil.getJsonFromContexts(context);
 
  79         LOG.info("function=grantVnfRes, dataObject: {}", dataObject);
 
  80         if(null == dataObject) {
 
  81             LOG.error("function=grantVnfRes, msg=param error");
 
  82             restJson.put("data", "Params error");
 
  83             return restJson.toString();
 
  86         JSONObject grantObj = dataObject.getJSONObject("grant");
 
  88         if(null == grantObj) {
 
  89             LOG.error("function=grantVnfRes, msg=param error");
 
  90             restJson.put("data", "Grant param error");
 
  91             return restJson.toString();
 
  94         String vnfmId = grantObj.getString("project_id");
 
  96         JSONObject resultObj = vnfResourceMgr.grantVnfResource(grantObj, vnfId, vnfmId);
 
  97         LOG.info("grantVnfResource resultObj:", resultObj);
 
  98         JSONObject res = new JSONObject();
 
  99         res.put("msg", "grant success");
 
 100         return res.toString();
 
 104     @Path("/lifecycle_changes_notification")
 
 105     public String notify(@Context HttpServletRequest context) throws IOException {
 
 106         LOG.info("function=notify, msg=enter to notify vnf resource");
 
 107         JSONObject dataObject = VnfmJsonUtil.getJsonFromContexts(context);
 
 108         LOG.info("function=notify, dataObject: {}", dataObject);
 
 109         callLcmNotify(dataObject);
 
 110         JSONObject restJson = new JSONObject();
 
 111         restJson.put(Constant.RETCODE, Constant.REST_SUCCESS);
 
 112         return restJson.toString();
 
 115     private void callLcmNotify(JSONObject dataObject) throws IOException {
 
 116         String vnfPkgInfo = AdapterResourceManager.readVfnPkgInfoFromJson();
 
 117         JSONObject vnfpkgJson = JSONObject.fromObject(vnfPkgInfo);
 
 118         String vnfmId = vnfpkgJson.getString("vnfmid");
 
 119         String vimId = vnfpkgJson.getString("vimid");
 
 120         JSONArray affectedVnfc = new JSONArray();
 
 121         JSONArray vmList = dataObject.getJSONArray("vm_list");
 
 122         String changeType = "";
 
 123         String operation = "";
 
 124         if(1 == dataObject.getInt("event_type")) {
 
 125             changeType = "added";
 
 126             operation = "Instantiate";
 
 128             changeType = "removed";
 
 129             operation = "Terminal";
 
 131         String vnfcInstanceId = dataObject.getString("vnf_id");
 
 132         for(int i = 0; i < vmList.size(); i++) {
 
 133             JSONObject vm = vmList.getJSONObject(i);
 
 134             LOG.info("function=callLcmNotify, vm: {}", vm);
 
 135             JSONObject affectedVm = new JSONObject();
 
 136             affectedVm.put("vnfcInstanceId", vnfcInstanceId);
 
 137             affectedVm.put("changeType", changeType);
 
 138             affectedVm.put("vimid", vimId);
 
 139             affectedVm.put("vmid", vm.getString("vm_id"));
 
 140             affectedVm.put("vmname", vm.getString("vm_name"));
 
 141             LOG.info("function=callLcmNotify, affectedVm: {}", affectedVm);
 
 142             affectedVnfc.add(affectedVm);
 
 144         JSONObject notification = new JSONObject();
 
 145         notification.put("status", dataObject.getString("vnf_status"));
 
 146         notification.put("vnfInstanceId", vnfcInstanceId);
 
 147         notification.put("operation", operation);
 
 148         notification.put("affectedVnfc", affectedVnfc);
 
 149         LOG.info("function=callLcmNotify, notification: {}", notification);
 
 150         String url = "/api/nslcm/v1/ns/" + vnfmId + "/vnfs/" + vnfcInstanceId + "/Notify";
 
 151         LOG.info("function=callLcmNotify, url: {}", url);
 
 152         RestfulResponse rsp =
 
 153                 VnfmRestfulUtil.getRemoteResponse(url, VnfmRestfulUtil.TYPE_POST, notification.toString());
 
 155             LOG.info("function=callLcmNotify, status: {}, content: {}", rsp.getStatus(), rsp.getResponseContent());