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.HttpServletRequest;
 
  22 import javax.servlet.http.HttpServletResponse;
 
  24 import org.apache.commons.io.IOUtils;
 
  25 import org.apache.http.HttpStatus;
 
  26 import org.apache.http.client.ClientProtocolException;
 
  27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
 
  28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.exception.VnfmDriverException;
 
  29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
 
  30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfResponse;
 
  31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
 
  32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfResponse;
 
  33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.OperStatusVnfResponse;
 
  34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.QueryVnfResponse;
 
  35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfRequest;
 
  36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfResponse;
 
  37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
 
  38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
 
  39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
 
  40 import org.slf4j.Logger;
 
  41 import org.slf4j.LoggerFactory;
 
  43 import org.springframework.beans.factory.annotation.Autowired;
 
  44 import org.springframework.http.MediaType;
 
  45 import org.springframework.stereotype.Controller;
 
  46 import org.springframework.web.bind.annotation.PathVariable;
 
  47 import org.springframework.web.bind.annotation.RequestBody;
 
  48 import org.springframework.web.bind.annotation.RequestMapping;
 
  49 import org.springframework.web.bind.annotation.RequestMethod;
 
  50 import org.springframework.web.bind.annotation.ResponseBody;
 
  52 import com.google.gson.Gson;
 
  55 @RequestMapping(value = "/api/nokiavnfmdriver/v1")
 
  56 public class VnfmDriverController {
 
  57         private static final Logger logger = LoggerFactory.getLogger(VnfmDriverController.class);
 
  60         private VnfmDriverMgmrInf vnfmDriverMgmr;
 
  63         private CbamMgmrInf cbamMgmr;
 
  65         private Gson gson = new Gson();
 
  67         @RequestMapping(value = "/swagger.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
 
  69         public String apidoc() throws IOException {
 
  70 //              String client = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteAddr();
 
  71                 ClassLoader classLoader = getClass().getClassLoader();
 
  72         return IOUtils.toString(classLoader.getResourceAsStream("swagger.json"));
 
  75         @RequestMapping(value = "/{vnfmId}/vnfs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
 
  77     public InstantiateVnfResponse instantiateVnf(@RequestBody InstantiateVnfRequest request, @PathVariable("vnfmId") String vnfmId, HttpServletResponse httpResponse)
 
  79                 MDC.put("MDCtest", "MDCtest_001");
 
  80                 String jsonString = gson.toJson(request);
 
  81                 logger.info("instantiateVnf request: vnfmId = " + vnfmId + ", bodyMessage is " + jsonString);
 
  83                 InstantiateVnfResponse response = vnfmDriverMgmr.instantiateVnf(request, vnfmId);
 
  85                 logger.info("VnfmDriverController --> instantiateVnf response is " + gson.toJson(response));
 
  87                 httpResponse.setStatus(HttpStatus.SC_CREATED);
 
  92         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/terminate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
 
  94     public TerminateVnfResponse terminateVnf(@RequestBody TerminateVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
 
  96                 String jsonString = gson.toJson(request);
 
  97                 logger.info("terminateVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
 
 100                         TerminateVnfResponse response = vnfmDriverMgmr.terminateVnf(request, vnfmId, vnfInstanceId);
 
 101                         httpResponse.setStatus(HttpStatus.SC_CREATED);
 
 103                         logger.info("VnfmDriverController --> terminateVnf response is " + gson.toJson(response));
 
 106                 catch(VnfmDriverException e)
 
 108                         processControllerException(httpResponse, e);
 
 115         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
 
 117     public QueryVnfResponse queryVnf(@PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
 
 119                 logger.info("queryVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId);
 
 122                         QueryVnfResponse response = vnfmDriverMgmr.queryVnf(vnfmId, vnfInstanceId);
 
 123                         httpResponse.setStatus(HttpStatus.SC_CREATED);
 
 124                         logger.info("VnfmDriverController --> queryVnf response is " + gson.toJson(response));
 
 127                 catch(VnfmDriverException e)
 
 129                         processControllerException(httpResponse, e);
 
 134         @RequestMapping(value = "/{vnfmId}/jobs/{jobId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
 
 136     public OperStatusVnfResponse getOperStatus(@PathVariable("vnfmId") String vnfmId,@PathVariable("jobId") String jobId, HttpServletResponse httpResponse)
 
 138                 logger.info("getOperStatus request: vnfmId = " + vnfmId + ", jobId = " + jobId);
 
 141                         OperStatusVnfResponse response = vnfmDriverMgmr.getOperStatus(vnfmId, jobId);
 
 142                         httpResponse.setStatus(HttpStatus.SC_CREATED);
 
 144                         logger.info("VnfmDriverController --> getOperStatus response is " + gson.toJson(response));
 
 147                 catch(VnfmDriverException e)
 
 149                         processControllerException(httpResponse, e);
 
 156         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/scale", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
 
 158     public ScaleVnfResponse scaleVnf(@RequestBody ScaleVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
 
 160                 String jsonString = gson.toJson(request);
 
 161                 logger.info("scaleVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
 
 164                         ScaleVnfResponse response = vnfmDriverMgmr.scaleVnf(request, vnfmId, vnfInstanceId);
 
 165                         httpResponse.setStatus(HttpStatus.SC_CREATED);
 
 166                         logger.info("VnfmDriverController --> scaleVnf response is " + gson.toJson(response));
 
 169                 catch(VnfmDriverException e)
 
 171                         processControllerException(httpResponse, e);
 
 178         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/heal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
 
 180     public HealVnfResponse healVnf(@RequestBody HealVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
 
 182                 String jsonString = gson.toJson(request);
 
 183                 logger.info("healVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
 
 186                         HealVnfResponse response = vnfmDriverMgmr.healVnf(request, vnfmId, vnfInstanceId);
 
 187                         httpResponse.setStatus(HttpStatus.SC_CREATED);
 
 188                         logger.info("VnfmDriverController --> healVnf response is " + gson.toJson(response));
 
 191                 catch(VnfmDriverException e)
 
 193                         processControllerException(httpResponse, e);
 
 199 //      @RequestMapping(value = "/notifications", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
 
 200 //      public CBAMVnfNotificationResponse notificationVnf(@RequestBody CBAMVnfNotificationRequest request, HttpServletResponse httpResponse) throws ClientProtocolException, Exception
 
 201         @RequestMapping(value = "/notifications")
 
 203     public void notificationVnf(HttpServletRequest request, HttpServletResponse httpResponse) throws ClientProtocolException, Exception
 
 206 //              String jsonString = gson.toJson(request);
 
 207 //              logger.info("notificationVnf request:  bodyMessage is " + jsonString);
 
 208                 logger.info("notificationVnf request:  bodyMessage is " + request.getMethod() + ",");
 
 211 //                      CBAMVnfNotificationResponse response = cbamMgmr.getNotification(request);
 
 212                         httpResponse.setStatus(204);
 
 213 //                      logger.info("cbamController --> notificationVnf response is " + gson.toJson(response));
 
 216                 catch(VnfmDriverException e)
 
 218                         processControllerException(httpResponse, e);
 
 224         private void processControllerException(HttpServletResponse httpResponse, VnfmDriverException e) {
 
 226                         logger.error(" VnfmDriverController --> processControllerException", e);
 
 227                         httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
 
 228                         httpResponse.sendError(e.getHttpStatus(), e.getMessage());
 
 229                 } catch (IOException e1) {
 
 230                         logger.error("VnfmDriverController --> processControllerException error to sendError ", e1);