8dd6832322da97a1922324fcabdcd47209dc5b54
[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.commons.io.IOUtils;
24 import org.apache.http.HttpStatus;
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         @ResponseBody
62         public String apidoc() throws IOException {
63         ClassLoader classLoader = getClass().getClassLoader();
64         return IOUtils.toString(classLoader.getResourceAsStream("swagger.json"));
65     }
66         
67         @RequestMapping(value = "/{vnfmId}/vnfs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
68     @ResponseBody
69     public InstantiateVnfResponse instantiateVnf(@RequestBody InstantiateVnfRequest request, @PathVariable("vnfmId") String vnfmId, HttpServletResponse httpResponse)
70     {
71                 String jsonString = gson.toJson(request);
72                 logger.info("instantiateVnf request: vnfmId = " + vnfmId + ", bodyMessage is " + jsonString);
73                 
74                 InstantiateVnfResponse response = vnfmDriverMgmr.instantiateVnf(request, vnfmId);
75                 
76                 logger.info("InstantiateVnfResponse is " + gson.toJson(response));
77                 
78                 httpResponse.setStatus(HttpStatus.SC_CREATED);
79                 
80                 return response;
81     }
82         
83         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/terminate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
84     @ResponseBody
85     public TerminateVnfResponse terminateVnf(@RequestBody TerminateVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
86     {
87                 String jsonString = gson.toJson(request);
88                 logger.info("terminateVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
89                 
90                 try {
91                         TerminateVnfResponse response = vnfmDriverMgmr.terminateVnf(request, vnfmId, vnfInstanceId);
92                         httpResponse.setStatus(HttpStatus.SC_CREATED);
93                         return response;
94                 }
95                 catch(VnfmDriverException e)
96                 {
97                         processControllerException(httpResponse, e);
98                 }
99                 
100                 return null;
101     }
102         
103         
104         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
105     @ResponseBody
106     public QueryVnfResponse queryVnf(@PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
107     {
108                 logger.info("queryVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId);
109                 
110                 try {
111                         QueryVnfResponse response = vnfmDriverMgmr.queryVnf(vnfmId, vnfInstanceId);
112                         httpResponse.setStatus(HttpStatus.SC_CREATED);
113                         return response;
114                 }
115                 catch(VnfmDriverException e)
116                 {
117                         processControllerException(httpResponse, e);
118                 }
119                 
120                 return null;
121     }
122         @RequestMapping(value = "/{vnfmId}/jobs/{jobId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
123     @ResponseBody
124     public OperStatusVnfResponse getOperStatus(@PathVariable("vnfmId") String vnfmId,@PathVariable("jobId") String jobId, HttpServletResponse httpResponse)
125     {
126                 logger.info("getOperStatus request: vnfmId = " + vnfmId + ", jobId = " + jobId);
127                 
128                 try {
129                         OperStatusVnfResponse response = vnfmDriverMgmr.getOperStatus(vnfmId, jobId);
130                         httpResponse.setStatus(HttpStatus.SC_CREATED);
131                         return response;
132                 }
133                 catch(VnfmDriverException e)
134                 {
135                         processControllerException(httpResponse, e);
136                 }
137                 
138                 return null;
139     }
140         
141         
142         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/scale", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
143     @ResponseBody
144     public ScaleVnfResponse scaleVnf(@RequestBody ScaleVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
145     {
146                 String jsonString = gson.toJson(request);
147                 logger.info("scaleVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
148                 
149                 try {
150                         ScaleVnfResponse response = vnfmDriverMgmr.scaleVnf(request, vnfmId, vnfInstanceId);
151                         httpResponse.setStatus(HttpStatus.SC_CREATED);
152                         return response;
153                 }
154                 catch(VnfmDriverException e)
155                 {
156                         processControllerException(httpResponse, e);
157                 }
158                 
159                 return null;
160     }
161         
162         
163         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/heal", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
164     @ResponseBody
165     public HealVnfResponse healVnf(@RequestBody HealVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
166     {
167                 String jsonString = gson.toJson(request);
168                 logger.info("healVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
169                 
170                 try {
171                         HealVnfResponse response = vnfmDriverMgmr.healVnf(request, vnfmId, vnfInstanceId);
172                         httpResponse.setStatus(HttpStatus.SC_CREATED);
173                         return response;
174                 }
175                 catch(VnfmDriverException e)
176                 {
177                         processControllerException(httpResponse, e);
178                 }
179                 
180                 return null;
181     }
182
183         private void processControllerException(HttpServletResponse httpResponse, VnfmDriverException e) {
184                 try {
185                         logger.error(" VnfmDriverController --> processControllerException", e);
186                         httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
187                         httpResponse.sendError(e.getHttpStatus(), e.getMessage());
188                 } catch (IOException e1) {
189                         logger.error("VnfmDriverController --> processControllerException error to sendError ", e1);
190                 }
191         }
192
193
194 }