6ffb3fc2141ac42cf5c12b5bffcb01e50753e12b
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 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.openo.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.lang3.StringUtils;
25 import org.openo.baseservice.remoteservice.exception.ServiceException;
26 import org.openo.nfvo.resmanagement.common.ResourceUtil;
27 import org.openo.nfvo.resmanagement.common.constant.ParamConstant;
28 import org.openo.nfvo.resmanagement.service.business.inf.SitesBusiness;
29 import org.openo.nfvo.resmanagement.service.dao.inf.SitesDao;
30 import org.openo.nfvo.resmanagement.service.entity.SitesEntity;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 /**
35  * Sites info interface.<br/>
36  * <p>
37  * </p>
38  *
39  * @author
40  * @version NFVO 0.5 Aug 24, 2016
41  */
42 public class SitesBusinessImpl implements SitesBusiness {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(SitesBusinessImpl.class);
45
46     private SitesDao sitesDao;
47
48     private static final String TYPE_ADD = "add";
49
50     private static final String TYPE_UPDATE = "update";
51
52     private static final String TYPE_DELETE = "delete";
53
54     @Override
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"));
60         }
61         return sitesDao.getSite(id);
62     }
63
64     @Override
65     public List<SitesEntity> getSites(Map<String, Object> condition) {
66         return sitesDao.getSites(condition);
67     }
68
69     @Override
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"));
75         }
76
77         return sitesDao.deleteSite(id);
78     }
79
80     @Override
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"));
87         }
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"));
94         // }
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);
100     }
101
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"));
110             }
111         }
112     }
113
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"));
122         }
123     }
124
125     private void checkId(String id) throws ServiceException {
126         SitesEntity site = sitesDao.getSite(id);
127         if(null != site) {
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"));
131         }
132     }
133
134     @Override
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"));
140         }
141         this.checkSite(sitesEntity, TYPE_ADD);
142
143         if(StringUtils.isEmpty(sitesEntity.getId())) {
144             sitesEntity.setId(UUID.randomUUID().toString());
145         }
146         SitesEntity.dataFramat(sitesEntity);
147         return sitesDao.addSiteSelective(sitesEntity);
148     }
149
150     @Override
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"));
156         }
157
158         this.checkSite(sitesEntity, TYPE_UPDATE);
159         SitesEntity.dataFramat(sitesEntity);
160         return sitesDao.updateSiteSelective(sitesEntity);
161     }
162
163     @Override
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"));
169         }
170         this.checkSite(sitesEntity, TYPE_UPDATE);
171         SitesEntity.dataFramat(sitesEntity);
172         return sitesDao.updateSite(sitesEntity);
173     }
174
175     @Override
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"));
181         }
182         SitesEntity.dataFramat(sitesEntity);
183         return sitesDao.updateSiteByVimId(sitesEntity);
184     }
185
186     @Override
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"));
192         }
193         SitesEntity.dataFramat(sitesEntity);
194         return sitesDao.updateSiteSelective(sitesEntity);
195     }
196
197     public void setSitesDao(SitesDao sitesDao) {
198         this.sitesDao = sitesDao;
199     }
200 }