Update docker image to fix CSIT failure
[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.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStreamReader;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.servlet.http.HttpServletResponse;
25
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;
46 import org.slf4j.MDC;
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;
55
56 import com.google.gson.Gson;
57
58 @Controller
59 @RequestMapping(value = "/api/nokiavnfmdriver/v1")
60 public class VnfmDriverController {
61         private static final Logger logger = LoggerFactory.getLogger(VnfmDriverController.class);
62         
63         @Autowired
64         private VnfmDriverMgmrInf vnfmDriverMgmr;
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.read(new BufferedReader(new InputStreamReader(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")
201     public void notificationVnf(HttpServletRequest request, HttpServletResponse httpResponse) throws IOException
202     {
203                 
204                 logger.info("VnfmDriverController -> notificationVnf request:  request method is " + request.getMethod() + ",");
205                 
206                 try {
207                         if("GET".equalsIgnoreCase(request.getMethod())) // this is for link test
208                         {
209                                 httpResponse.setStatus(HttpStatus.SC_NO_CONTENT);
210                         }
211                         else if("POST".equalsIgnoreCase(request.getMethod()))
212                         {
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);
217                         }
218                 }
219                 catch(VnfmDriverException e)
220                 {
221                         processControllerException(httpResponse, e);
222                 }
223                 
224     }
225
226         private void processControllerException(HttpServletResponse httpResponse, VnfmDriverException e) {
227                 try {
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);
233                 }
234         }
235         
236 // -- The following VNFM Driver APIs are compliant to ETSI SOL003 -- Begin      
237         
238         @RequestMapping(value = "/createSubscription", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
239         @ResponseBody
240         public CreateSubscriptionResponse createSubscription(@RequestBody CreateSubscriptionRequest request, HttpServletResponse httpResponse)
241         {
242                 String jsonString = gson.toJson(request);
243                 logger.info("VnfmDriverController --> createSubscription, bodyMessage is " + jsonString);
244                 
245                 try {
246                         CreateSubscriptionResponse response = vnfmDriverMgmr.createSubscription(request);
247                         httpResponse.setStatus(HttpStatus.SC_CREATED);
248                         logger.info("VnfmDriverController --> createSubscription end ");
249                         return response;
250                 }
251                 catch(VnfmDriverException e)
252                 {
253                         processControllerException(httpResponse, e);
254                 }
255                 
256                 return null;
257         }
258         
259 // -- The following VNFM Driver APIs are compliant to ETSI SOL003 -- End
260 }
261