2 * Copyright 2016 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.res.service.business.impl;
19 import java.util.HashMap;
20 import java.util.List;
22 import java.util.UUID;
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.vfc.nfvo.res.common.ResourceUtil;
26 import org.onap.vfc.nfvo.res.common.constant.ParamConstant;
27 import org.onap.vfc.nfvo.res.service.business.inf.SitesBusiness;
28 import org.onap.vfc.nfvo.res.service.dao.inf.SitesDao;
29 import org.onap.vfc.nfvo.res.service.entity.SitesEntity;
30 import org.openo.baseservice.remoteservice.exception.ServiceException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Sites info interface.<br/>
40 * @version NFVO 0.5 Aug 24, 2016
42 public class SitesBusinessImpl implements SitesBusiness {
44 private static final Logger LOGGER = LoggerFactory.getLogger(SitesBusinessImpl.class);
46 private SitesDao sitesDao;
48 private static final String TYPE_ADD = "add";
50 private static final String TYPE_UPDATE = "update";
52 private static final String TYPE_DELETE = "delete";
55 public SitesEntity getSite(String id) throws ServiceException {
56 if(StringUtils.isEmpty(id)) {
57 LOGGER.error("function=getSite; msg=get error, because id is empty.");
58 throw new ServiceException(
59 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.id.null"));
61 return sitesDao.getSite(id);
65 public List<SitesEntity> getSites(Map<String, Object> condition) {
66 return sitesDao.getSites(condition);
70 public int deleteSite(String id) throws ServiceException {
71 if(StringUtils.isEmpty(id)) {
72 LOGGER.error("function=deleteSite; msg=delete error, because id is empty.");
73 throw new ServiceException(
74 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.delete.id.null"));
77 return sitesDao.deleteSite(id);
81 public int addSite(SitesEntity sitesEntity) throws ServiceException {
82 LOGGER.info("addSite sitesEntity");
83 if(null == sitesEntity) {
84 LOGGER.error("function=addSite; msg=add error, because sitesEntity is null.");
85 throw new ServiceException(
86 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.entity.null"));
88 // if(!StringUtil.checkXss(sitesEntity.getName()) ||
89 // !StringUtil.checkXss(sitesEntity.getCountry())
90 // || !StringUtil.checkXss(sitesEntity.getLocation())) {
91 // LOGGER.error("function=addLocation; msg=add site error, because XSS injection.");
92 // throw new ServiceException(
93 // ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.add.xss.check"));
95 LOGGER.info("sitesEntity: " + sitesEntity.toString());
96 this.checkSite(sitesEntity, TYPE_ADD);
97 SitesEntity.dataFramat(sitesEntity);
98 LOGGER.info("Add datacenter data into DB.");
99 return sitesDao.addSite(sitesEntity);
102 private void checkSite(SitesEntity sitesEntity, String type) throws ServiceException {
103 if(TYPE_ADD.equals(type)) {
104 checkId(sitesEntity.getId());
105 checkSiteName(sitesEntity.getName());
106 if(!SitesEntity.checkResource(sitesEntity)) {
107 LOGGER.error("function=checkRespool; msg=site{} resource error", sitesEntity.toString());
108 throw new ServiceException(
109 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.resource.check"));
114 private void checkSiteName(String siteName) throws ServiceException {
115 Map<String, Object> siteMap = new HashMap<>(10);
116 siteMap.put(ParamConstant.PARAM_NAME, siteName);
117 List<SitesEntity> siteList = sitesDao.getSites(siteMap);
118 if(null != siteList && !siteList.isEmpty()) {
119 LOGGER.error("function=checkSiteName; msg=site: {} has already exist.", siteName);
120 throw new ServiceException(
121 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.name.check"));
125 private void checkId(String id) throws ServiceException {
126 SitesEntity site = sitesDao.getSite(id);
128 LOGGER.error("function=checkId; msg=add error, because id is already exist.");
129 throw new ServiceException(
130 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.id.check"));
135 public int addSiteSelective(SitesEntity sitesEntity) throws ServiceException {
136 if(null == sitesEntity) {
137 LOGGER.error("function=addSiteSelective; msg=add error, because sitesEntity is null.");
138 throw new ServiceException(
139 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.entity.null"));
141 this.checkSite(sitesEntity, TYPE_ADD);
143 if(StringUtils.isEmpty(sitesEntity.getId())) {
144 sitesEntity.setId(UUID.randomUUID().toString());
146 SitesEntity.dataFramat(sitesEntity);
147 return sitesDao.addSiteSelective(sitesEntity);
151 public int updateSiteSelective(SitesEntity sitesEntity) throws ServiceException {
152 if(null == sitesEntity) {
153 LOGGER.error("function=updateSiteSelective; msg=update error, because sitesEntity is null.");
154 throw new ServiceException(
155 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
158 this.checkSite(sitesEntity, TYPE_UPDATE);
159 SitesEntity.dataFramat(sitesEntity);
160 return sitesDao.updateSiteSelective(sitesEntity);
164 public int updateSite(SitesEntity sitesEntity) throws ServiceException {
165 if(null == sitesEntity) {
166 LOGGER.error("function=updateSite; msg=update error, because sitesEntity is null.");
167 throw new ServiceException(
168 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
170 this.checkSite(sitesEntity, TYPE_UPDATE);
171 SitesEntity.dataFramat(sitesEntity);
172 return sitesDao.updateSite(sitesEntity);
176 public int updateSiteByVimId(SitesEntity sitesEntity) throws ServiceException {
177 if(null == sitesEntity) {
178 LOGGER.error("function=updateSiteByVimId; msg=update error, because sitesEntity is null.");
179 throw new ServiceException(
180 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
182 SitesEntity.dataFramat(sitesEntity);
183 return sitesDao.updateSiteByVimId(sitesEntity);
187 public int updateSiteResource(SitesEntity sitesEntity) throws ServiceException {
188 if(null == sitesEntity) {
189 LOGGER.error("function=updateSiteResource; msg=update error, because sitesEntity is null.");
190 throw new ServiceException(
191 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
193 SitesEntity.dataFramat(sitesEntity);
194 return sitesDao.updateSiteSelective(sitesEntity);
197 public void setSitesDao(SitesDao sitesDao) {
198 this.sitesDao = sitesDao;