Add unit test cases
[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.onap.vfc.nfvo.driver.vnfm.svnfm.cbam.inf.CbamMgmrInf;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.exception.VnfmDriverException;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.CreateSubscriptionRequest;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.CreateSubscriptionResponse;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfRequest;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.HealVnfResponse;
32 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfRequest;
33 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.InstantiateVnfResponse;
34 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.OperStatusVnfResponse;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.QueryVnfResponse;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfRequest;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.ScaleVnfResponse;
38 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
39 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
40 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.slf4j.MDC;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.http.MediaType;
46 import org.springframework.stereotype.Controller;
47 import org.springframework.web.bind.annotation.PathVariable;
48 import org.springframework.web.bind.annotation.RequestBody;
49 import org.springframework.web.bind.annotation.RequestMapping;
50 import org.springframework.web.bind.annotation.RequestMethod;
51 import org.springframework.web.bind.annotation.ResponseBody;
52
53 import com.google.gson.Gson;
54
55 @Controller
56 @RequestMapping(value = "/api/nokiavnfmdriver/v1")
57 public class VnfmDriverController {
58         private static final Logger logger = LoggerFactory.getLogger(VnfmDriverController.class);
59         
60         @Autowired
61         private VnfmDriverMgmrInf vnfmDriverMgmr;
62         
63         @Autowired
64         private CbamMgmrInf cbamMgmr;
65         
66         private Gson gson = new Gson();
67         
68         @RequestMapping(value = "/swagger.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
69         @ResponseBody
70         public String apidoc() throws IOException {
71 //              String client = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getRemoteAddr();
72                 ClassLoader classLoader = getClass().getClassLoader();
73         return IOUtils.toString(classLoader.getResourceAsStream("swagger.json"));
74     }
75         
76         @RequestMapping(value = "/{vnfmId}/vnfs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
77         @ResponseBody
78     public InstantiateVnfResponse instantiateVnf(@RequestBody InstantiateVnfRequest request, @PathVariable("vnfmId") String vnfmId, HttpServletResponse httpResponse)
79     {
80                 MDC.put("MDCtest", "MDCtest_001");
81                 String jsonString = gson.toJson(request);
82                 logger.info("instantiateVnf request: vnfmId = " + vnfmId + ", bodyMessage is " + jsonString);
83                 
84                 InstantiateVnfResponse response = vnfmDriverMgmr.instantiateVnf(request, vnfmId);
85                 
86                 logger.info("VnfmDriverController --> instantiateVnf response is " + gson.toJson(response));
87                 
88                 httpResponse.setStatus(HttpStatus.SC_CREATED);
89                 
90                 return response;
91     }
92         
93         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/terminate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
94     @ResponseBody
95     public TerminateVnfResponse terminateVnf(@RequestBody TerminateVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
96     {
97                 String jsonString = gson.toJson(request);
98                 logger.info("terminateVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
99                 
100                 try {
101                         TerminateVnfResponse response = vnfmDriverMgmr.terminateVnf(request, vnfmId, vnfInstanceId);
102                         httpResponse.setStatus(HttpStatus.SC_CREATED);
103                         
104                         logger.info("VnfmDriverController --> terminateVnf response is " + gson.toJson(response));
105                         return response;
106                 }
107                 catch(VnfmDriverException e)
108                 {
109                         processControllerException(httpResponse, e);
110                 }
111                 
112                 return null;
113     }
114         
115         
116         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
117     @ResponseBody
118     public QueryVnfResponse queryVnf(@PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
119     {
120                 logger.info("queryVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId);
121                 
122                 try {
123                         QueryVnfResponse response = vnfmDriverMgmr.queryVnf(vnfmId, vnfInstanceId);
124                         httpResponse.setStatus(HttpStatus.SC_CREATED);
125                         logger.info("VnfmDriverController --> queryVnf response is " + gson.toJson(response));
126                         return response;
127                 }
128                 catch(VnfmDriverException e)
129                 {
130                         processControllerException(httpResponse, e);
131                 }
132                 
133                 return null;
134     }
135         @RequestMapping(value = "/{vnfmId}/jobs/{jobId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
136     @ResponseBody
137     public OperStatusVnfResponse getOperStatus(@PathVariable("vnfmId") String vnfmId,@PathVariable("jobId") String jobId, HttpServletResponse httpResponse)
138     {
139                 logger.info("getOperStatus request: vnfmId = " + vnfmId + ", jobId = " + jobId);
140                 
141                 try {
142                         OperStatusVnfResponse response = vnfmDriverMgmr.getOperStatus(vnfmId, jobId);
143                         httpResponse.setStatus(HttpStatus.SC_CREATED);
144                         
145                         logger.info("VnfmDriverController --> getOperStatus response is " + gson.toJson(response));
146                         return response;
147                 }
148                 catch(VnfmDriverException e)
149                 {
150                         processControllerException(httpResponse, e);
151                 }
152                 
153                 return null;
154     }
155         
156         
157         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/scale", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
158     @ResponseBody
159     public ScaleVnfResponse scaleVnf(@RequestBody ScaleVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
160     {
161                 String jsonString = gson.toJson(request);
162                 logger.info("scaleVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
163                 
164                 try {
165                         ScaleVnfResponse response = vnfmDriverMgmr.scaleVnf(request, vnfmId, vnfInstanceId);
166                         httpResponse.setStatus(HttpStatus.SC_CREATED);
167                         logger.info("VnfmDriverController --> scaleVnf response is " + gson.toJson(response));
168                         return response;
169                 }
170                 catch(VnfmDriverException e)
171                 {
172                         processControllerException(httpResponse, e);
173                 }
174                 
175                 return null;
176     }
177         
178         
179         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/heal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
180     @ResponseBody
181     public HealVnfResponse healVnf(@RequestBody HealVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
182     {
183                 String jsonString = gson.toJson(request);
184                 logger.info("healVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
185                 
186                 try {
187                         HealVnfResponse response = vnfmDriverMgmr.healVnf(request, vnfmId, vnfInstanceId);
188                         httpResponse.setStatus(HttpStatus.SC_CREATED);
189                         logger.info("VnfmDriverController --> healVnf response is " + gson.toJson(response));
190                         return response;
191                 }
192                 catch(VnfmDriverException e)
193                 {
194                         processControllerException(httpResponse, e);
195                 }
196                 
197                 return null;
198     }
199         
200 //      @RequestMapping(value = "/notifications", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
201 //      public CBAMVnfNotificationResponse notificationVnf(@RequestBody CBAMVnfNotificationRequest request, HttpServletResponse httpResponse) throws ClientProtocolException, Exception
202         @RequestMapping(value = "/notifications")
203 //    @ResponseBody
204     public void notificationVnf(HttpServletRequest request, HttpServletResponse httpResponse)
205     {
206                 
207 //              String jsonString = gson.toJson(request);
208 //              logger.info("notificationVnf request:  bodyMessage is " + jsonString);
209                 logger.info("notificationVnf request:  bodyMessage is " + request.getMethod() + ",");
210                 
211                 try {
212 //                      CBAMVnfNotificationResponse response = cbamMgmr.getNotification(request);
213                         httpResponse.setStatus(204);
214 //                      logger.info("cbamController --> notificationVnf response is " + gson.toJson(response));
215 //                      return response;
216                 }
217                 catch(VnfmDriverException e)
218                 {
219                         processControllerException(httpResponse, e);
220                 }
221                 
222 //              return null;
223     }
224
225         private void processControllerException(HttpServletResponse httpResponse, VnfmDriverException e) {
226                 try {
227                         logger.error(" VnfmDriverController --> processControllerException", e);
228                         httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
229                         httpResponse.sendError(e.getHttpStatus(), e.getMessage());
230                 } catch (IOException e1) {
231                         logger.error("VnfmDriverController --> processControllerException error to sendError ", e1);
232                 }
233         }
234         
235 // -- The following VNFM Driver APIs are compliant to ETSI SOL003 -- Begin      
236         
237         @RequestMapping(value = "/createSubscription", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
238         @ResponseBody
239         public CreateSubscriptionResponse createSubscription(@RequestBody CreateSubscriptionRequest request, HttpServletResponse httpResponse)
240         {
241                 String jsonString = gson.toJson(request);
242                 logger.info("VnfmDriverController --> createSubscription, bodyMessage is " + jsonString);
243                 
244                 try {
245                         CreateSubscriptionResponse response = vnfmDriverMgmr.createSubscription(request);
246                         httpResponse.setStatus(HttpStatus.SC_CREATED);
247                         logger.info("VnfmDriverController --> createSubscription end ");
248                         return response;
249                 }
250                 catch(VnfmDriverException e)
251                 {
252                         processControllerException(httpResponse, e);
253                 }
254                 
255                 return null;
256         }
257         
258 // -- The following VNFM Driver APIs are compliant to ETSI SOL003 -- End
259 }
260