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 / VnfRoa.java
1 /*
2  * Copyright 2016-2017 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 java.util.HashMap;
20 import java.util.Map;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.DELETE;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.Path;
29 import javax.ws.rs.PathParam;
30 import javax.ws.rs.Produces;
31 import javax.ws.rs.QueryParam;
32 import javax.ws.rs.core.Context;
33 import javax.ws.rs.core.MediaType;
34
35 import org.apache.commons.collections.map.UnmodifiableMap;
36 import org.apache.commons.lang3.StringUtils;
37 import org.openo.baseservice.remoteservice.exception.ServiceException;
38 import org.openo.nfvo.jujuvnfmadapter.common.EntityUtils;
39 import org.openo.nfvo.jujuvnfmadapter.common.StringUtil;
40 import org.openo.nfvo.jujuvnfmadapter.common.SwitchController;
41 import org.openo.nfvo.jujuvnfmadapter.service.constant.Constant;
42 import org.openo.nfvo.jujuvnfmadapter.service.process.VnfMgr;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import net.sf.json.JSONObject;
47
48 /**
49  * Provide interfaces for instantiate or terminate VNF.
50  * <br/>
51  * 
52  * @author
53  * @version NFVO 0.5 Aug 24, 2016
54  */
55 @SuppressWarnings("unchecked")
56 @Path("/v1")
57 @Consumes(MediaType.APPLICATION_JSON)
58 @Produces(MediaType.APPLICATION_JSON)
59 public class VnfRoa {
60
61     private static final Logger LOG = LoggerFactory.getLogger(VnfRoa.class);
62
63     private VnfMgr vnfMgr;
64
65     private static Map<String, String> progressItem;
66
67     private static Map<String, String> jobStatusitem;
68
69     static {
70         Map<String, String> map = new HashMap<>();
71         map.put("Building", "50");
72         map.put("Active", "100");
73         map.put("Stopped", "50");
74         map.put("Error", "100");
75         progressItem = UnmodifiableMap.decorate(map);
76
77         map = new HashMap<>();
78         map.put("Building", "processing");
79         map.put("Active", "finished");
80         map.put("Stopped", "processing");
81         map.put("Error", "error");
82         jobStatusitem = UnmodifiableMap.decorate(map);
83     }
84
85     public void setVnfMgr(VnfMgr vnfMgr) {
86         this.vnfMgr = vnfMgr;
87     }
88
89
90     @POST
91     @Path("/vnfminfo")
92     public String setVNFMInfo(@Context HttpServletRequest context, @Context HttpServletResponse resp)
93             throws ServiceException {
94         JSONObject result = new JSONObject();
95         result.put("retCode", Constant.REST_SUCCESS);
96         JSONObject reqJsonObject = StringUtil.getJsonFromContexts(context);
97         String vnfmServiceUrl = reqJsonObject.getString("url");
98         SwitchController.vnfmServiceUrl = vnfmServiceUrl;
99         LOG.info(reqJsonObject + ":setVNFMInfo success!");
100         return result.toString();
101     }
102
103     /**
104      * Provide function for instantiate VNF
105      * <br/>
106      * 
107      * @param context
108      * @param resp
109      * @param vnfmId
110      * @return
111      * @throws ServiceException
112      * @since NFVO 0.5
113      */
114     @POST
115     @Path("/{vnfmId}/vnfs")
116     public String addVnf(@Context HttpServletRequest context, @Context HttpServletResponse resp,
117             @PathParam("vnfmId") String vnfmId) throws ServiceException {
118         LOG.warn("function=addVnf, msg=enter to add a vnf");
119         JSONObject subJsonObject = StringUtil.getJsonFromContexts(context);
120         LOG.info("request context:"+subJsonObject);
121         JSONObject restJson = new JSONObject();
122
123         if(null == subJsonObject) {
124             LOG.error("function=addVnf, msg=params are insufficient");
125             resp.setStatus(Constant.HTTP_INNERERROR);
126             return restJson.toString();
127         }
128
129         restJson = vnfMgr.addVnf(subJsonObject, vnfmId);
130
131         if(restJson.getInt(EntityUtils.RESULT_CODE_KEY) == Constant.REST_FAIL) {
132             LOG.error("function=addVnf, msg=addvnf fail");
133             resp.setStatus(Constant.HTTP_INNERERROR);
134             return restJson.toString();
135         }
136
137         return JSONObject.fromObject(restJson.getJSONObject("data")).toString();
138     }
139     
140     /**
141      * Provide function for terminate VNF
142      * <br/>
143      * 
144      * @param vnfmId
145      * @param resp
146      * @param vnfInstanceId
147      * @param context
148      * @return
149      * @throws ServiceException
150      * @since NFVO 0.5
151      */
152     @DELETE
153     @Path("/{vnfmId}/vnfs/{vnfInstanceId}/terminate")
154     public String delVnfDel(@PathParam("vnfmId") String vnfmId, @Context HttpServletResponse resp,
155             @PathParam("vnfInstanceId") String vnfInstanceId, @Context HttpServletRequest context)
156             throws ServiceException {
157
158         return this.delVnf(vnfmId, resp, vnfInstanceId, context);
159     }
160
161     /**
162      * Provide function for terminate VNF
163      * <br/>
164      * 
165      * @param vnfmId
166      * @param resp
167      * @param vnfInstanceId
168      * @param context
169      * @return
170      * @throws ServiceException
171      * @since NFVO 0.5
172      */
173     @POST
174     @Path("/{vnfmId}/vnfs/{vnfInstanceId}/terminate")
175     public String delVnf(@PathParam("vnfmId") String vnfmId, @Context HttpServletResponse resp,
176             @PathParam("vnfInstanceId") String vnfInstanceId, @Context HttpServletRequest context)
177             throws ServiceException {
178         LOG.warn("function=delVnf, msg=enter to delete a vnf: vnfInstanceId: {}, vnfmId: {}", vnfInstanceId, vnfmId);
179         JSONObject vnfObject = StringUtil.getJsonFromContexts(context);
180         LOG.info("request context:"+vnfObject);
181         JSONObject restJson = new JSONObject();
182
183         if(StringUtils.isEmpty(vnfInstanceId) || StringUtils.isEmpty(vnfmId)) {
184             resp.setStatus(Constant.HTTP_INNERERROR);
185             return restJson.toString();
186         }
187
188         restJson = vnfMgr.deleteVnf(vnfInstanceId, vnfmId, vnfObject);
189         if(restJson.getInt(EntityUtils.RESULT_CODE_KEY) == Constant.REST_FAIL) {
190             LOG.error("function=delVnf, msg=delVnf fail");
191             resp.setStatus(Constant.HTTP_INNERERROR);
192             return restJson.toString();
193         }
194
195         return JSONObject.fromObject(restJson.getJSONObject("data")).toString();
196     }
197
198     /**
199      * Provide function for get VNF
200      * <br/>
201      * 
202      * @param vnfmId
203      * @param resp
204      * @param vnfInstanceId
205      * @param context
206      * @return
207      * @throws ServiceException
208      * @since NFVO 0.5
209      */
210     @GET
211     @Path("/{vnfmId}/vnfs/{vnfInstanceId}")
212     public String getVnf(@PathParam("vnfmId") String vnfmId, @Context HttpServletResponse resp,
213             @PathParam("vnfInstanceId") String vnfInstanceId, @Context HttpServletRequest context)
214             throws ServiceException {
215         LOG.warn("function=getVnf, msg=enter to get a vnf: vnfInstanceId: {}, vnfmId: {}", vnfInstanceId, vnfmId);
216         JSONObject restJson = new JSONObject();
217
218         if(StringUtils.isEmpty(vnfInstanceId) || StringUtils.isEmpty(vnfmId)) {
219             resp.setStatus(Constant.HTTP_INNERERROR);
220             return restJson.toString();
221         }
222
223         restJson = vnfMgr.getVnf(vnfInstanceId, vnfmId);
224         if(restJson.getInt(EntityUtils.RESULT_CODE_KEY) == Constant.REST_FAIL) {
225             LOG.error("function=getVnf, msg=getVnf fail");
226             resp.setStatus(Constant.HTTP_INNERERROR);
227             return restJson.toString();
228         }
229
230         restJson.remove(EntityUtils.RESULT_CODE_KEY);
231         return restJson.toString();
232     }
233
234     /**
235      * Provide function for get job
236      * <br/>
237      * 
238      * @param jobId
239      * @param vnfmId
240      * @param resp
241      * @param responseId
242      * @return
243      * @throws ServiceException
244      * @since NFVO 0.5
245      */
246     @GET
247     @Path("/{vnfmId}/jobs/{jobId}")
248     public String getJob(@PathParam("jobId") String jobId, @PathParam("vnfmId") String vnfmId,
249             @Context HttpServletResponse resp, @QueryParam("@responseId") String responseId) throws ServiceException {
250         LOG.warn("function=getJob, msg=enter to get a job: jobId: {}", jobId);
251         JSONObject restJson = new JSONObject();
252         restJson = vnfMgr.getJob(jobId, vnfmId);
253
254         return restJson.toString();
255     }
256 }