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 int eventType = dataObject.getInt("event_type");
126 changeType = "added";
127 operation = "Instantiate";
128 } else if(4 == eventType) {
129 changeType = "removed";
130 operation = "Terminal";
131 } else if(3 == eventType) {
132 changeType = "added";
133 operation = "Scaleout";
134 } else if(2 == eventType) {
135 changeType = "removed";
136 operation = "Scalein";
138 String vnfInstanceId = dataObject.getString("vnf_id");
139 for(int i = 0; i < vmList.size(); i++) {
140 JSONObject vm = vmList.getJSONObject(i);
141 LOG.info("function=callLcmNotify, vm: {}", vm);
142 if((2 == eventType) && ("Active".equalsIgnoreCase(vm.getString("status")))) {
145 JSONObject affectedVm = new JSONObject();
146 String vimVimId = vm.getString("vim_vm_id");
147 affectedVm.put("vnfcInstanceId", vimVimId);
148 affectedVm.put("changeType", changeType);
149 affectedVm.put("vimid", vimId);
150 affectedVm.put("vmid", vimVimId);
151 affectedVm.put("vmname", vm.getString("vm_name"));
152 affectedVm.put("vduid", vimVimId);
153 LOG.info("function=callLcmNotify, affectedVm: {}", affectedVm);
154 affectedVnfc.add(affectedVm);
156 if(affectedVnfc.isEmpty()) {
157 LOG.warn("function=callLcmNotify, affectedVnfc is empty.");
160 JSONObject notification = new JSONObject();
161 notification.put("status", dataObject.getString("vnf_status"));
162 notification.put("vnfInstanceId", vnfInstanceId);
163 notification.put("operation", operation);
164 notification.put("affectedVnfc", affectedVnfc);
165 LOG.info("function=callLcmNotify, notification: {}", notification);
166 String url = "/api/nslcm/v1/ns/" + vnfmId + "/vnfs/" + vnfInstanceId + "/Notify";
167 LOG.info("function=callLcmNotify, url: {}", url);
168 RestfulResponse rsp =
169 VnfmRestfulUtil.getRemoteResponse(url, VnfmRestfulUtil.TYPE_POST, notification.toString());
171 LOG.info("function=callLcmNotify, status: {}, content: {}", rsp.getStatus(), rsp.getResponseContent());