13e408ca21d04f969881e8e296e5092e4e1f6849
[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.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;
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 = LogManager.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         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);
91     }
92
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"));
101             }
102         }
103     }
104
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"));
113         }
114     }
115
116     private void checkId(String id) throws ServiceException {
117         SitesEntity site = sitesDao.getSite(id);
118         if(null != site) {
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"));
122         }
123     }
124
125     @Override
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"));
131         }
132         this.checkSite(sitesEntity, TYPE_ADD);
133
134         if(StringUtils.isEmpty(sitesEntity.getId())) {
135             sitesEntity.setId(UUID.randomUUID().toString());
136         }
137         SitesEntity.dataFramat(sitesEntity);
138         return sitesDao.addSiteSelective(sitesEntity);
139     }
140
141     @Override
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"));
147         }
148
149         this.checkSite(sitesEntity, TYPE_UPDATE);
150         SitesEntity.dataFramat(sitesEntity);
151         return sitesDao.updateSiteSelective(sitesEntity);
152     }
153
154     @Override
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"));
160         }
161         this.checkSite(sitesEntity, TYPE_UPDATE);
162         SitesEntity.dataFramat(sitesEntity);
163         return sitesDao.updateSite(sitesEntity);
164     }
165
166     @Override
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"));
172         }
173         SitesEntity.dataFramat(sitesEntity);
174         return sitesDao.updateSiteByVimId(sitesEntity);
175     }
176
177     @Override
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"));
183         }
184         SitesEntity.dataFramat(sitesEntity);
185         return sitesDao.updateSiteSelective(sitesEntity);
186     }
187
188     public void setSitesDao(SitesDao sitesDao) {
189         this.sitesDao = sitesDao;
190     }
191 }