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