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.business.impl;
19 import java.util.HashMap;
20 import java.util.List;
22 import java.util.UUID;
24 import org.apache.commons.lang.StringUtils;
25 import org.onap.vfc.nfvo.resmanagement.common.ResourceUtil;
26 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
27 import org.onap.vfc.nfvo.resmanagement.common.util.StringUtil;
28 import org.onap.vfc.nfvo.resmanagement.service.business.inf.LocationBusiness;
29 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.LocationDao;
30 import org.onap.vfc.nfvo.resmanagement.service.entity.LocationEntity;
31 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
37 * Location Business implementation class.<br>
42 * @version NFVO 0.5 Sep 10, 2016
44 public class LocationBusinessImpl implements LocationBusiness {
46 private static final Logger LOGGER = LoggerFactory.getLogger(LocationBusinessImpl.class);
48 private LocationDao locationDao;
54 * @param locationEntity
56 * @throws ServiceException
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()) {
72 * Check Latitude.;<br>
74 * @param locationEntity
76 * @throws ServiceException
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");
85 if("-0".equals(locationEntity.getLongitude())) {
86 locationEntity.setLongitude("0");
88 float latitude = Float.parseFloat(latitu);
89 float longitude = Float.parseFloat(longitu);
90 if((latitude >= -90 && latitude <= 90) && (longitude >= -180 && longitude <= 180)) {
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) {
104 String oldCountry = selectLocation.getCountry();
105 String oldLocation = selectLocation.getLocation();
106 if(newCountry.equals(oldCountry) && newLocation.equals(oldLocation)) {
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.");
118 return locationDao.getLocation(id);
122 public List<String> getCountry() throws ServiceException {
123 return locationDao.getCountry();
127 public List<String> getLocationByCountry(Map<String, Object> condition) throws ServiceException {
128 return locationDao.getLocationByCountry(condition);
132 public List<LocationEntity> getLocations(Map<String, Object> condition) throws ServiceException {
133 return locationDao.getLocations(condition);
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"));
143 return locationDao.deleteLocation(location);
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"));
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"));
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"));
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"));
170 if(null == locationEntity.getId() || locationEntity.getId().isEmpty()) {
171 locationEntity.setId(UUID.randomUUID().toString());
173 LOGGER.error("function=addLocation; msg=add DO success, : " + locationEntity);
174 return locationDao.addLocation(locationEntity);
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"));
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"));
192 if(null == locationEntity.getId() || locationEntity.getId().isEmpty()) {
193 locationEntity.setId(UUID.randomUUID().toString());
195 return locationDao.addLocationSelective(locationEntity);
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"));
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"));
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"));
217 return locationDao.updateLocationSelective(locationEntity);
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"));
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"));
232 return locationDao.updateLocation(locationEntity);
235 public LocationDao getLocationDao() {
239 public void setLocationDao(LocationDao locationDao) {
240 this.locationDao = locationDao;