838a45c3b211d581e345892bc6f29fab6cebfd90
[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.group.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.onap.vfc.nfvo.resmanagement.common.ResourceUtil;
25 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.VirtualLinkDao;
26 import org.onap.vfc.nfvo.resmanagement.service.entity.VirtualLinkEntity;
27 import org.onap.vfc.nfvo.resmanagement.service.group.inf.VirtualLinkService;
28 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import net.sf.json.JSONObject;
33
34 /**
35  * <br>
36  * <p>
37  * </p>
38  * 
39  * @author
40  * @version VFC 1.0 Oct 27, 2016
41  */
42 public class VirtualLinkServiceImpl implements VirtualLinkService {
43
44     private static final Logger LOGGER = LoggerFactory.getLogger(VirtualLinkServiceImpl.class);
45
46     private VirtualLinkDao virtualLinkDao;
47
48     /**
49      * <br>
50      * 
51      * @param virtualLinkEntity
52      * @return
53      * @throws ServiceException
54      * @since VFC 1.0
55      */
56     @Override
57     public JSONObject addVl(VirtualLinkEntity virtualLinkEntity) throws ServiceException {
58         if(!checkId(virtualLinkEntity.getId())) {
59             LOGGER.error("function=addVl; msg=add error, because id is already exist.");
60             throw new ServiceException(ResourceUtil
61                     .getMessage("org.onap.vfc.nfvo.resmanagement.service.group.impl.VirtualLinkServiceImpl.add.id.check"));
62         }
63         if(StringUtils.isEmpty(virtualLinkEntity.getId())) {
64             virtualLinkEntity.setId(UUID.randomUUID().toString());
65         }
66         int result = virtualLinkDao.addVl(virtualLinkEntity);
67         JSONObject restJson = new JSONObject();
68         if(result > 0) {
69             restJson.put("id", virtualLinkEntity.getId());
70             restJson.put("name", virtualLinkEntity.getName());
71         } else {
72             LOGGER.error("function=addVl; msg=add vl into DB error.");
73             restJson.put("message", "Add Vl into DB error.");
74         }
75         return restJson;
76
77     }
78
79     /**
80      * <br>
81      * 
82      * @param id
83      * @return
84      * @since VFC 1.0
85      */
86     private boolean checkId(String id) {
87         VirtualLinkEntity vl = virtualLinkDao.getVl(id);
88         if(null == vl) {
89             return true;
90         }
91         return false;
92     }
93
94     /**
95      * <br>
96      * 
97      * @param map
98      * @return
99      * @throws ServiceException
100      * @since VFC 1.0
101      */
102     @Override
103     public List<VirtualLinkEntity> getList(Map<String, Object> map) throws ServiceException {
104         return virtualLinkDao.getVls(map);
105     }
106
107     /**
108      * <br>
109      * 
110      * @param id
111      * @return
112      * @throws ServiceException
113      * @since VFC 1.0
114      */
115     @Override
116     public int delete(String id) throws ServiceException {
117         return virtualLinkDao.deleteVlById(id);
118     }
119
120     public void setVirtualLinkDao(VirtualLinkDao virtualLinkDao) {
121         this.virtualLinkDao = virtualLinkDao;
122     }
123 }