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.core.Context;
33 import javax.ws.rs.core.MediaType;
35 import org.apache.logging.log4j.LogManager;
36 import org.apache.logging.log4j.Logger;
37 import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
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.Sites;
45 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
46 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
48 import net.sf.json.JSONArray;
49 import net.sf.json.JSONObject;
52 * Sites ROA method<br/>
57 * @version VFC 1.0 Aug 24, 2016
59 @Path(UrlConstant.SITES_URL)
60 @Consumes(MediaType.APPLICATION_JSON)
61 @Produces(MediaType.APPLICATION_JSON)
62 public class SitesRoa {
64 private static final Logger LOGGER = LogManager.getLogger(SitesRoa.class);
69 * getSites ROA method<br/>
72 * @return the get result
73 * @throws ServiceException When get failed.
77 public JSONObject getSites(@Context HttpServletRequest context) throws ServiceException {
78 Map<String, Object> map = new HashMap<>(10);
79 List<SitesEntity> datacenters = sites.getList(map);
81 JSONObject result = new JSONObject();
82 result.put("datacenters", datacenters);
87 * getSite ROA method<br/>
91 * @return the get result
92 * @throws ServiceException When get failed.
96 @Path("/{datacenterId}")
97 public JSONObject getSite(@Context HttpServletRequest context, @PathParam("datacenterId") String id)
98 throws ServiceException {
99 LOGGER.warn("SitesRoa::getSitesById id:{}", id);
100 Map<String, Object> map = new HashMap<>(10);
101 map.put(ParamConstant.PARAM_ID, id);
102 List<SitesEntity> datacenters = sites.getList(map);
104 JSONObject result = new JSONObject();
105 result.put("datacenters", datacenters);
110 * addSites ROA method<br/>
114 * @return the add result
115 * @throws ServiceException When add failed.
119 public JSONObject addSites(@Context HttpServletRequest context) throws ServiceException {
120 JSONObject json = RequestUtil.getAllJsonRequestBody(context);
122 LOGGER.warn("SitesRoa:: start add Sites");
124 int result = sites.add(json);
125 sites.sendToMonitor(json);
126 return RoaResponseUtil.add(result);
127 } catch(ServiceException se) {
128 LOGGER.error("SitesRoa::addSites error:{}" + se);
129 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
134 * deleteSites ROA method<br/>
138 * @return the delete result
139 * @throws ServiceException When delete failed.
143 @Path("/{datacenterId}")
144 public JSONObject deleteSites(@Context HttpServletRequest context, @PathParam("datacenterId") String id)
145 throws ServiceException {
146 LOGGER.warn("SitesRoa::deleteSites siteId:{}", id);
148 int result = sites.delete(id);
149 return RoaResponseUtil.delete(result);
150 } catch(ServiceException se) {
151 LOGGER.error("SitesRoa::deleteSites error: " + se);
152 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
157 * updateSites ROA method<br/>
161 * @return the update result
162 * @throws ServiceException When update failed.
166 public JSONObject updateSites(@Context HttpServletRequest context) throws ServiceException {
167 JSONObject json = RequestUtil.getAllJsonRequestBody(context);
169 LOGGER.warn("SitesRoa::start update Sites");
171 int result = sites.update(SitesEntity.toEntity(json));
172 return RoaResponseUtil.update(result);
173 } catch(ServiceException se) {
174 LOGGER.error("SitesRoa::updateSites error:" + se);
175 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
180 * grant resource method
185 * @throws ServiceException
190 public JSONObject grantResource(@Context HttpServletRequest context) throws ServiceException {
191 JSONObject json = RequestUtil.getAllJsonRequestBody(context);
193 LOGGER.warn("SitesRoa::grant resource");
195 int result = sites.update(json);
196 return RoaResponseUtil.update(result);
197 } catch(ServiceException se) {
198 LOGGER.error("SitesRoa::grant resource:" + se);
199 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
205 public String getVims(@Context HttpServletRequest context) throws ServiceException {
206 LOGGER.info("SitesRoa::get vims");
207 JSONArray vims = VimUtil.getVims();
208 JSONObject result = new JSONObject();
209 result.put("data", vims);
210 return result.toString();
214 @Path("/vims/{vimId}")
215 public String getVim(@Context HttpServletRequest context, @PathParam("vimId") String vimId)
216 throws ServiceException {
217 LOGGER.info("SitesRoa::get vim by id: {}", vimId);
218 return VimUtil.getVimById(vimId).toString();
221 public void setSites(Sites sites) {