7051fbf1687f91b0a807b9b0bf6177b76b8b2e2b
[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.business.impl;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.UUID;
23
24 import org.apache.commons.lang.StringUtils;
25 import org.apache.logging.log4j.LogManager;
26 import org.apache.logging.log4j.Logger;
27 import org.onap.vfc.nfvo.resmanagement.common.ResourceUtil;
28 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
29 import org.onap.vfc.nfvo.resmanagement.common.util.StringUtil;
30 import org.onap.vfc.nfvo.resmanagement.service.business.inf.LocationBusiness;
31 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.LocationDao;
32 import org.onap.vfc.nfvo.resmanagement.service.entity.LocationEntity;
33 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
34
35 /**
36  *
37  * Location Business implementation class.<br>
38  * <p>
39  * </p>
40  *
41  * @author
42  * @version     VFC 1.0  Sep 10, 2016
43  */
44 public class LocationBusinessImpl implements LocationBusiness {
45
46     private static final Logger LOGGER = LogManager.getLogger(LocationBusinessImpl.class);
47
48     private LocationDao locationDao;
49
50     /**
51      *
52      * Check location.<br>
53      *
54      * @param locationEntity
55      * @return
56      * @throws ServiceException
57      * @since  VFC 1.0
58      */
59     public boolean checkLocation(LocationEntity locationEntity) throws ServiceException {
60         String location = locationEntity.getLocation();
61         Map<String, Object> map = new HashMap<>();
62         map.put(ParamConstant.PARAM_LOCATION, location);
63         List<LocationEntity> locationList = getLocations(map);
64         if(locationList == null || locationList.isEmpty()) {
65             return false;
66         }
67         return true;
68     }
69
70     /**
71      *
72      * Check Latitude.;<br>
73      *
74      * @param locationEntity
75      * @return
76      * @throws ServiceException
77      * @since  VFC 1.0
78      */
79     public boolean checkLatitude(LocationEntity locationEntity) throws ServiceException {
80         String latitu = locationEntity.getLatitude();
81         String longitu = locationEntity.getLongitude();
82         if("-0".equals(locationEntity.getLatitude())) {
83             locationEntity.setLatitude("0");
84         }
85         if("-0".equals(locationEntity.getLongitude())) {
86             locationEntity.setLongitude("0");
87         }
88         float latitude = Float.parseFloat(latitu);
89         float longitude = Float.parseFloat(longitu);
90         if((latitude >= -90 && latitude <= 90) && (longitude >= -180 && longitude <= 180)) {
91             return true;
92         }
93         return false;
94     }
95
96     private boolean checkModified(LocationEntity locationEntity) {
97         String newCountry = locationEntity.getCountry();
98         String newLocation = locationEntity.getLocation();
99         String id = locationEntity.getId();
100         LocationEntity selectLocation = locationDao.getLocation(id);
101         if(null == selectLocation) {
102             return false;
103         }
104         String oldCountry = selectLocation.getCountry();
105         String oldLocation = selectLocation.getLocation();
106         if(newCountry.equals(oldCountry) && newLocation.equals(oldLocation)) {
107             return true;
108         }
109         return false;
110     }
111
112     @Override
113     public LocationEntity getLocation(String id) throws ServiceException {
114         if(StringUtils.isEmpty(id)) {
115             LOGGER.error("function=getLocation; msg=get error, because id is empty.");
116             return null;
117         }
118         return locationDao.getLocation(id);
119     }
120
121     @Override
122     public List<String> getCountry() throws ServiceException {
123         return locationDao.getCountry();
124     }
125
126     @Override
127     public List<String> getLocationByCountry(Map<String, Object> condition) throws ServiceException {
128         return locationDao.getLocationByCountry(condition);
129     }
130
131     @Override
132     public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
133         return locationDao.getLocations(condition);
134     }
135
136     @Override
137     public int deleteLocation(String location) throws ServiceException {
138         if(StringUtils.isEmpty(location)) {
139             LOGGER.error("function=deleteLocation; msg=delete error, because location is empty.");
140             throw new ServiceException(
141                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.delete.base.entity.check"));
142         }
143         return locationDao.deleteLocation(location);
144     }
145
146     @Override
147     public int addLocation(LocationEntity locationEntity) throws ServiceException {
148         if(null == locationEntity) {
149             LOGGER.error("function=addLocation; msg=add error, because locationEntity is null.");
150             throw new ServiceException(
151                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.add.entity.null"));
152         }
153         if(!StringUtil.checkXss(locationEntity.getCountry()) || !StringUtil.checkXss(locationEntity.getLocation())
154                 || !StringUtil.checkXss(locationEntity.getLatitude())
155                 || !StringUtil.checkXss(locationEntity.getLongitude())) {
156             LOGGER.error("function=addLocation; msg=add Location error, because XSS injection.");
157             throw new ServiceException(
158                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.add.xss.check"));
159         }
160         if(checkLocation(locationEntity)) {
161             LOGGER.error("function=addLocation; msg=add Location error, because location exist.");
162             throw new ServiceException(
163                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.add.entity.check"));
164         }
165         if(!checkLatitude(locationEntity)) {
166             LOGGER.error("function=addLocation; msg=add Location error, because latitude or longitude illegal.");
167             throw new ServiceException(
168                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.add.latitude.check"));
169         }
170         if(null == locationEntity.getId() || locationEntity.getId().isEmpty()) {
171             locationEntity.setId(UUID.randomUUID().toString());
172         }
173         LOGGER.error("function=addLocation; msg=add DO success, : " + locationEntity);
174         return locationDao.addLocation(locationEntity);
175
176     }
177
178     @Override
179     public int addLocationSelective(LocationEntity locationEntity) throws ServiceException {
180         if(null == locationEntity) {
181             LOGGER.error("function=addLocationSelective; msg=add error, because locationEntity is null.");
182             throw new ServiceException(
183                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.add.entity.null"));
184         }
185         if(!StringUtil.checkXss(locationEntity.getCountry()) || !StringUtil.checkXss(locationEntity.getLocation())
186                 || !StringUtil.checkXss(locationEntity.getLatitude())
187                 || !StringUtil.checkXss(locationEntity.getLongitude())) {
188             LOGGER.error("function=addLocation; msg=add Location error, because XSS injection.");
189             throw new ServiceException(
190                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.add.xss.check"));
191         }
192         if(null == locationEntity.getId() || locationEntity.getId().isEmpty()) {
193             locationEntity.setId(UUID.randomUUID().toString());
194         }
195         return locationDao.addLocationSelective(locationEntity);
196     }
197
198     @Override
199     public int updateLocationSelective(LocationEntity locationEntity) throws ServiceException {
200         if(null == locationEntity) {
201             LOGGER.error("function=updateLocationSelective; msg=update error, because locationEntity is null.");
202             throw new ServiceException(
203                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.update.entity.check"));
204         }
205         if(!checkLatitude(locationEntity)) {
206             LOGGER.error("function=updateLocationSelective; msg=update Location error, "
207                     + "because latitude or longitude illegal.");
208             throw new ServiceException(
209                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.update.latitude.check"));
210         }
211         if(!checkModified(locationEntity)) {
212             LOGGER.error("function=updateLocationSelective; msg=update Location error, "
213                     + "because country or location be modified.");
214             throw new ServiceException(
215                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.update.modified.check"));
216         }
217         return locationDao.updateLocationSelective(locationEntity);
218     }
219
220     @Override
221     public int updateLocation(LocationEntity locationEntity) throws ServiceException {
222         if(null == locationEntity) {
223             LOGGER.error("function=updateLocation; msg=update error, because locationEntity is null.");
224             throw new ServiceException(
225                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.update.entity.null"));
226         }
227         if(!checkLatitude(locationEntity)) {
228             LOGGER.error("function=updateLocation; msg=update Location error, because latitude or longitude illegal.");
229             throw new ServiceException(
230                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.update.latitude.check"));
231         }
232         return locationDao.updateLocation(locationEntity);
233     }
234
235     public LocationDao getLocationDao() {
236         return locationDao;
237     }
238
239     public void setLocationDao(LocationDao locationDao) {
240         this.locationDao = locationDao;
241     }
242 }