52286fd3ac66e7681316f6cb8874f168c2e8693a
[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.servlet.http.HttpServletRequest;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.DELETE;
26 import javax.ws.rs.POST;
27 import javax.ws.rs.Path;
28 import javax.ws.rs.PathParam;
29 import javax.ws.rs.Produces;
30 import javax.ws.rs.core.MediaType;
31 import javax.ws.rs.core.Response;
32 import javax.ws.rs.core.Response.ResponseBuilder;
33
34 import org.openecomp.mso.adapters.vfc.exceptions.ApplicationException;
35 import org.openecomp.mso.adapters.vfc.model.NSResourceInputParameter;
36 import org.openecomp.mso.adapters.vfc.model.NsOperationKey;
37 import org.openecomp.mso.adapters.vfc.model.RestfulResponse;
38 import org.openecomp.mso.adapters.vfc.util.JsonUtil;
39 import org.openecomp.mso.adapters.vfc.util.RestfulUtil;
40 import org.openecomp.mso.adapters.vfc.util.ValidateUtil;
41 import org.openecomp.mso.logger.MsoLogger;
42
43 /**
44  * The rest class for VF-c Adapter
45  * <br>
46  * <p>
47  * </p>
48  * 
49  * @author
50  * @version ONAP Amsterdam Release 2017-08-28
51  */
52 @Path("/vfcadapter/v1")
53 public class VfcAdapterRest {
54
55     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA);
56
57     private final VfcManager driverMgr = new VfcManager();
58
59     public VfcAdapterRest() {
60
61     }
62
63     /**
64      * Create a NS
65      * <br>
66      * 
67      * @param servletReq the http request
68      * @return
69      * @since ONAP Amsterdam Release
70      */
71     @POST
72     @Path("/ns")
73     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
74     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
75     public Response createNfvoNs(HttpServletRequest servletReq) {
76         // Step 1: get parameters from request for current node
77         try {
78             String body = RestfulUtil.getRequestBody(servletReq);
79             ValidateUtil.assertObjectNotNull(body);
80             LOGGER.debug("body from request is {}" + body);
81             NSResourceInputParameter nsInput = JsonUtil.unMarshal(body, NSResourceInputParameter.class);
82             RestfulResponse rsp = driverMgr.createNs(nsInput);
83             return buildResponse(rsp);
84         } catch(ApplicationException e) {
85             LOGGER.debug("ApplicationException: ", e);
86             return e.buildErrorResponse();
87         }
88     }
89
90     /**
91      * Delete NS instance<br>
92      *
93      * @param servletReq http request
94      * @return response
95      * @since ONAP Amsterdam Release
96      */
97     @DELETE
98     @Path("/ns/{nsInstanceId}")
99     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
100     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
101     public Response deleteNfvoNs(HttpServletRequest servletReq, @PathParam("nsInstanceId") String nsInstanceId) {
102         try {
103             // Step 1: get parameters from request for current node
104             String body = RestfulUtil.getRequestBody(servletReq);
105             ValidateUtil.assertObjectNotNull(body);
106             LOGGER.debug("body from request is {}" + body);
107             NsOperationKey nsOperationKey = JsonUtil.unMarshal(body, NsOperationKey.class);
108             RestfulResponse rsp = driverMgr.deleteNs(nsOperationKey, nsInstanceId);
109             return buildResponse(rsp);
110         } catch(ApplicationException e) {
111             LOGGER.debug("ApplicationException: ", e);
112             return e.buildErrorResponse();
113         }
114     }
115
116     /**
117      * Query Operation job status
118      * <br>
119      * 
120      * @param servletReq The Http Request
121      * @param jobId The job id
122      * @return
123      * @since ONAP Amsterdam Release
124      */
125     @POST
126     @Path("/jobs/{jobId}")
127     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
128     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
129     public Response queryNfvoJobStatus(HttpServletRequest servletReq, @PathParam("jobId") String jobId) {
130         try {
131             ValidateUtil.assertObjectNotNull(jobId);
132             String body = RestfulUtil.getRequestBody(servletReq);
133             ValidateUtil.assertObjectNotNull(body);
134             LOGGER.debug("body from request is {}" + body);
135             NsOperationKey nsOperationKey = JsonUtil.unMarshal(body, NsOperationKey.class);
136
137             RestfulResponse rsp = driverMgr.getNsProgress(nsOperationKey, jobId);
138             return buildResponse(rsp);
139         } catch(ApplicationException e) {
140             LOGGER.debug("ApplicationException: ", e);
141             return e.buildErrorResponse();
142         }
143     }
144
145     /**
146      * Instantiate NS instance
147      * <br>
148      * 
149      * @param servletReq The http request
150      * @param nsInstanceId The NS instance id
151      * @return
152      * @since ONAP Amsterdam Release
153      */
154     @POST
155     @Path("/ns/{nsInstanceId}/instantiate")
156     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
157     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
158     public Response instantiateNfvoNs(HttpServletRequest servletReq, @PathParam("nsInstanceId") String nsInstanceId) {
159         String body = RestfulUtil.getRequestBody(servletReq);
160         try {
161             ValidateUtil.assertObjectNotNull(body);
162             LOGGER.debug("body from request is {}" + body);
163             NSResourceInputParameter nsInput = JsonUtil.unMarshal(body, NSResourceInputParameter.class);
164             RestfulResponse rsp = driverMgr.instantiateNs(nsInstanceId, nsInput);
165             return buildResponse(rsp);
166         } catch(ApplicationException e) {
167             LOGGER.debug("ApplicationException: ", e);
168             return e.buildErrorResponse();
169         }
170     }
171
172     /**
173      * Terminate NS instance
174      * <br>
175      * 
176      * @param servletReq The http request
177      * @param nsInstanceId The NS instance id
178      * @return
179      * @since ONAP Amsterdam Release
180      */
181     @POST
182     @Path("/ns/{nsInstanceId}/terminate")
183     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
184     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
185     public Response terminateNfvoNs(HttpServletRequest servletReq, @PathParam("nsInstanceId") String nsInstanceId) {
186         try {
187             ValidateUtil.assertObjectNotNull(nsInstanceId);
188             String body = RestfulUtil.getRequestBody(servletReq);
189             ValidateUtil.assertObjectNotNull(body);
190             LOGGER.debug("body from request is {}" + body);
191             NsOperationKey nsOperationKey = JsonUtil.unMarshal(body, NsOperationKey.class);
192             RestfulResponse rsp = driverMgr.terminateNs(nsOperationKey, nsInstanceId);
193             return buildResponse(rsp);
194         } catch(ApplicationException e) {
195             LOGGER.debug("ApplicationException: ", e);
196             return e.buildErrorResponse();
197         }
198     }
199
200     /**
201      * build response from restful response
202      * <br>
203      * 
204      * @param rsp general response object
205      * @return
206      * @since ONAP Amsterdam Release
207      */
208     private Response buildResponse(RestfulResponse rsp) {
209         ResponseBuilder rspBuilder = Response.status(rsp.getStatus());
210         rspBuilder.entity(rsp.getResponseContent());
211         return rspBuilder.build();
212     }
213 }