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