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.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.Host;
43 import org.onap.vfc.nfvo.resmanagement.service.entity.HostEntity;
44 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
48 import net.sf.json.JSONObject;
57 * @version NFVO 0.5 Sep 10, 2016
59 @Path(UrlConstant.HOST_URL)
60 @Produces(MediaType.APPLICATION_JSON)
61 @Consumes(MediaType.APPLICATION_JSON)
62 public class HostRoa {
64 private static final Logger LOGGER = LoggerFactory.getLogger(HostRoa.class);
74 * @throws ServiceException
78 public JSONObject getHosts(@Context HttpServletRequest context) throws ServiceException {
79 Map<String, Object> map = new HashMap<>(10);
80 List<HostEntity> hosts = host.getList(map);
82 JSONObject result = new JSONObject();
83 result.put("hosts", hosts);
94 * @throws ServiceException
99 public JSONObject getHost(@Context HttpServletRequest context, @PathParam("hostId") String id)
100 throws ServiceException {
101 LOGGER.info("HostRoa::getHost id:{}", id);
102 Map<String, Object> map = new HashMap<>(10);
103 map.put(ParamConstant.PARAM_ID, id);
104 List<HostEntity> hosts = host.getList(map);
106 JSONObject result = new JSONObject();
107 result.put("hosts", hosts);
117 * @throws ServiceException
121 public JSONObject addHost(@Context HttpServletRequest context) throws ServiceException {
122 JSONObject object = RequestUtil.getJsonRequestBody(context);
124 LOGGER.info("HostRoa::addHost:{}", object.toString());
126 int result = host.add(object);
127 return RoaResponseUtil.add(result);
128 } catch(ServiceException se) {
129 LOGGER.error("HostRoa::addHost error:{}" + se);
130 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
141 * @throws ServiceException
145 public JSONObject deleteHost(@Context HttpServletRequest context, @QueryParam(ParamConstant.PARAM_ID) String id)
146 throws ServiceException {
147 LOGGER.info("HostRoa::deleteHost id:{}", id);
149 int result = host.delete(id);
150 return RoaResponseUtil.delete(result);
151 } catch(ServiceException se) {
152 LOGGER.error("HostRoa::deleteHost error:{}" + se);
153 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
163 * @throws ServiceException
167 public JSONObject updateHost(@Context HttpServletRequest context) throws ServiceException {
168 JSONObject object = RequestUtil.getJsonRequestBody(context);
170 LOGGER.info("HostRoa::updateHost:{}", object.toString());
172 int result = host.update(object);
173 return RoaResponseUtil.update(result);
174 } catch(ServiceException se) {
175 LOGGER.error("HostRoa::updateHost error:{}" + se);
176 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
180 public void setHost(Host host) {