2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.resmanagement.service.rest;
19 import java.util.HashMap;
20 import java.util.List;
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;
36 import org.apache.logging.log4j.LogManager;
37 import org.apache.logging.log4j.Logger;
38 import org.onap.vfc.nfvo.resmanagement.common.constant.HttpConstant;
39 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
40 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
41 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
42 import org.onap.vfc.nfvo.resmanagement.common.util.response.ResponseUtil;
43 import org.onap.vfc.nfvo.resmanagement.common.util.response.RoaResponseUtil;
44 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Port;
45 import org.onap.vfc.nfvo.resmanagement.service.entity.PortEntity;
46 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
48 import net.sf.json.JSONObject;
57 * @version VFC 1.0 Sep 10, 2016
59 @Path(UrlConstant.PORT_URL)
60 @Produces(MediaType.APPLICATION_JSON)
61 @Consumes(MediaType.APPLICATION_JSON)
62 public class PortRoa {
64 private static final Logger LOGGER = LogManager.getLogger(PortRoa.class);
70 * Get details of Ports.<br>
74 * @throws ServiceException
78 public JSONObject getPorts(@Context HttpServletRequest context) throws ServiceException {
79 Map<String, Object> map = new HashMap<>(10);
80 List<PortEntity> ports = port.getList(map);
82 JSONObject result = new JSONObject();
83 result.put("ports", ports);
89 * Get port details.<br>
94 * @throws ServiceException
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);
106 JSONObject result = new JSONObject();
107 result.put("ports", ports);
117 * @throws ServiceException
121 public JSONObject addPort(@Context HttpServletRequest context) throws ServiceException {
122 JSONObject object = RequestUtil.getJsonRequestBody(context);
124 LOGGER.info("PortRoa::addPort:{}", object.toString());
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());
141 * @throws ServiceException
145 public JSONObject deletePort(@Context HttpServletRequest context, @QueryParam(ParamConstant.PARAM_ID) String id)
146 throws ServiceException {
147 LOGGER.info("PortRoa::deletePort id:{}", id);
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());
163 * @throws ServiceException
167 public JSONObject updatePort(@Context HttpServletRequest context) throws ServiceException {
168 JSONObject object = RequestUtil.getJsonRequestBody(context);
170 LOGGER.info("PortRoa::updatePort:{}", object.toString());
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());
180 public void setPort(Port port) {