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