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.IOException;
21 import javax.servlet.http.HttpServletResponse;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.http.HttpStatus;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.exception.VnfmDriverException;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfResponse;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfResponse;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.OperStatusVnfResponse;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.QueryVnfResponse;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfRequest;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfResponse;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.http.MediaType;
41 import org.springframework.stereotype.Controller;
42 import org.springframework.web.bind.annotation.PathVariable;
43 import org.springframework.web.bind.annotation.RequestBody;
44 import org.springframework.web.bind.annotation.RequestMapping;
45 import org.springframework.web.bind.annotation.RequestMethod;
46 import org.springframework.web.bind.annotation.ResponseBody;
48 import com.google.gson.Gson;
51 @RequestMapping(value = "/api/nokiavnfmdriver/v1")
52 public class VnfmDriverController {
53 private static final Logger logger = LoggerFactory.getLogger(VnfmDriverController.class);
56 private VnfmDriverMgmrInf vnfmDriverMgmr;
58 private Gson gson = new Gson();
60 @RequestMapping(value = "/swagger.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
62 public String apidoc() throws IOException {
63 ClassLoader classLoader = getClass().getClassLoader();
64 return IOUtils.toString(classLoader.getResourceAsStream("swagger.json"));
67 @RequestMapping(value = "/{vnfmId}/vnfs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
69 public InstantiateVnfResponse instantiateVnf(@RequestBody InstantiateVnfRequest request, @PathVariable("vnfmId") String vnfmId, HttpServletResponse httpResponse)
71 String jsonString = gson.toJson(request);
72 logger.info("instantiateVnf request: vnfmId = " + vnfmId + ", bodyMessage is " + jsonString);
74 InstantiateVnfResponse response = vnfmDriverMgmr.instantiateVnf(request, vnfmId);
76 logger.info("VnfmDriverController --> instantiateVnf response is " + gson.toJson(response));
78 httpResponse.setStatus(HttpStatus.SC_CREATED);
83 @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/terminate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
85 public TerminateVnfResponse terminateVnf(@RequestBody TerminateVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
87 String jsonString = gson.toJson(request);
88 logger.info("terminateVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
91 TerminateVnfResponse response = vnfmDriverMgmr.terminateVnf(request, vnfmId, vnfInstanceId);
92 httpResponse.setStatus(HttpStatus.SC_CREATED);
94 logger.info("VnfmDriverController --> terminateVnf response is " + gson.toJson(response));
97 catch(VnfmDriverException e)
99 processControllerException(httpResponse, e);
106 @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
108 public QueryVnfResponse queryVnf(@PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
110 logger.info("queryVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId);
113 QueryVnfResponse response = vnfmDriverMgmr.queryVnf(vnfmId, vnfInstanceId);
114 httpResponse.setStatus(HttpStatus.SC_CREATED);
115 logger.info("VnfmDriverController --> queryVnf response is " + gson.toJson(response));
118 catch(VnfmDriverException e)
120 processControllerException(httpResponse, e);
125 @RequestMapping(value = "/{vnfmId}/jobs/{jobId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
127 public OperStatusVnfResponse getOperStatus(@PathVariable("vnfmId") String vnfmId,@PathVariable("jobId") String jobId, HttpServletResponse httpResponse)
129 logger.info("getOperStatus request: vnfmId = " + vnfmId + ", jobId = " + jobId);
132 OperStatusVnfResponse response = vnfmDriverMgmr.getOperStatus(vnfmId, jobId);
133 httpResponse.setStatus(HttpStatus.SC_CREATED);
135 logger.info("VnfmDriverController --> getOperStatus response is " + gson.toJson(response));
138 catch(VnfmDriverException e)
140 processControllerException(httpResponse, e);
147 @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/scale", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
149 public ScaleVnfResponse scaleVnf(@RequestBody ScaleVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
151 String jsonString = gson.toJson(request);
152 logger.info("scaleVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
155 ScaleVnfResponse response = vnfmDriverMgmr.scaleVnf(request, vnfmId, vnfInstanceId);
156 httpResponse.setStatus(HttpStatus.SC_CREATED);
157 logger.info("VnfmDriverController --> scaleVnf response is " + gson.toJson(response));
160 catch(VnfmDriverException e)
162 processControllerException(httpResponse, e);
169 @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/heal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
171 public HealVnfResponse healVnf(@RequestBody HealVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
173 String jsonString = gson.toJson(request);
174 logger.info("healVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
177 HealVnfResponse response = vnfmDriverMgmr.healVnf(request, vnfmId, vnfInstanceId);
178 httpResponse.setStatus(HttpStatus.SC_CREATED);
179 logger.info("VnfmDriverController --> healVnf response is " + gson.toJson(response));
182 catch(VnfmDriverException e)
184 processControllerException(httpResponse, e);
190 private void processControllerException(HttpServletResponse httpResponse, VnfmDriverException e) {
192 logger.error(" VnfmDriverController --> processControllerException", e);
193 httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
194 httpResponse.sendError(e.getHttpStatus(), e.getMessage());
195 } catch (IOException e1) {
196 logger.error("VnfmDriverController --> processControllerException error to sendError ", e1);