handle swagger part
[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.HttpServletResponse;
22
23 import org.apache.http.HttpStatus;
24 import org.apache.commons.io.IOUtils;
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.SwaggerInfo;
35 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfRequest;
36 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.bo.TerminateVnfResponse;
37 import org.onap.vfc.nfvo.driver.vnfm.svnfm.vnfmdriver.inf.VnfmDriverMgmrInf;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.http.MediaType;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.PathVariable;
44 import org.springframework.web.bind.annotation.RequestBody;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestMethod;
47 import org.springframework.web.bind.annotation.ResponseBody;
48
49 import com.google.gson.Gson;
50
51 @Controller
52 @RequestMapping(value = "/api/nokiavnfmdriver/v1")
53 public class VnfmDriverController {
54         private static final Logger logger = LoggerFactory.getLogger(VnfmDriverController.class);
55         
56         @Autowired
57         private VnfmDriverMgmrInf vnfmDriverMgmr;
58         
59         private Gson gson = new Gson();
60         
61         @RequestMapping(value = "/swagger.json", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
62         @ResponseBody
63         public SwaggerInfo apidoc() {
64         ClassLoader classLoader = getClass().getClassLoader();
65         SwaggerInfo info = null;
66                 try {
67                         String json = IOUtils.toString(classLoader.getResourceAsStream("swagger.json"));
68                         info = gson.fromJson(json, SwaggerInfo.class);
69                 } catch (IOException e) {
70                         logger.error("", e);
71                 }
72         
73         return info;
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                 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                 httpResponse.setStatus(HttpStatus.SC_CREATED);
86                 
87                 return response;
88     }
89         
90         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/terminate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
91     @ResponseBody
92     public TerminateVnfResponse terminateVnf(@RequestBody TerminateVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
93     {
94                 String jsonString = gson.toJson(request);
95                 logger.info("terminateVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
96                 
97                 try {
98                         TerminateVnfResponse response = vnfmDriverMgmr.terminateVnf(request, vnfmId, vnfInstanceId);
99                         httpResponse.setStatus(HttpStatus.SC_CREATED);
100                         return response;
101                 }
102                 catch(VnfmDriverException e)
103                 {
104                         processControllerException(httpResponse, e);
105                 }
106                 
107                 return null;
108     }
109         
110         
111         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
112     @ResponseBody
113     public QueryVnfResponse queryVnf(@PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
114     {
115                 logger.info("queryVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId);
116                 
117                 try {
118                         QueryVnfResponse response = vnfmDriverMgmr.queryVnf(vnfmId, vnfInstanceId);
119                         httpResponse.setStatus(HttpStatus.SC_CREATED);
120                         return response;
121                 }
122                 catch(VnfmDriverException e)
123                 {
124                         processControllerException(httpResponse, e);
125                 }
126                 
127                 return null;
128     }
129         @RequestMapping(value = "/{vnfmId}/jobs/{jobId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
130     @ResponseBody
131     public OperStatusVnfResponse getOperStatus(@PathVariable("vnfmId") String vnfmId,@PathVariable("jobId") String jobId, HttpServletResponse httpResponse)
132     {
133                 logger.info("getOperStatus request: vnfmId = " + vnfmId + ", jobId = " + jobId);
134                 
135                 try {
136                         OperStatusVnfResponse response = vnfmDriverMgmr.getOperStatus(vnfmId, jobId);
137                         httpResponse.setStatus(HttpStatus.SC_CREATED);
138                         return response;
139                 }
140                 catch(VnfmDriverException e)
141                 {
142                         processControllerException(httpResponse, e);
143                 }
144                 
145                 return null;
146     }
147         
148         
149         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/scale", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
150     @ResponseBody
151     public ScaleVnfResponse scaleVnf(@RequestBody ScaleVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
152     {
153                 String jsonString = gson.toJson(request);
154                 logger.info("scaleVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
155                 
156                 try {
157                         ScaleVnfResponse response = vnfmDriverMgmr.scaleVnf(request, vnfmId, vnfInstanceId);
158                         httpResponse.setStatus(HttpStatus.SC_CREATED);
159                         return response;
160                 }
161                 catch(VnfmDriverException e)
162                 {
163                         processControllerException(httpResponse, e);
164                 }
165                 
166                 return null;
167     }
168         
169         
170         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/heal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
171     @ResponseBody
172     public HealVnfResponse healVnf(@RequestBody HealVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
173     {
174                 String jsonString = gson.toJson(request);
175                 logger.info("healVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
176                 
177                 try {
178                         HealVnfResponse response = vnfmDriverMgmr.healVnf(request, vnfmId, vnfInstanceId);
179                         httpResponse.setStatus(HttpStatus.SC_CREATED);
180                         return response;
181                 }
182                 catch(VnfmDriverException e)
183                 {
184                         processControllerException(httpResponse, e);
185                 }
186                 
187                 return null;
188     }
189
190         private void processControllerException(HttpServletResponse httpResponse, VnfmDriverException e) {
191                 try {
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);
197                 }
198         }
199
200
201 }