Containerization feature of SO
[so.git] / adapters / mso-vfc-adapter / src / main / java / org / onap / so / 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.onap.so.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.onap.so.adapters.vfc.exceptions.ApplicationException;
33 import org.onap.so.adapters.vfc.model.NSResourceInputParameter;
34 import org.onap.so.adapters.vfc.model.NsOperationKey;
35 import org.onap.so.adapters.vfc.model.RestfulResponse;
36 import org.onap.so.adapters.vfc.util.JsonUtil;
37 import org.onap.so.adapters.vfc.util.ValidateUtil;
38 import org.onap.so.logger.MsoLogger;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41
42 /**
43  * The rest class for VF-c Adapter <br>
44  * <p>
45  * </p>
46  * 
47  * @author
48  * @version ONAP Amsterdam Release 2017-08-28
49  */
50 @Component
51 @Path("/v1/vfcadapter")
52 public class VfcAdapterRest {
53
54     private static final MsoLogger LOGGER = MsoLogger.getMsoLogger(MsoLogger.Catalog.RA, VfcAdapterRest.class);
55
56     @Autowired
57     private VfcManager driverMgr ;
58
59     public VfcAdapterRest() {
60
61     }
62
63     /**
64      * Create a NS <br>
65      * 
66      * @param data http request
67      * @return
68      * @since ONAP Amsterdam Release
69      */
70     @POST
71     @Path("/ns")
72     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
73     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
74     public Response createNfvoNs(String data) {
75         try {
76             ValidateUtil.assertObjectNotNull(data);
77             LOGGER.debug("body from request is {}" + data);
78             NSResourceInputParameter nsInput = JsonUtil.unMarshal(data, NSResourceInputParameter.class);
79             RestfulResponse rsp = driverMgr.createNs(nsInput);
80             return buildResponse(rsp);
81         } catch(ApplicationException e) {
82             LOGGER.debug("ApplicationException: ", e);
83             return e.buildErrorResponse();
84         }
85     }
86
87     /**
88      * Delete NS instance<br>
89      *
90      * @param data The http request
91      * @param  nsInstanceId The NS instance id
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(String data, @PathParam("nsInstanceId") String nsInstanceId) {
100         try {
101
102             ValidateUtil.assertObjectNotNull(data);
103             LOGGER.debug("body from request is {}" + data);
104             NsOperationKey nsOperationKey = JsonUtil.unMarshal(data, NsOperationKey.class);
105             RestfulResponse rsp = driverMgr.deleteNs(nsOperationKey, nsInstanceId);
106             return buildResponse(rsp);
107         } catch(ApplicationException e) {
108             LOGGER.debug("ApplicationException: ", e);
109             return e.buildErrorResponse();
110         }
111     }
112
113     /**
114      * Query Operation job status <br>
115      * 
116      * @param data The Http Request
117      * @param jobId The job id
118      * @return
119      * @since ONAP Amsterdam Release
120      */
121     @POST
122     @Path("/jobs/{jobId}")
123     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
124     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
125     public Response queryNfvoJobStatus(String data, @PathParam("jobId") String jobId) {
126         try {
127             ValidateUtil.assertObjectNotNull(data);
128             LOGGER.debug("body from request is {}" + data);
129             NsOperationKey nsOperationKey = JsonUtil.unMarshal(data, NsOperationKey.class);
130             RestfulResponse rsp = driverMgr.getNsProgress(nsOperationKey, jobId);
131             return buildResponse(rsp);
132         } catch(ApplicationException e) {
133             LOGGER.debug("ApplicationException: ", e);
134             return e.buildErrorResponse();
135         }
136     }
137
138     /**
139      * Instantiate NS instance <br>
140      * 
141      * @param data The http request
142      * @param nsInstanceId The NS instance id
143      * @return
144      * @since ONAP Amsterdam Release
145      */
146     @POST
147     @Path("/ns/{nsInstanceId}/instantiate")
148     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
149     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
150     public Response instantiateNfvoNs(String data, @PathParam("nsInstanceId") String nsInstanceId) {
151         try {
152             ValidateUtil.assertObjectNotNull(data);
153             LOGGER.debug("body from request is {}" + data);
154             NSResourceInputParameter nsInput = JsonUtil.unMarshal(data, NSResourceInputParameter.class);
155             RestfulResponse rsp = driverMgr.instantiateNs(nsInstanceId, nsInput);
156             return buildResponse(rsp);
157         } catch(ApplicationException e) {
158             LOGGER.debug("ApplicationException: ", e);
159             return e.buildErrorResponse();
160         }
161     }
162
163     /**
164      * Terminate NS instance <br>
165      * 
166      * @param data The http request
167      * @param nsInstanceId The NS instance id
168      * @return
169      * @since ONAP Amsterdam Release
170      */
171     @POST
172     @Path("/ns/{nsInstanceId}/terminate")
173     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
174     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
175     public Response terminateNfvoNs(String data, @PathParam("nsInstanceId") String nsInstanceId) {
176         try {
177             ValidateUtil.assertObjectNotNull(data);
178             LOGGER.debug("body from request is {}" + data);
179             NsOperationKey nsOperationKey = JsonUtil.unMarshal(data, NsOperationKey.class);
180             RestfulResponse rsp = driverMgr.terminateNs(nsOperationKey, nsInstanceId);
181             return buildResponse(rsp);
182         } catch(ApplicationException e) {
183             LOGGER.debug("ApplicationException: ", e);
184             return e.buildErrorResponse();
185         }
186     }
187
188     /**
189      * Scale NS instance
190      * <br>
191      * 
192      * @param servletReq The http request
193      * @param nsInstanceId The NS instance id
194      * @return
195      * @since ONAP Amsterdam Release
196      */
197     @POST
198     @Path("/ns/{nsInstanceId}/scale")
199     @Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
200     @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
201     public Response scaleNfvoNs(String data, @PathParam("nsInstanceId") String nsInstanceId) {
202         try {
203                 ValidateUtil.assertObjectNotNull(data);
204             LOGGER.debug("Scale Ns Request Received.Body from request is {}" + data);
205                 NSResourceInputParameter nsInput = JsonUtil.unMarshal(data, NSResourceInputParameter.class);
206                 RestfulResponse rsp = driverMgr.scaleNs(nsInstanceId, nsInput);
207                 return buildResponse(rsp);
208         } catch(ApplicationException e) {
209                     LOGGER.debug("ApplicationException: ", e);
210                 return e.buildErrorResponse();
211         }
212     }
213
214
215     /**
216      * build response from restful response <br>
217      * 
218      * @param rsp general response object
219      * @return
220      * @since ONAP Amsterdam Release
221      */
222     private Response buildResponse(RestfulResponse rsp) {
223         return Response.status(rsp.getStatus()).entity(rsp.getResponseContent()).build();
224     }
225 }