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.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
24 import javax.servlet.http.HttpServletRequest;
25 import javax.ws.rs.Consumes;
26 import javax.ws.rs.DELETE;
27 import javax.ws.rs.GET;
28 import javax.ws.rs.POST;
29 import javax.ws.rs.PUT;
30 import javax.ws.rs.Path;
31 import javax.ws.rs.PathParam;
32 import javax.ws.rs.Produces;
33 import javax.ws.rs.QueryParam;
34 import javax.ws.rs.core.Context;
35 import javax.ws.rs.core.MediaType;
37 import org.onap.vfc.nfvo.resmanagement.common.ResourceUtil;
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.JsonUtil;
42 import org.onap.vfc.nfvo.resmanagement.common.util.request.RequestUtil;
43 import org.onap.vfc.nfvo.resmanagement.common.util.response.ResponseUtil;
44 import org.onap.vfc.nfvo.resmanagement.common.util.response.RoaResponseUtil;
45 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Location;
46 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Sites;
47 import org.onap.vfc.nfvo.resmanagement.service.entity.LocationEntity;
48 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
49 import org.openo.baseservice.remoteservice.exception.ServiceException;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 import net.sf.json.JSONObject;
56 * Location ROA Class.<br>
61 * @version NFVO 0.5 Sep 10, 2016
63 @Path(UrlConstant.LOCATION_URL)
64 @Produces(MediaType.APPLICATION_JSON)
65 @Consumes(MediaType.APPLICATION_JSON)
66 public class LocationRoa {
68 private static final Logger LOGGER = LoggerFactory.getLogger(LocationRoa.class);
70 private Location location;
75 * Get Locations Base.<br>
79 * @throws ServiceException
83 public JSONObject getLocationsbase(@Context HttpServletRequest context) throws ServiceException {
84 Map<String, Object> condition = new HashMap<>();
85 List<LocationEntity> locations = location.get(condition);
87 JSONObject result = new JSONObject();
88 result.put("locations", locations);
93 * Get Locations Base.<br>
98 * @throws ServiceException
102 @Path("/{locationId}")
103 public JSONObject getLocationbase(@Context HttpServletRequest context, @PathParam("locationId") String id)
104 throws ServiceException {
105 LOGGER.info("LocationRoa::getLocationbase id:{}", id);
106 Map<String, Object> map = new HashMap<>();
107 map.put(ParamConstant.PARAM_ID, id);
108 List<LocationEntity> locations = location.get(map);
110 JSONObject result = new JSONObject();
111 result.put("locations", locations);
120 * @throws ServiceException
125 @Consumes(MediaType.APPLICATION_JSON)
126 @Produces(MediaType.APPLICATION_JSON)
127 public JSONObject getCountry(@Context HttpServletRequest context) throws ServiceException {
128 return RoaResponseUtil.get(location.getCountry());
132 * Get Location by Country.<br>
137 * @throws ServiceException
141 @Path("/locationbycountry")
142 @Consumes(MediaType.APPLICATION_JSON)
143 @Produces(MediaType.APPLICATION_JSON)
144 public JSONObject getLocationByCountry(@Context HttpServletRequest context,
145 @QueryParam(ParamConstant.PARAM_COUNTRY) String country) throws ServiceException {
146 LOGGER.info("LocationRoa::getLocationByCountry country:{}", country);
147 Map<String, Object> condition = new HashMap<>();
148 condition.put(ParamConstant.PARAM_COUNTRY, country);
149 return RoaResponseUtil.get(location.getLocationByCountry(condition));
153 * Get Cloud Service.<br>
157 * @throws ServiceException
161 @Path("/cloudservice")
162 @Consumes(MediaType.APPLICATION_JSON)
163 @Produces(MediaType.APPLICATION_JSON)
164 public JSONObject getCloudservice(@Context HttpServletRequest context) throws ServiceException {
165 return RoaResponseUtil.get(location.getCloudservice());
169 * Get location details.<br>
174 * @throws ServiceException
179 @Consumes(MediaType.APPLICATION_JSON)
180 @Produces(MediaType.APPLICATION_JSON)
181 public JSONObject getLocation(@Context HttpServletRequest context,
182 @QueryParam(ParamConstant.PARAM_LOCATION) String locations) throws ServiceException {
183 LOGGER.info("LocationRoa::getLocation locations:{}", locations);
184 Map<String, Object> condition = new HashMap<>();
185 List<LocationEntity> loca = new ArrayList<LocationEntity>();
186 condition.put(ParamConstant.PARAM_LOCATION, locations);
187 loca = location.get(condition);
188 LOGGER.info("LocationRoa::getLocation loca:{}", loca);
189 return RoaResponseUtil.get(location.getLocationInfo(loca));
197 * @throws ServiceException
201 @Consumes(MediaType.APPLICATION_JSON)
202 @Produces(MediaType.APPLICATION_JSON)
203 public JSONObject addLocation(@Context HttpServletRequest context) throws ServiceException {
204 JSONObject object = RequestUtil.getJsonRequestBody(context);
206 LOGGER.info("LocationRoa::addLocation : " + object.toString());
208 int result = location.add(object);
209 return RoaResponseUtil.add(result);
210 } catch(ServiceException se) {
211 LOGGER.error("LocationRoa::addLocation error:{}" + se);
212 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
217 * Delete Location Base.<br>
222 * @throws ServiceException
227 public JSONObject deleteLocationbase(@Context HttpServletRequest context,
228 @PathParam(ParamConstant.PARAM_LOCATION) String locations) throws ServiceException {
229 LOGGER.info("LocationRoa::deleteLocation locations:{}", locations);
230 Map<String, Object> condition = new HashMap<>();
231 condition.put(ParamConstant.PARAM_LOCATION, locations);
233 int result = location.delete(locations);
234 return RoaResponseUtil.delete(result);
235 } catch(ServiceException se) {
236 LOGGER.error("LocationRoa::deleteLocationbase error:{}" + se);
237 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
242 * Delete Location.<br>
246 * @throws ServiceException
250 @Consumes(MediaType.APPLICATION_JSON)
251 @Produces(MediaType.APPLICATION_JSON)
252 public JSONObject deleteLocation(@Context HttpServletRequest context) throws ServiceException {
254 JSONObject object = RequestUtil.getJsonRequestBody(context);
255 String locations = JsonUtil.getJsonFieldStr(object, ParamConstant.PARAM_LOCATION);
256 String id = JsonUtil.getJsonFieldStr(object, ParamConstant.PARAM_ID);
257 LOGGER.info("LocationRoa::deleteLocation locations:{}", locations);
259 Map<String, Object> condition = new HashMap<>();
260 condition.put(ParamConstant.PARAM_LOCATION, locations);
261 SitesEntity sitesEntity = sites.get(condition);
263 if(sitesEntity != null) {
264 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE,
265 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.delete.used.check"));
267 int result = location.delete(id);
268 return RoaResponseUtil.delete(result);
269 } catch(ServiceException se) {
270 LOGGER.error("LocationRoa::deleteLocation error:{}" + se);
271 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
276 * Update Location.<br>
280 * @throws ServiceException
284 @Consumes(MediaType.APPLICATION_JSON)
285 @Produces(MediaType.APPLICATION_JSON)
286 public JSONObject updateLocation(@Context HttpServletRequest context) throws ServiceException {
287 JSONObject object = RequestUtil.getJsonRequestBody(context);
288 String local = JsonUtil.getJsonFieldStr(object, ParamConstant.PARAM_LOCATION);
289 Map<String, Object> localInfo = new HashMap<>();
290 localInfo.put(ParamConstant.PARAM_LOCATION, local);
291 SitesEntity sitesEntity = sites.get(localInfo);
292 if(sitesEntity != null) {
293 LOGGER.error("function=updateLocation; msg=update error, because location is used.");
294 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE,
295 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.update.used.check"));
297 LOGGER.info("LocationRoa::updateLocation : " + object.toString());
300 int result = location.update(object);
301 return RoaResponseUtil.update(result);
302 } catch(ServiceException se) {
303 LOGGER.error("LocationRoa::update Location error:{}" + se);
304 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE, se.getMessage());
308 public void setLocation(Location location) {
309 this.location = location;
312 public void setSites(Sites sites) {