Merge "Reorder modifiers"
[so.git] / adapters / mso-vfc-adapter / src / main / java / org / openecomp / mso / adapters / vfc / VfcAdapterRest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.mso.adapters.vfc;
22
23 import javax.ws.rs.Consumes;
24 import javax.ws.rs.DELETE;
25 import javax.ws.rs.POST;
26 import javax.ws.rs.Path;
27 import javax.ws.rs.PathParam;
28 import javax.ws.rs.Produces;
29 import javax.ws.rs.core.MediaType;
30 import javax.ws.rs.core.Response;
31
32 import org.openecomp.mso.adapters.vfc.exceptions.ApplicationException;
33 import org.openecomp.mso.adapters.vfc.model.NSResourceInputParameter;
34 import org.openecomp.mso.adapters.vfc.model.NsOperationKey;
35 import org.openecomp.mso.adapters.vfc.model.RestfulResponse;
36 import org.openecomp.mso.adapters.vfc.util.JsonUtil;
37 import org.openecomp.mso.adapters.vfc.util.ValidateUtil;
38 import org.openecomp.mso.logger.MsoLogger;
39
40 /**
41  * The rest class for VF-c Adapter <br>
42  * <p>
43  * </p>
44  * 
45  * @author
46  * @version ONAP Amsterdam Release 2017-08-28
47  */
48 @Path("/v1/vfcadapter")
49 public class VfcAdapterRest {
50
51     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
52
53     private final VfcManager driverMgr = new VfcManager();
54
55     public VfcAdapterRest() {
56
57     }
58
59     /**
60      * Create a NS <br>
61      * 
62      * @param data the http request
63      * @return
64      * @since ONAP Amsterdam Release
65      */
66     @POST
67     @Path("/ns")
68     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
69     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
70     public Response createNfvoNs(String data) {
71         try {
72             ValidateUtil.assertObjectNotNull(data);
73             LOGGER.debug("body from request is {}" + data);
74             NSResourceInputParameter nsInput = JsonUtil.unMarshal(data, NSResourceInputParameter.class);
75             RestfulResponse rsp = driverMgr.createNs(nsInput);
76             return buildResponse(rsp);
77         } catch(ApplicationException e) {
78             LOGGER.debug("ApplicationException: ", e);
79             return e.buildErrorResponse();
80         }
81     }
82
83     /**
84      * Delete NS instance<br>
85      *
86      * @param servletReq http request
87      * @return response
88      * @since ONAP Amsterdam Release
89      */
90     @DELETE
91     @Path("/ns/{nsInstanceId}")
92     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
93     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
94     public Response deleteNfvoNs(String data, @PathParam("nsInstanceId") String nsInstanceId) {
95         try {
96
97             ValidateUtil.assertObjectNotNull(data);
98             LOGGER.debug("body from request is {}" + data);
99             NsOperationKey nsOperationKey = JsonUtil.unMarshal(data, NsOperationKey.class);
100             RestfulResponse rsp = driverMgr.deleteNs(nsOperationKey, nsInstanceId);
101             return buildResponse(rsp);
102         } catch(ApplicationException e) {
103             LOGGER.debug("ApplicationException: ", e);
104             return e.buildErrorResponse();
105         }
106     }
107
108     /**
109      * Query Operation job status <br>
110      * 
111      * @param servletReq The Http Request
112      * @param jobId The job id
113      * @return
114      * @since ONAP Amsterdam Release
115      */
116     @POST
117     @Path("/jobs/{jobId}")
118     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
119     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
120     public Response queryNfvoJobStatus(String data, @PathParam("jobId") String jobId) {
121         try {
122             ValidateUtil.assertObjectNotNull(data);
123             LOGGER.debug("body from request is {}" + data);
124             NsOperationKey nsOperationKey = JsonUtil.unMarshal(data, NsOperationKey.class);
125             RestfulResponse rsp = driverMgr.getNsProgress(nsOperationKey, jobId);
126             return buildResponse(rsp);
127         } catch(ApplicationException e) {
128             LOGGER.debug("ApplicationException: ", e);
129             return e.buildErrorResponse();
130         }
131     }
132
133     /**
134      * Instantiate NS instance <br>
135      * 
136      * @param servletReq The http request
137      * @param nsInstanceId The NS instance id
138      * @return
139      * @since ONAP Amsterdam Release
140      */
141     @POST
142     @Path("/ns/{nsInstanceId}/instantiate")
143     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
144     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
145     public Response instantiateNfvoNs(String data, @PathParam("nsInstanceId") String nsInstanceId) {
146         try {
147             ValidateUtil.assertObjectNotNull(data);
148             LOGGER.debug("body from request is {}" + data);
149             NSResourceInputParameter nsInput = JsonUtil.unMarshal(data, NSResourceInputParameter.class);
150             RestfulResponse rsp = driverMgr.instantiateNs(nsInstanceId, nsInput);
151             return buildResponse(rsp);
152         } catch(ApplicationException e) {
153             LOGGER.debug("ApplicationException: ", e);
154             return e.buildErrorResponse();
155         }
156     }
157
158     /**
159      * Terminate NS instance <br>
160      * 
161      * @param servletReq The http request
162      * @param nsInstanceId The NS instance id
163      * @return
164      * @since ONAP Amsterdam Release
165      */
166     @POST
167     @Path("/ns/{nsInstanceId}/terminate")
168     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
169     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
170     public Response terminateNfvoNs(String data, @PathParam("nsInstanceId") String nsInstanceId) {
171         try {
172             ValidateUtil.assertObjectNotNull(data);
173             LOGGER.debug("body from request is {}" + data);
174             NsOperationKey nsOperationKey = JsonUtil.unMarshal(data, NsOperationKey.class);
175             RestfulResponse rsp = driverMgr.terminateNs(nsOperationKey, nsInstanceId);
176             return buildResponse(rsp);
177         } catch(ApplicationException e) {
178             LOGGER.debug("ApplicationException: ", e);
179             return e.buildErrorResponse();
180         }
181     }
182
183           /**
184      * Scale NS instance
185      * <br>
186      * 
187      * @param servletReq The http request
188      * @param nsInstanceId The NS instance id
189      * @return
190      * @since ONAP Amsterdam Release
191      */
192     @POST
193     @Path("/ns/{nsInstanceId}/scale")
194     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
195     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
196     public Response scaleNfvoNs(String data, @PathParam("nsInstanceId") String nsInstanceId) {
197         try {
198                 ValidateUtil.assertObjectNotNull(data);
199             LOGGER.debug("Scale Ns Request Received.Body from request is {}" + data);
200                 NSResourceInputParameter nsInput = JsonUtil.unMarshal(data, NSResourceInputParameter.class);
201                 RestfulResponse rsp = driverMgr.scaleNs(nsInstanceId, nsInput);
202                 return buildResponse(rsp);
203         } catch(ApplicationException e) {
204                     LOGGER.debug("ApplicationException: ", e);
205                 return e.buildErrorResponse();
206         }
207     }
208
209
210     /**
211      * build response from restful response <br>
212      * 
213      * @param rsp general response object
214      * @return
215      * @since ONAP Amsterdam Release
216      */
217     private Response buildResponse(RestfulResponse rsp) {
218         return Response.status(rsp.getStatus()).entity(rsp.getResponseContent()).build();
219     }
220 }