e66c6f5c74fa0b295d798e76865005e5c4a73c42
[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.onap.vfc.nfvo.resmanagement.service.business.impl;
18
19 import java.util.List;
20 import java.util.Map;
21 import java.util.UUID;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.vfc.nfvo.resmanagement.common.ResourceUtil;
25 import org.onap.vfc.nfvo.resmanagement.service.business.inf.HostBusiness;
26 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.HostDao;
27 import org.onap.vfc.nfvo.resmanagement.service.entity.HostEntity;
28 import org.openo.baseservice.remoteservice.exception.ServiceException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  *
34  * Host business implementation class.<br>
35  * <p>
36  * </p>
37  *
38  * @author
39  * @version     NFVO 0.5  Sep 10, 2016
40  */
41 public class HostBusinessImpl implements HostBusiness {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(HostBusinessImpl.class);
44
45     private HostDao hostDao;
46
47     @Override
48     public HostEntity getHost(String id) {
49         if(StringUtils.isEmpty(id)) {
50             LOGGER.error("function=getHost; msg=get error, because id is empty.");
51             return null;
52         }
53         return hostDao.getHost(id);
54     }
55
56     @Override
57     public List<HostEntity> getHosts(Map<String, Object> condition) {
58         return hostDao.getHosts(condition);
59     }
60
61     @Override
62     public int deleteHost(String id) throws ServiceException {
63         if(StringUtils.isEmpty(id)) {
64             LOGGER.error("function=deleteHost; msg=delete error, because id is empty.");
65             throw new ServiceException(ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.host.delete.id.null"));
66         }
67         HostEntity rp = hostDao.getHost(id);
68         if(null == rp) {
69             return hostDao.deleteHost(id);
70         }
71
72         return hostDao.deleteHost(id);
73     }
74
75     @Override
76     public int addHost(HostEntity hostEntity) throws ServiceException {
77         if(null == hostEntity) {
78             LOGGER.error("function=addHost; msg=add error, because hostEntity is null.");
79             throw new ServiceException(
80                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.host.add.entity.null"));
81         }
82
83         if(!checkId(hostEntity.getId())) {
84             LOGGER.error("function=addHost; msg=add error, because id is already exist.");
85             throw new ServiceException(ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.host.add.id.check"));
86         }
87         if(StringUtils.isEmpty(hostEntity.getId())) {
88             hostEntity.setId(UUID.randomUUID().toString());
89         }
90         return hostDao.addHost(hostEntity);
91     }
92
93     @Override
94     public int addHostSelective(HostEntity hostEntity) throws ServiceException {
95         if(null == hostEntity) {
96             LOGGER.error("function=addHostSelective; msg=add error, because hostEntity is null.");
97             throw new ServiceException(
98                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.host.add.entity.null"));
99         }
100         if(!checkId(hostEntity.getId())) {
101             LOGGER.error("function=addHostSelective; msg=add error, because id is allready exist.");
102             throw new ServiceException(ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.host.add.id.check"));
103         }
104
105         if(StringUtils.isEmpty(hostEntity.getId())) {
106             hostEntity.setId(UUID.randomUUID().toString());
107         }
108         return hostDao.addHostSelective(hostEntity);
109     }
110
111     private boolean checkId(String id) {
112         HostEntity respool = hostDao.getHost(id);
113         if(null == respool) {
114             return true;
115         }
116         return false;
117     }
118
119     @Override
120     public int updateHostSelective(HostEntity hostEntity) throws ServiceException {
121         if(!checkId(hostEntity.getId())) {
122             return hostDao.updateHostSelective(hostEntity);
123         } else {
124             return addHostSelective(hostEntity);
125         }
126     }
127
128     @Override
129     public int updateHost(HostEntity hostEntity) throws ServiceException {
130         if(null == hostEntity) {
131             LOGGER.error("function=updateHost; msg=update error, because hostEntity is null.");
132             throw new ServiceException("update error, because hostEntity is null.");
133         }
134
135         return hostDao.updateHost(hostEntity);
136     }
137
138     @Override
139     public int updateHostByVimId(HostEntity hostEntity) throws ServiceException {
140         if(null == hostEntity) {
141             LOGGER.error("function=updateHostByVimId; msg=update error, because hostEntity is null.");
142             throw new ServiceException(
143                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.host.update.entity.null"));
144         }
145         return hostDao.updateHostByVimId(hostEntity);
146     }
147
148     @Override
149     public int deleteHostByVimId(String vimId) throws ServiceException {
150         if(StringUtils.isEmpty(vimId)) {
151             LOGGER.error("function=deleteHostByVimId; msg=delete error, because VimId is empty.");
152             throw new ServiceException(
153                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.host.delete.vimid.check"));
154         }
155         return hostDao.deleteHostByVimId(vimId);
156     }
157
158     public void setHostDao(HostDao hostDao) {
159         this.hostDao = hostDao;
160     }
161
162 }