ba2641b5faab532fde7dc8977486b7ed07e6d64c
[vfc/nfvo/driver/vnfm/svnfm.git] / nokia / vnfmdriver / vfcadaptorservice / vfcadaptor / src / main / java / com / nokia / vfcadaptor / 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 com.nokia.vfcadaptor.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.log4j.Logger;
25 import org.springframework.beans.factory.annotation.Autowired;
26 import org.springframework.http.MediaType;
27 import org.springframework.stereotype.Controller;
28 import org.springframework.web.bind.annotation.PathVariable;
29 import org.springframework.web.bind.annotation.RequestBody;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RequestMethod;
32 import org.springframework.web.bind.annotation.ResponseBody;
33
34 import com.google.gson.Gson;
35 import com.nokia.vfcadaptor.exception.VnfmDriverException;
36 import com.nokia.vfcadaptor.vnfmdriver.bo.HealVnfRequest;
37 import com.nokia.vfcadaptor.vnfmdriver.bo.HealVnfResponse;
38 import com.nokia.vfcadaptor.vnfmdriver.bo.InstantiateVnfRequest;
39 import com.nokia.vfcadaptor.vnfmdriver.bo.InstantiateVnfResponse;
40 import com.nokia.vfcadaptor.vnfmdriver.bo.OperStatusVnfResponse;
41 import com.nokia.vfcadaptor.vnfmdriver.bo.QueryVnfResponse;
42 import com.nokia.vfcadaptor.vnfmdriver.bo.ScaleVnfRequest;
43 import com.nokia.vfcadaptor.vnfmdriver.bo.ScaleVnfResponse;
44 import com.nokia.vfcadaptor.vnfmdriver.bo.TerminateVnfRequest;
45 import com.nokia.vfcadaptor.vnfmdriver.bo.TerminateVnfResponse;
46 import com.nokia.vfcadaptor.vnfmdriver.inf.VnfmDriverMgmrInf;
47
48 @Controller
49 @RequestMapping(value = "/nokiavnfm/v1")
50 public class VnfmDriverController {
51         private Logger logger = Logger.getLogger(VnfmDriverController.class);
52         
53         @Autowired
54         private VnfmDriverMgmrInf vnfmDriverMgmr;
55         
56         private Gson gson = new Gson();
57         
58         @RequestMapping(value = "/{vnfmId}/vnfs", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
59     @ResponseBody
60     public InstantiateVnfResponse instantiateVnf(@RequestBody InstantiateVnfRequest request, @PathVariable("vnfmId") String vnfmId, HttpServletResponse httpResponse)
61     {
62                 String jsonString = gson.toJson(request);
63                 logger.info("instantiateVnf request: vnfmId = " + vnfmId + ", bodyMessage is " + jsonString);
64                 
65                 InstantiateVnfResponse response = vnfmDriverMgmr.instantiateVnf(request, vnfmId);
66                 
67                 httpResponse.setStatus(HttpStatus.SC_CREATED);
68                 
69                 return response;
70     }
71         
72         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/terminate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
73     @ResponseBody
74     public TerminateVnfResponse terminateVnf(@RequestBody TerminateVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
75     {
76                 String jsonString = gson.toJson(request);
77                 logger.info("terminateVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
78                 
79                 try {
80                         TerminateVnfResponse response = vnfmDriverMgmr.terminateVnf(request, vnfmId, vnfInstanceId);
81                         httpResponse.setStatus(HttpStatus.SC_CREATED);
82                         return response;
83                 }
84                 catch(VnfmDriverException e)
85                 {
86                         try {
87                                 httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
88                                 httpResponse.sendError(e.getHttpStatus(), e.getMessage());
89                         } catch (IOException e1) {
90                                 
91                         }
92                 }
93                 
94                 return null;
95     }
96         
97         
98         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
99     @ResponseBody
100     public QueryVnfResponse queryVnf(@PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
101     {
102                 logger.info("queryVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId);
103                 
104                 try {
105                         QueryVnfResponse response = vnfmDriverMgmr.queryVnf(vnfmId, vnfInstanceId);
106                         httpResponse.setStatus(HttpStatus.SC_CREATED);
107                         return response;
108                 }
109                 catch(VnfmDriverException e)
110                 {
111                         try {
112                                 httpResponse.setStatus(HttpStatus.SC_BAD_REQUEST);
113                                 httpResponse.sendError(e.getHttpStatus(), e.getMessage());
114                         } catch (IOException e1) {
115                                 
116                         }
117                 }
118                 
119                 return null;
120     }
121         @RequestMapping(value = "/{vnfmId}/jobs/{jobId}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
122     @ResponseBody
123     public OperStatusVnfResponse getOperStatus(@PathVariable("vnfmId") String vnfmId,@PathVariable("jobId") String jobId, HttpServletResponse httpResponse)
124     {
125                 logger.info("getOperStatus request: vnfmId = " + vnfmId + ", jobId = " + jobId);
126                 
127                 try {
128                         OperStatusVnfResponse response = vnfmDriverMgmr.getOperStatus(vnfmId, jobId);
129                         httpResponse.setStatus(HttpStatus.SC_CREATED);
130                         return response;
131                 }
132                 catch(VnfmDriverException e)
133                 {
134                         try {
135                                 httpResponse.sendError(e.getHttpStatus(), e.getMessage());
136                         } catch (IOException e1) {
137                                 
138                         }
139                 }
140                 
141                 return null;
142     }
143         
144         
145         @RequestMapping(value = "/{vnfmId}/vnfs/{vnfInstanceId}/scale", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
146     @ResponseBody
147     public ScaleVnfResponse scaleVnf(@RequestBody ScaleVnfRequest request, @PathVariable("vnfmId") String vnfmId, @PathVariable("vnfInstanceId") String vnfInstanceId, HttpServletResponse httpResponse)
148     {
149                 String jsonString = gson.toJson(request);
150                 logger.info("scaleVnf request: vnfmId = " + vnfmId + ", vnfInstanceId = " + vnfInstanceId + ", bodyMessage is " + jsonString);
151                 
152                 try {
153                         ScaleVnfResponse response = vnfmDriverMgmr.scaleVnf(request, vnfmId, vnfInstanceId);
154                         httpResponse.setStatus(HttpStatus.SC_CREATED);
155                         return response;
156                 }
157                 catch(VnfmDriverException e)
158                 {
159                         try {
160                                 httpResponse.sendError(e.getHttpStatus(), e.getMessage());
161                         } catch (IOException e1) {
162                                 
163                         }
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                         try {
185                                 httpResponse.sendError(e.getHttpStatus(), e.getMessage());
186                         } catch (IOException e1) {
187                                 
188                         }
189                 }
190                 
191                 return null;
192     }
193
194
195 }