06d972de3f3f866ffafc101462fbfd83ddc43100
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016-2017 Huawei Technologies Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.vfc.nfvo.resmanagement.service.rest;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
23 import javax.servlet.http.HttpServletRequest;
24 import javax.ws.rs.Consumes;
25 import javax.ws.rs.DELETE;
26 import javax.ws.rs.GET;
27 import javax.ws.rs.POST;
28 import javax.ws.rs.PUT;
29 import javax.ws.rs.Path;
30 import javax.ws.rs.PathParam;
31 import javax.ws.rs.Produces;
32 import javax.ws.rs.QueryParam;
33 import javax.ws.rs.core.Context;
34 import javax.ws.rs.core.MediaType;
35
36 import org.onap.vfc.nfvo.resmanagement.common.constant.HttpConstant;
37 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
38 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
39 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
40 import org.onap.vfc.nfvo.resmanagement.common.util.response.ResponseUtil;
41 import org.onap.vfc.nfvo.resmanagement.common.util.response.RoaResponseUtil;
42 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Port;
43 import org.onap.vfc.nfvo.resmanagement.service.entity.PortEntity;
44 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48 import net.sf.json.JSONObject;
49
50 /**
51  *
52  * Port ROA Class.<br>
53  * <p>
54  * </p>
55  *
56  * @author
57  * @version     VFC 1.0  Sep 10, 2016
58  */
59 @Path(UrlConstant.PORT_URL)
60 @Produces(MediaType.APPLICATION_JSON)
61 @Consumes(MediaType.APPLICATION_JSON)
62 public class PortRoa {
63
64     private static final Logger LOGGER = LoggerFactory.getLogger(PortRoa.class);
65
66     private Port port;
67
68     /**
69      *
70      * Get details of Ports.<br>
71      *
72      * @param context
73      * @return
74      * @throws ServiceException
75      * @since  VFC 1.0
76      */
77     @GET
78     public JSONObject getPorts(@Context HttpServletRequest context) throws ServiceException {
79         Map<String, Object> map = new HashMap<>(10);
80         List<PortEntity> ports = port.getList(map);
81
82         JSONObject result = new JSONObject();
83         result.put("ports", ports);
84         return result;
85     }
86
87     /**
88      *
89      * Get port details.<br>
90      *
91      * @param context
92      * @param id
93      * @return
94      * @throws ServiceException
95      * @since  VFC 1.0
96      */
97     @GET
98     @Path("/{portId}")
99     public JSONObject getPort(@Context HttpServletRequest context, @PathParam("portId") String id)
100             throws ServiceException {
101         LOGGER.info("PortRoa::getPort id:{}", id);
102         Map<String, Object> map = new HashMap<>(10);
103         map.put(ParamConstant.PARAM_ID, id);
104         List<PortEntity> ports = port.getList(map);
105
106         JSONObject result = new JSONObject();
107         result.put("ports", ports);
108         return result;
109     }
110
111     /**
112      *
113      * Add port.<br>
114      *
115      * @param context
116      * @return
117      * @throws ServiceException
118      * @since  VFC 1.0
119      */
120     @POST
121     public JSONObject addPort(@Context HttpServletRequest context) throws ServiceException {
122         JSONObject object = RequestUtil.getJsonRequestBody(context);
123
124         LOGGER.info("PortRoa::addPort:{}", object.toString());
125         try {
126             int result = port.add(PortEntity.toEntity(object));
127             return RoaResponseUtil.add(result);
128         } catch(ServiceException se) {
129             LOGGER.error("PortRoa::addPort error:{}" + se);
130             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
131         }
132     }
133
134     /**
135      *
136      * Delete port.<br>
137      *
138      * @param context
139      * @param id
140      * @return
141      * @throws ServiceException
142      * @since  VFC 1.0
143      */
144     @DELETE
145     public JSONObject deletePort(@Context HttpServletRequest context, @QueryParam(ParamConstant.PARAM_ID) String id)
146             throws ServiceException {
147         LOGGER.info("PortRoa::deletePort id:{}", id);
148         try {
149             int result = port.delete(id);
150             return RoaResponseUtil.delete(result);
151         } catch(ServiceException se) {
152             LOGGER.error("PortRoa::deletePort error:{}" + se);
153             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
154         }
155     }
156
157     /**
158      *
159      * Update port.<br>
160      *
161      * @param context
162      * @return
163      * @throws ServiceException
164      * @since  VFC 1.0
165      */
166     @PUT
167     public JSONObject updatePort(@Context HttpServletRequest context) throws ServiceException {
168         JSONObject object = RequestUtil.getJsonRequestBody(context);
169
170         LOGGER.info("PortRoa::updatePort:{}", object.toString());
171         try {
172             int result = port.update(object);
173             return RoaResponseUtil.update(result);
174         } catch(ServiceException se) {
175             LOGGER.error("PortRoa::updatePort error:{}" + se);
176             return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
177         }
178     }
179
180     public void setPort(Port port) {
181         this.port = port;
182     }
183 }