2 * Copyright 2016-2017, Nokia Corporation
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.driver.vnfm.svnfm.vnfmdriver.controller;
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
26 //import org.apache.commons.io.IOUtils;
27 import com.alibaba.dubbo.common.utils.IOUtils;
28 import org.apache.http.HttpStatus;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.exception.VnfmDriverException;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.CreateSubscriptionRequest;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.CreateSubscriptionResponse;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfResponse;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfResponse;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.OperStatusVnfResponse;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.QueryVnfResponse;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfRequest;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfResponse;
41 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
42 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
43 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.http.MediaType;
49 import org.springframework.stereotype.Controller;
50 import org.springframework.web.bind.annotation.PathVariable;
51 import org.springframework.web.bind.annotation.RequestBody;
52 import org.springframework.web.bind.annotation.RequestMapping;
53 import org.springframework.web.bind.annotation.RequestMethod;
54 import org.springframework.web.bind.annotation.ResponseBody;
56 import com.google.gson.Gson;
59 @RequestMapping(value = "/api/nokiavnfmdriver/v1")
60 public class VnfmDriverController {
61 private static final Logger logger = LoggerFactory.getLogger(VnfmDriverController.class);
64 private VnfmDriverMgmrInf vnfmDriverMgmr;
66 private Gson gson = new Gson();
68 @RequestMapping(value = "/swagger.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
70 public String apidoc() throws IOException {
71 // String client = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteAddr();
72 ClassLoader classLoader = getClass().getClassLoader();
73 return IOUtils.read(new BufferedReader(new InputStreamReader(classLoader.getResourceAsStream("swagger.json"))));
76 @RequestMapping(value = "/{vnfmId}/vnfs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
78 public InstantiateVnfResponse instantiateVnf(@RequestBody InstantiateVnfRequest request, @PathVariable("vnfmId") String vnfmId, HttpServletResponse httpResponse)
80 MDC.put("MDCtest", "MDCtest_001");
81 String jsonString = gson.toJson(request);
82 logger.info("instantiateVnf request: vnfmId = " + vnfmId + ", bodyMessage is " + jsonString);
84 InstantiateVnfResponse response = vnfmDriverMgmr.instantiateVnf(request, vnfmId);
86 logger.info("VnfmDriverController --> instantiateVnf response is " + gson.toJson(response));
88 httpResponse.setStatus(HttpStatus.SC_CREATED);
93 @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/terminate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
95 public TerminateVnfResponse terminateVnf(@RequestBody TerminateVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
97 String jsonString = gson.toJson(request);
98 logger.info("terminateVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
101 TerminateVnfResponse response = vnfmDriverMgmr.terminateVnf(request, vnfmId, vnfInstanceId);
102 httpResponse.setStatus(HttpStatus.SC_CREATED);
104 logger.info("VnfmDriverController --> terminateVnf response is " + gson.toJson(response));
107 catch(VnfmDriverException e)
109 processControllerException(httpResponse, e);
116 @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
118 public QueryVnfResponse queryVnf(@PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
120 logger.info("queryVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId);
123 QueryVnfResponse response = vnfmDriverMgmr.queryVnf(vnfmId, vnfInstanceId);
124 httpResponse.setStatus(HttpStatus.SC_CREATED);
125 logger.info("VnfmDriverController --> queryVnf response is " + gson.toJson(response));
128 catch(VnfmDriverException e)
130 processControllerException(httpResponse, e);
135 @RequestMapping(value = "/{vnfmId}/jobs/{jobId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
137 public OperStatusVnfResponse getOperStatus(@PathVariable("vnfmId") String vnfmId,@PathVariable("jobId") String jobId, HttpServletResponse httpResponse)
139 logger.info("getOperStatus request: vnfmId = " + vnfmId + ", jobId = " + jobId);
142 OperStatusVnfResponse response = vnfmDriverMgmr.getOperStatus(vnfmId, jobId);
143 httpResponse.setStatus(HttpStatus.SC_CREATED);
145 logger.info("VnfmDriverController --> getOperStatus response is " + gson.toJson(response));
148 catch(VnfmDriverException e)
150 processControllerException(httpResponse, e);
157 @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/scale", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
159 public ScaleVnfResponse scaleVnf(@RequestBody ScaleVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
161 String jsonString = gson.toJson(request);
162 logger.info("scaleVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
165 ScaleVnfResponse response = vnfmDriverMgmr.scaleVnf(request, vnfmId, vnfInstanceId);
166 httpResponse.setStatus(HttpStatus.SC_CREATED);
167 logger.info("VnfmDriverController --> scaleVnf response is " + gson.toJson(response));
170 catch(VnfmDriverException e)
172 processControllerException(httpResponse, e);
179 @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/heal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
181 public HealVnfResponse healVnf(@RequestBody HealVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
183 String jsonString = gson.toJson(request);
184 logger.info("healVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
187 HealVnfResponse response = vnfmDriverMgmr.healVnf(request, vnfmId, vnfInstanceId);
188 httpResponse.setStatus(HttpStatus.SC_CREATED);
189 logger.info("VnfmDriverController --> healVnf response is " + gson.toJson(response));
192 catch(VnfmDriverException e)
194 processControllerException(httpResponse, e);
200 @RequestMapping(value = "/notifications")
201 public void notificationVnf(HttpServletRequest request, HttpServletResponse httpResponse) throws IOException
204 logger.info("VnfmDriverController -> notificationVnf request: request method is " + request.getMethod() + ",");
207 if("GET".equalsIgnoreCase(request.getMethod())) // this is for link test
209 httpResponse.setStatus(HttpStatus.SC_NO_CONTENT);
211 else if("POST".equalsIgnoreCase(request.getMethod()))
213 httpResponse.setStatus(HttpStatus.SC_NO_CONTENT);
214 BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));
215 String bodyString = IOUtils.read(reader);
216 logger.info("VnfmDriverController -> notificationVnf request: bodyMessage is " + bodyString);
219 catch(VnfmDriverException e)
221 processControllerException(httpResponse, e);
226 private void processControllerException(HttpServletResponse httpResponse, VnfmDriverException e) {
228 logger.error(" VnfmDriverController --> processControllerException", e);
229 httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
230 httpResponse.sendError(e.getHttpStatus(), e.getMessage());
231 } catch (IOException e1) {
232 logger.error("VnfmDriverController --> processControllerException error to sendError ", e1);
236 // -- The following VNFM Driver APIs are compliant to ETSI SOL003 -- Begin
238 @RequestMapping(value = "/createSubscription", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
240 public CreateSubscriptionResponse createSubscription(@RequestBody CreateSubscriptionRequest request, HttpServletResponse httpResponse)
242 String jsonString = gson.toJson(request);
243 logger.info("VnfmDriverController --> createSubscription, bodyMessage is " + jsonString);
246 CreateSubscriptionResponse response = vnfmDriverMgmr.createSubscription(request);
247 httpResponse.setStatus(HttpStatus.SC_CREATED);
248 logger.info("VnfmDriverController --> createSubscription end ");
251 catch(VnfmDriverException e)
253 processControllerException(httpResponse, e);
259 // -- The following VNFM Driver APIs are compliant to ETSI SOL003 -- End