b875f534e49e1961ff9fa443281082867912e24f
[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.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
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;
36
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;
52
53 import net.sf.json.JSONObject;
54
55 /**
56  * Location ROA Class.<br>
57  * <p>
58  * </p>
59  *
60  * @author
61  * @version NFVO 0.5 Sep 10, 2016
62  */
63 @Path(UrlConstant.LOCATION_URL)
64 @Produces(MediaType.APPLICATION_JSON)
65 @Consumes(MediaType.APPLICATION_JSON)
66 public class LocationRoa {
67
68     private static final Logger LOGGER = LoggerFactory.getLogger(LocationRoa.class);
69
70     private Location location;
71
72     private Sites sites;
73
74     /**
75      * Get Locations Base.<br>
76      *
77      * @param context
78      * @return
79      * @throws ServiceException
80      * @since NFVO 0.5
81      */
82     @GET
83     public JSONObject getLocationsbase(@Context HttpServletRequest context) throws ServiceException {
84         Map<String, Object> condition = new HashMap<>();
85         List<LocationEntity> locations = location.get(condition);
86
87         JSONObject result = new JSONObject();
88         result.put("locations", locations);
89         return result;
90     }
91
92     /**
93      * Get Locations Base.<br>
94      *
95      * @param context
96      * @param id
97      * @return
98      * @throws ServiceException
99      * @since NFVO 0.5
100      */
101     @GET
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);
109
110         JSONObject result = new JSONObject();
111         result.put("locations", locations);
112         return result;
113     }
114
115     /**
116      * Get Country.<br>
117      *
118      * @param context
119      * @return
120      * @throws ServiceException
121      * @since NFVO 0.5
122      */
123     @GET
124     @Path("/country")
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());
129     }
130
131     /**
132      * Get Location by Country.<br>
133      *
134      * @param context
135      * @param country
136      * @return
137      * @throws ServiceException
138      * @since NFVO 0.5
139      */
140     @GET
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));
150     }
151
152     /**
153      * Get Cloud Service.<br>
154      *
155      * @param context
156      * @return
157      * @throws ServiceException
158      * @since NFVO 0.5
159      */
160     @GET
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());
166     }
167
168     /**
169      * Get location details.<br>
170      *
171      * @param context
172      * @param locations
173      * @return
174      * @throws ServiceException
175      * @since NFVO 0.5
176      */
177     @GET
178     @Path("/site")
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));
190     }
191
192     /**
193      * Add Location.<br>
194      *
195      * @param context
196      * @return
197      * @throws ServiceException
198      * @since NFVO 0.5
199      */
200     @POST
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);
205
206         LOGGER.info("LocationRoa::addLocation : " + object.toString());
207         try {
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());
213         }
214     }
215
216     /**
217      * Delete Location Base.<br>
218      *
219      * @param context
220      * @param locations
221      * @return
222      * @throws ServiceException
223      * @since NFVO 0.5
224      */
225     @DELETE
226     @Path("/{location}")
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);
232         try {
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());
238         }
239     }
240
241     /**
242      * Delete Location.<br>
243      *
244      * @param context
245      * @return
246      * @throws ServiceException
247      * @since NFVO 0.5
248      */
249     @DELETE
250     @Consumes(MediaType.APPLICATION_JSON)
251     @Produces(MediaType.APPLICATION_JSON)
252     public JSONObject deleteLocation(@Context HttpServletRequest context) throws ServiceException {
253
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);
258
259         Map<String, Object> condition = new HashMap<>();
260         condition.put(ParamConstant.PARAM_LOCATION, locations);
261         SitesEntity sitesEntity = sites.get(condition);
262         try {
263             if(sitesEntity != null) {
264                 return ResponseUtil.genHttpResponse(HttpConstant.ERROR_CODE,
265                         ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.delete.used.check"));
266             }
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());
272         }
273     }
274
275     /**
276      * Update Location.<br>
277      *
278      * @param context
279      * @return
280      * @throws ServiceException
281      * @since NFVO 0.5
282      */
283     @PUT
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"));
296         }
297         LOGGER.info("LocationRoa::updateLocation : " + object.toString());
298
299         try {
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());
305         }
306     }
307
308     public void setLocation(Location location) {
309         this.location = location;
310     }
311
312     public void setSites(Sites sites) {
313         this.sites = sites;
314     }
315
316 }