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.service.business.inf.SitesBusiness;
28 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.SitesDao;
29 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
30 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
35 * Sites info interface.<br/>
40 * @version VFC 1.0 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";
53 public SitesEntity getSite(String id) throws ServiceException {
54 if(StringUtils.isEmpty(id)) {
55 LOGGER.error("function=getSite; msg=get error, because id is empty.");
56 throw new ServiceException(
57 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.id.null"));
59 return sitesDao.getSite(id);
63 public List<SitesEntity> getSites(Map<String, Object> condition) {
64 return sitesDao.getSites(condition);
68 public int deleteSite(String id) throws ServiceException {
69 if(StringUtils.isEmpty(id)) {
70 LOGGER.error("function=deleteSite; msg=delete error, because id is empty.");
71 throw new ServiceException(
72 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.delete.id.null"));
75 return sitesDao.deleteSite(id);
79 public int addSite(SitesEntity sitesEntity) throws ServiceException {
80 LOGGER.info("addSite sitesEntity");
81 if(null == sitesEntity) {
82 LOGGER.error("function=addSite; msg=add error, because sitesEntity is null.");
83 throw new ServiceException(
84 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.entity.null"));
86 // if(!StringUtil.checkXss(sitesEntity.getName()) ||
87 // !StringUtil.checkXss(sitesEntity.getCountry())
88 // || !StringUtil.checkXss(sitesEntity.getLocation())) {
89 // LOGGER.error("function=addLocation; msg=add site error, because XSS injection.");
90 // throw new ServiceException(
91 // ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.location.add.xss.check"));
93 LOGGER.info("sitesEntity: " + sitesEntity.toString());
94 this.checkSite(sitesEntity, TYPE_ADD);
95 SitesEntity.dataFramat(sitesEntity);
96 LOGGER.info("Add datacenter data into DB.");
97 return sitesDao.addSite(sitesEntity);
100 private void checkSite(SitesEntity sitesEntity, String type) throws ServiceException {
101 if(TYPE_ADD.equals(type)) {
102 checkId(sitesEntity.getId());
103 checkSiteName(sitesEntity.getName());
104 if(!SitesEntity.checkResource(sitesEntity)) {
105 LOGGER.error("function=checkRespool; msg=site{} resource error", sitesEntity.toString());
106 throw new ServiceException(
107 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.resource.check"));
112 private void checkSiteName(String siteName) throws ServiceException {
113 Map<String, Object> siteMap = new HashMap<>(10);
114 siteMap.put(ParamConstant.PARAM_NAME, siteName);
115 List<SitesEntity> siteList = sitesDao.getSites(siteMap);
116 if(null != siteList && !siteList.isEmpty()) {
117 LOGGER.error("function=checkSiteName; msg=site: {} has already exist.", siteName);
118 throw new ServiceException(
119 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.name.check"));
123 private void checkId(String id) throws ServiceException {
124 SitesEntity site = sitesDao.getSite(id);
126 LOGGER.error("function=checkId; msg=add error, because id is already exist.");
127 throw new ServiceException(
128 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.id.check"));
133 public int addSiteSelective(SitesEntity sitesEntity) throws ServiceException {
134 if(null == sitesEntity) {
135 LOGGER.error("function=addSiteSelective; msg=add error, because sitesEntity is null.");
136 throw new ServiceException(
137 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.entity.null"));
139 this.checkSite(sitesEntity, TYPE_ADD);
141 if(StringUtils.isEmpty(sitesEntity.getId())) {
142 sitesEntity.setId(UUID.randomUUID().toString());
144 SitesEntity.dataFramat(sitesEntity);
145 return sitesDao.addSiteSelective(sitesEntity);
149 public int updateSiteSelective(SitesEntity sitesEntity) throws ServiceException {
150 if(null == sitesEntity) {
151 LOGGER.error("function=updateSiteSelective; msg=update error, because sitesEntity is null.");
152 throw new ServiceException(
153 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
156 this.checkSite(sitesEntity, TYPE_UPDATE);
157 SitesEntity.dataFramat(sitesEntity);
158 return sitesDao.updateSiteSelective(sitesEntity);
162 public int updateSite(SitesEntity sitesEntity) throws ServiceException {
163 if(null == sitesEntity) {
164 LOGGER.error("function=updateSite; msg=update error, because sitesEntity is null.");
165 throw new ServiceException(
166 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
168 this.checkSite(sitesEntity, TYPE_UPDATE);
169 SitesEntity.dataFramat(sitesEntity);
170 return sitesDao.updateSite(sitesEntity);
174 public int updateSiteByVimId(SitesEntity sitesEntity) throws ServiceException {
175 if(null == sitesEntity) {
176 LOGGER.error("function=updateSiteByVimId; msg=update error, because sitesEntity is null.");
177 throw new ServiceException(
178 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
180 SitesEntity.dataFramat(sitesEntity);
181 return sitesDao.updateSiteByVimId(sitesEntity);
185 public int updateSiteResource(SitesEntity sitesEntity) throws ServiceException {
186 if(null == sitesEntity) {
187 LOGGER.error("function=updateSiteResource; msg=update error, because sitesEntity is null.");
188 throw new ServiceException(
189 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
191 SitesEntity.dataFramat(sitesEntity);
192 return sitesDao.updateSiteSelective(sitesEntity);
195 public void setSitesDao(SitesDao sitesDao) {
196 this.sitesDao = sitesDao;