2 * Copyright 2016 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.gvnfm.jujuvnfmadapter.service.rest;
19 import javax.servlet.http.HttpServletRequest;
20 import javax.ws.rs.Consumes;
21 import javax.ws.rs.PUT;
22 import javax.ws.rs.Path;
23 import javax.ws.rs.PathParam;
24 import javax.ws.rs.Produces;
25 import javax.ws.rs.core.Context;
26 import javax.ws.rs.core.MediaType;
28 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.EntityUtils;
29 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.common.StringUtil;
30 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.constant.Constant;
31 import org.onap.vfc.nfvo.vnfm.gvnfm.jujuvnfmadapter.service.process.VnfResourceMgr;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import net.sf.json.JSONObject;
38 * Provide interfaces of resource for juju VNFM to call.
42 * @version NFVO 0.5 Aug 24, 2016
45 @Consumes(MediaType.APPLICATION_JSON)
46 @Produces(MediaType.APPLICATION_JSON)
47 public class VnfResourceRoa {
49 private static final Logger LOG = LoggerFactory.getLogger(VnfResourceRoa.class);
51 private VnfResourceMgr vnfResourceMgr;
53 public void setVnfResourceMgr(VnfResourceMgr vnfResourceMgr) {
54 this.vnfResourceMgr = vnfResourceMgr;
58 * Provide function of grant resource.
61 * field:(cpu,mem,disk,action(addResource/removeResource))
68 @Path("/instances/{vnfId}/grant")
69 public String grantVnfRes(@Context HttpServletRequest context, @PathParam("vnfId") String vnfId) {
70 LOG.info("function=grantVnfRes, msg=enter to grant vnf resource");
71 JSONObject restJson = new JSONObject();
72 restJson.put(EntityUtils.RESULT_CODE_KEY, Constant.REST_FAIL);
74 JSONObject compute = StringUtil.getJsonFromContexts(context);
76 LOG.error("function=grantVnfRes, msg=param request content can't be null!");
77 restJson.put("data", "param request content can't be null!");
78 return restJson.toString();
81 JSONObject resultObj = vnfResourceMgr.grantVnfResource(compute, vnfId);
82 handleResult(resultObj, restJson);
84 return restJson.toString();
87 private void handleResult(JSONObject resultObj, JSONObject restJson) {
88 if(resultObj.getInt("retCode") == Constant.REST_SUCCESS) {
89 restJson.put("retCode", Constant.REST_SUCCESS);
90 restJson.put("data", resultObj.getJSONObject("data"));
92 if(resultObj.containsKey("data")) {
93 String errorMsg = resultObj.getString("data");
94 LOG.error("function=handleResult, msg={}", errorMsg);
95 restJson.put("data", errorMsg);
97 LOG.error("function=handleResult, msg=Vnf Resource dispose fail");
98 restJson.put("data", "Vnf Resource dispose fail");