3a99e23e8bcbeca0a2def7cf18b064cd75a2964a
[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.List;
20 import java.util.Map;
21 import java.util.UUID;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.onap.vfc.nfvo.resmanagement.common.ResourceUtil;
27 import org.onap.vfc.nfvo.resmanagement.service.business.inf.PortBusiness;
28 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.PortDao;
29 import org.onap.vfc.nfvo.resmanagement.service.entity.PortEntity;
30 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
31
32 /**
33  * Port business implementation class.<br>
34  * <p>
35  * </p>
36  *
37  * @author
38  * @version VFC 1.0 Sep 10, 2016
39  */
40 public class PortBusinessImpl implements PortBusiness {
41
42     private static final Logger LOGGER = LogManager.getLogger(PortBusinessImpl.class);
43
44     private PortDao portDao;
45
46     @Override
47     public PortEntity getPort(String id) {
48         if(StringUtils.isEmpty(id)) {
49             LOGGER.error("function=getPort; msg=get error, because id is empty.");
50             return null;
51         }
52         return portDao.getPort(id);
53     }
54
55     @Override
56     public List<PortEntity> getPorts(Map<String, Object> condition) {
57         return portDao.getPorts(condition);
58     }
59
60     @Override
61     public int deletePort(String id) throws ServiceException {
62         if(StringUtils.isEmpty(id)) {
63             LOGGER.error("function=deletePort; msg=delete error, because id is empty.");
64             throw new ServiceException(ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.port.delete.id.null"));
65         }
66         PortEntity rp = portDao.getPort(id);
67         if(null == rp) {
68             return portDao.deletePort(id);
69         }
70
71         return portDao.deletePort(id);
72     }
73
74     @Override
75     public int addPort(PortEntity portEntity) throws ServiceException {
76         if(null == portEntity) {
77             LOGGER.error("function=addPort; msg=add error, because portEntity is null.");
78             throw new ServiceException(
79                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.port.add.entity.null"));
80         }
81
82         if(!checkId(portEntity.getId())) {
83             return portDao.updatePortSelective(portEntity);
84         }
85         if(StringUtils.isEmpty(portEntity.getId())) {
86             portEntity.setId(UUID.randomUUID().toString());
87         }
88         return portDao.addPort(portEntity);
89     }
90
91     @Override
92     public int addPortSelective(PortEntity portEntity) throws ServiceException {
93         if(null == portEntity) {
94             LOGGER.error("function=addPortSelective; msg=add error, because portEntity is null.");
95             throw new ServiceException(
96                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.port.add.entity.null"));
97         }
98         if(!checkId(portEntity.getId())) {
99             return portDao.updatePortSelective(portEntity);
100         }
101
102         if(StringUtils.isEmpty(portEntity.getId())) {
103             portEntity.setId(UUID.randomUUID().toString());
104         }
105         return portDao.addPortSelective(portEntity);
106     }
107
108     private boolean checkId(String id) {
109         PortEntity respool = portDao.getPort(id);
110         if(null == respool) {
111             return true;
112         }
113         return false;
114     }
115
116     @Override
117     public int updatePortSelective(PortEntity portEntity) throws ServiceException {
118         if(null == portEntity) {
119             LOGGER.error("function=updatePortSelective; msg=update error, because portEntity is null.");
120             throw new ServiceException(
121                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.port.update.entity.null"));
122         }
123
124         return portDao.updatePortSelective(portEntity);
125     }
126
127     @Override
128     public int updatePort(PortEntity portEntity) throws ServiceException {
129         if(null == portEntity) {
130             LOGGER.error("function=updatePort; msg=update error, because portEntity is null.");
131             throw new ServiceException("update error, because portEntity is null.");
132         }
133
134         return portDao.updatePort(portEntity);
135     }
136
137     @Override
138     public int updatePortByVimId(PortEntity portEntity) throws ServiceException {
139         if(null == portEntity) {
140             LOGGER.error("function=updatePortByVimId; msg=update error, because portEntity is null.");
141             throw new ServiceException(
142                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.port.update.entity.null"));
143         }
144         return portDao.updatePortByVimId(portEntity);
145     }
146
147     @Override
148     public int deletePortByVimId(String vimId) throws ServiceException {
149         if(StringUtils.isEmpty(vimId)) {
150             LOGGER.error("function=deletePortByVimId; msg=delete error, because VimId is empty.");
151             throw new ServiceException(
152                     ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.port.delete.vimid.check"));
153         }
154         return portDao.deletePortByVimId(vimId);
155     }
156
157     public void setPortDao(PortDao portDao) {
158         this.portDao = portDao;
159     }
160
161 }