Improve unit-test coverage
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / vnfmdriver / controller / VnfmDriverController.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
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.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.controller;
18
19 import java.io.IOException;
20
21 import javax.servlet.http.HttpServletRequest;
22 import javax.servlet.http.HttpServletResponse;
23
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;
42 import org.slf4j.MDC;
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;
51
52 import com.google.gson.Gson;
53
54 @Controller
55 @RequestMapping(value = "/api/nokiavnfmdriver/v1")
56 public class VnfmDriverController {
57         private static final Logger logger = LoggerFactory.getLogger(VnfmDriverController.class);
58         
59         @Autowired
60         private VnfmDriverMgmrInf vnfmDriverMgmr;
61         
62         @Autowired
63         private CbamMgmrInf cbamMgmr;
64         
65         private Gson gson = new Gson();
66         
67         @RequestMapping(value = "/swagger.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
68         @ResponseBody
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"));
73     }
74         
75         @RequestMapping(value = "/{vnfmId}/vnfs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
76         @ResponseBody
77     public InstantiateVnfResponse instantiateVnf(@RequestBody InstantiateVnfRequest request, @PathVariable("vnfmId") String vnfmId, HttpServletResponse httpResponse)
78     {
79                 MDC.put("MDCtest", "MDCtest_001");
80                 String jsonString = gson.toJson(request);
81                 logger.info("instantiateVnf request: vnfmId = " + vnfmId + ", bodyMessage is " + jsonString);
82                 
83                 InstantiateVnfResponse response = vnfmDriverMgmr.instantiateVnf(request, vnfmId);
84                 
85                 logger.info("VnfmDriverController --> instantiateVnf response is " + gson.toJson(response));
86                 
87                 httpResponse.setStatus(HttpStatus.SC_CREATED);
88                 
89                 return response;
90     }
91         
92         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/terminate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
93     @ResponseBody
94     public TerminateVnfResponse terminateVnf(@RequestBody TerminateVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
95     {
96                 String jsonString = gson.toJson(request);
97                 logger.info("terminateVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
98                 
99                 try {
100                         TerminateVnfResponse response = vnfmDriverMgmr.terminateVnf(request, vnfmId, vnfInstanceId);
101                         httpResponse.setStatus(HttpStatus.SC_CREATED);
102                         
103                         logger.info("VnfmDriverController --> terminateVnf response is " + gson.toJson(response));
104                         return response;
105                 }
106                 catch(VnfmDriverException e)
107                 {
108                         processControllerException(httpResponse, e);
109                 }
110                 
111                 return null;
112     }
113         
114         
115         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
116     @ResponseBody
117     public QueryVnfResponse queryVnf(@PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
118     {
119                 logger.info("queryVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId);
120                 
121                 try {
122                         QueryVnfResponse response = vnfmDriverMgmr.queryVnf(vnfmId, vnfInstanceId);
123                         httpResponse.setStatus(HttpStatus.SC_CREATED);
124                         logger.info("VnfmDriverController --> queryVnf response is " + gson.toJson(response));
125                         return response;
126                 }
127                 catch(VnfmDriverException e)
128                 {
129                         processControllerException(httpResponse, e);
130                 }
131                 
132                 return null;
133     }
134         @RequestMapping(value = "/{vnfmId}/jobs/{jobId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
135     @ResponseBody
136     public OperStatusVnfResponse getOperStatus(@PathVariable("vnfmId") String vnfmId,@PathVariable("jobId") String jobId, HttpServletResponse httpResponse)
137     {
138                 logger.info("getOperStatus request: vnfmId = " + vnfmId + ", jobId = " + jobId);
139                 
140                 try {
141                         OperStatusVnfResponse response = vnfmDriverMgmr.getOperStatus(vnfmId, jobId);
142                         httpResponse.setStatus(HttpStatus.SC_CREATED);
143                         
144                         logger.info("VnfmDriverController --> getOperStatus response is " + gson.toJson(response));
145                         return response;
146                 }
147                 catch(VnfmDriverException e)
148                 {
149                         processControllerException(httpResponse, e);
150                 }
151                 
152                 return null;
153     }
154         
155         
156         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/scale", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
157     @ResponseBody
158     public ScaleVnfResponse scaleVnf(@RequestBody ScaleVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
159     {
160                 String jsonString = gson.toJson(request);
161                 logger.info("scaleVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
162                 
163                 try {
164                         ScaleVnfResponse response = vnfmDriverMgmr.scaleVnf(request, vnfmId, vnfInstanceId);
165                         httpResponse.setStatus(HttpStatus.SC_CREATED);
166                         logger.info("VnfmDriverController --> scaleVnf response is " + gson.toJson(response));
167                         return response;
168                 }
169                 catch(VnfmDriverException e)
170                 {
171                         processControllerException(httpResponse, e);
172                 }
173                 
174                 return null;
175     }
176         
177         
178         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/heal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
179     @ResponseBody
180     public HealVnfResponse healVnf(@RequestBody HealVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
181     {
182                 String jsonString = gson.toJson(request);
183                 logger.info("healVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
184                 
185                 try {
186                         HealVnfResponse response = vnfmDriverMgmr.healVnf(request, vnfmId, vnfInstanceId);
187                         httpResponse.setStatus(HttpStatus.SC_CREATED);
188                         logger.info("VnfmDriverController --> healVnf response is " + gson.toJson(response));
189                         return response;
190                 }
191                 catch(VnfmDriverException e)
192                 {
193                         processControllerException(httpResponse, e);
194                 }
195                 
196                 return null;
197     }
198         
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")
202 //    @ResponseBody
203     public void notificationVnf(HttpServletRequest request, HttpServletResponse httpResponse) throws ClientProtocolException, Exception
204     {
205                 
206 //              String jsonString = gson.toJson(request);
207 //              logger.info("notificationVnf request:  bodyMessage is " + jsonString);
208                 logger.info("notificationVnf request:  bodyMessage is " + request.getMethod() + ",");
209                 
210                 try {
211 //                      CBAMVnfNotificationResponse response = cbamMgmr.getNotification(request);
212                         httpResponse.setStatus(204);
213 //                      logger.info("cbamController --> notificationVnf response is " + gson.toJson(response));
214 //                      return response;
215                 }
216                 catch(VnfmDriverException e)
217                 {
218                         processControllerException(httpResponse, e);
219                 }
220                 
221 //              return null;
222     }
223
224         private void processControllerException(HttpServletResponse httpResponse, VnfmDriverException e) {
225                 try {
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);
231                 }
232         }
233 }