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.openo.nfvo.jujuvnfmadapter.service.rest;
19 import java.util.HashMap;
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;
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;
46 import net.sf.json.JSONObject;
49 * Provide interfaces for instantiate or terminate VNF.
53 * @version NFVO 0.5 Aug 24, 2016
55 @SuppressWarnings("unchecked")
57 @Consumes(MediaType.APPLICATION_JSON)
58 @Produces(MediaType.APPLICATION_JSON)
61 private static final Logger LOG = LoggerFactory.getLogger(VnfRoa.class);
63 private VnfMgr vnfMgr;
65 private static Map<String, String> progressItem;
67 private static Map<String, String> jobStatusitem;
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);
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);
85 public void setVnfMgr(VnfMgr vnfMgr) {
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();
104 * Provide function for instantiate VNF
111 * @throws ServiceException
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();
123 if(null == subJsonObject) {
124 LOG.error("function=addVnf, msg=params are insufficient");
125 resp.setStatus(Constant.HTTP_INNERERROR);
126 return restJson.toString();
129 restJson = vnfMgr.addVnf(subJsonObject, vnfmId);
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();
137 return JSONObject.fromObject(restJson.getJSONObject("data")).toString();
141 * Provide function for terminate VNF
146 * @param vnfInstanceId
149 * @throws ServiceException
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 {
158 return this.delVnf(vnfmId, resp, vnfInstanceId, context);
162 * Provide function for terminate VNF
167 * @param vnfInstanceId
170 * @throws ServiceException
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();
183 if(StringUtils.isEmpty(vnfInstanceId) || StringUtils.isEmpty(vnfmId)) {
184 resp.setStatus(Constant.HTTP_INNERERROR);
185 return restJson.toString();
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();
195 return JSONObject.fromObject(restJson.getJSONObject("data")).toString();
199 * Provide function for get VNF
204 * @param vnfInstanceId
207 * @throws ServiceException
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();
218 if(StringUtils.isEmpty(vnfInstanceId) || StringUtils.isEmpty(vnfmId)) {
219 resp.setStatus(Constant.HTTP_INNERERROR);
220 return restJson.toString();
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();
230 restJson.remove(EntityUtils.RESULT_CODE_KEY);
231 return restJson.toString();
235 * Provide function for get job
243 * @throws ServiceException
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);
254 return restJson.toString();