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.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.service.business.inf.SitesBusiness;
30 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.SitesDao;
31 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
32 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
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 = LogManager.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 LOGGER.info("sitesEntity: " + sitesEntity.toString());
87 this.checkSite(sitesEntity, TYPE_ADD);
88 SitesEntity.dataFramat(sitesEntity);
89 LOGGER.info("Add datacenter data into DB.");
90 return sitesDao.addSite(sitesEntity);
93 private void checkSite(SitesEntity sitesEntity, String type) throws ServiceException {
94 if(TYPE_ADD.equals(type)) {
95 checkId(sitesEntity.getId());
96 checkSiteName(sitesEntity.getName());
97 if(!SitesEntity.checkResource(sitesEntity)) {
98 LOGGER.error("function=checkRespool; msg=site{} resource error", sitesEntity.toString());
99 throw new ServiceException(
100 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.resource.check"));
105 private void checkSiteName(String siteName) throws ServiceException {
106 Map<String, Object> siteMap = new HashMap<>(10);
107 siteMap.put(ParamConstant.PARAM_NAME, siteName);
108 List<SitesEntity> siteList = sitesDao.getSites(siteMap);
109 if(null != siteList && !siteList.isEmpty()) {
110 LOGGER.error("function=checkSiteName; msg=site: {} has already exist.", siteName);
111 throw new ServiceException(
112 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.name.check"));
116 private void checkId(String id) throws ServiceException {
117 SitesEntity site = sitesDao.getSite(id);
119 LOGGER.error("function=checkId; msg=add error, because id is already exist.");
120 throw new ServiceException(
121 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.id.check"));
126 public int addSiteSelective(SitesEntity sitesEntity) throws ServiceException {
127 if(null == sitesEntity) {
128 LOGGER.error("function=addSiteSelective; msg=add error, because sitesEntity is null.");
129 throw new ServiceException(
130 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.add.entity.null"));
132 this.checkSite(sitesEntity, TYPE_ADD);
134 if(StringUtils.isEmpty(sitesEntity.getId())) {
135 sitesEntity.setId(UUID.randomUUID().toString());
137 SitesEntity.dataFramat(sitesEntity);
138 return sitesDao.addSiteSelective(sitesEntity);
142 public int updateSiteSelective(SitesEntity sitesEntity) throws ServiceException {
143 if(null == sitesEntity) {
144 LOGGER.error("function=updateSiteSelective; msg=update error, because sitesEntity is null.");
145 throw new ServiceException(
146 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
149 this.checkSite(sitesEntity, TYPE_UPDATE);
150 SitesEntity.dataFramat(sitesEntity);
151 return sitesDao.updateSiteSelective(sitesEntity);
155 public int updateSite(SitesEntity sitesEntity) throws ServiceException {
156 if(null == sitesEntity) {
157 LOGGER.error("function=updateSite; msg=update error, because sitesEntity is null.");
158 throw new ServiceException(
159 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
161 this.checkSite(sitesEntity, TYPE_UPDATE);
162 SitesEntity.dataFramat(sitesEntity);
163 return sitesDao.updateSite(sitesEntity);
167 public int updateSiteByVimId(SitesEntity sitesEntity) throws ServiceException {
168 if(null == sitesEntity) {
169 LOGGER.error("function=updateSiteByVimId; msg=update error, because sitesEntity is null.");
170 throw new ServiceException(
171 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
173 SitesEntity.dataFramat(sitesEntity);
174 return sitesDao.updateSiteByVimId(sitesEntity);
178 public int updateSiteResource(SitesEntity sitesEntity) throws ServiceException {
179 if(null == sitesEntity) {
180 LOGGER.error("function=updateSiteResource; msg=update error, because sitesEntity is null.");
181 throw new ServiceException(
182 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.base.site.update.entity.null"));
184 SitesEntity.dataFramat(sitesEntity);
185 return sitesDao.updateSiteSelective(sitesEntity);
188 public void setSitesDao(SitesDao sitesDao) {
189 this.sitesDao = sitesDao;