4fe206ec4040d4437bca80e67b91fbb3debf4e04
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016-2017 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.onap.vfc.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.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;
33
34 /**
35  * Sites info interface.<br/>
36  * <p>
37  * </p>
38  *
39  * @author
40  * @version VFC 1.0 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     @Override
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"));
58         }
59         return sitesDao.getSite(id);
60     }
61
62     @Override
63     public List<SitesEntity> getSites(Map<String, Object> condition) {
64         return sitesDao.getSites(condition);
65     }
66
67     @Override
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"));
73         }
74
75         return sitesDao.deleteSite(id);
76     }
77
78     @Override
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"));
85         }
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"));
92         // }
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);
98     }
99
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"));
108             }
109         }
110     }
111
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"));
120         }
121     }
122
123     private void checkId(String id) throws ServiceException {
124         SitesEntity site = sitesDao.getSite(id);
125         if(null != site) {
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"));
129         }
130     }
131
132     @Override
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"));
138         }
139         this.checkSite(sitesEntity, TYPE_ADD);
140
141         if(StringUtils.isEmpty(sitesEntity.getId())) {
142             sitesEntity.setId(UUID.randomUUID().toString());
143         }
144         SitesEntity.dataFramat(sitesEntity);
145         return sitesDao.addSiteSelective(sitesEntity);
146     }
147
148     @Override
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"));
154         }
155
156         this.checkSite(sitesEntity, TYPE_UPDATE);
157         SitesEntity.dataFramat(sitesEntity);
158         return sitesDao.updateSiteSelective(sitesEntity);
159     }
160
161     @Override
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"));
167         }
168         this.checkSite(sitesEntity, TYPE_UPDATE);
169         SitesEntity.dataFramat(sitesEntity);
170         return sitesDao.updateSite(sitesEntity);
171     }
172
173     @Override
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"));
179         }
180         SitesEntity.dataFramat(sitesEntity);
181         return sitesDao.updateSiteByVimId(sitesEntity);
182     }
183
184     @Override
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"));
190         }
191         SitesEntity.dataFramat(sitesEntity);
192         return sitesDao.updateSiteSelective(sitesEntity);
193     }
194
195     public void setSitesDao(SitesDao sitesDao) {
196         this.sitesDao = sitesDao;
197     }
198 }