Update gvnfm-driver .gitreview file
[vfc/nfvo/driver/vnfm/gvnfm.git] / juju / juju-vnfmadapter / Juju-vnfmadapterService / service / src / main / java / org / openo / nfvo / jujuvnfmadapter / service / rest / VnfResourceRoa.java
1 /*
2  * Copyright 2016 Huawei Technologies Co., Ltd.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openo.nfvo.jujuvnfmadapter.service.rest;
18
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;
27
28 import org.openo.nfvo.jujuvnfmadapter.common.EntityUtils;
29 import org.openo.nfvo.jujuvnfmadapter.common.StringUtil;
30 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
31 import org.openo.nfvo.jujuvnfmadapter.service.process.VnfResourceMgr;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 import net.sf.json.JSONObject;
36
37 /**
38  * Provide interfaces of resource for juju VNFM to call. 
39  * <br/>
40  *
41  * @author
42  * @version NFVO 0.5 Aug 24, 2016
43  */
44 @Path("/v1")
45 @Consumes(MediaType.APPLICATION_JSON)
46 @Produces(MediaType.APPLICATION_JSON)
47 public class VnfResourceRoa {
48
49     private static final Logger LOG = LoggerFactory.getLogger(VnfResourceRoa.class);
50
51     private VnfResourceMgr vnfResourceMgr;
52
53     public void setVnfResourceMgr(VnfResourceMgr vnfResourceMgr) {
54         this.vnfResourceMgr = vnfResourceMgr;
55     }
56
57     /**
58      * Provide function of grant resource.
59      * <br/>
60      *JSONObject compute :
61      * field:(cpu,mem,disk,action(addResource/removeResource))
62      * @param context
63      * @param vnfId
64      * @return
65      * @since NFVO 0.5
66      */
67     @PUT
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);
73
74         JSONObject compute = StringUtil.getJsonFromContexts(context);
75         if(null == compute) {
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();
79         }
80
81         JSONObject resultObj = vnfResourceMgr.grantVnfResource(compute, vnfId);
82         handleResult(resultObj, restJson);
83
84         return restJson.toString();
85     }
86
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"));
91         } else {
92             if(resultObj.containsKey("data")) {
93                 String errorMsg = resultObj.getString("data");
94                 LOG.error("function=handleResult, msg={}", errorMsg);
95                 restJson.put("data", errorMsg);
96             } else {
97                 LOG.error("function=handleResult, msg=Vnf Resource dispose fail");
98                 restJson.put("data", "Vnf Resource dispose fail");
99             }
100         }
101     }
102 }