3a1a02e761b8ca4f1f5840c51c28686716de7bb8
[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.openo.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.lang3.StringUtils;
24 import org.openo.baseservice.remoteservice.exception.ServiceException;
25 import org.openo.nfvo.resmanagement.common.ResourceUtil;
26 import org.openo.nfvo.resmanagement.service.dao.inf.VnfDao;
27 import org.openo.nfvo.resmanagement.service.entity.VnfEntity;
28 import org.openo.nfvo.resmanagement.service.group.inf.VnfInfoService;
29 import org.openo.nfvo.resmanagement.service.group.inf.VnfService;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 import net.sf.json.JSONObject;
34
35 /**
36  * <br>
37  * <p>
38  * </p>
39  * 
40  * @author
41  * @version NFVO 0.5 Oct 28, 2016
42  */
43 public class VnfServiceImpl implements VnfService {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(VnfServiceImpl.class);
46
47     private VnfDao vnfDao;
48
49     private VnfInfoService vnfInfoService;
50
51     /**
52      * <br>
53      * 
54      * @param vnfEntity
55      * @return
56      * @throws ServiceException
57      * @since NFVO 0.5
58      */
59     @Override
60     public JSONObject addVnf(VnfEntity vnfEntity) throws ServiceException {
61         if(!checkId(vnfEntity.getId())) {
62             LOGGER.error("function=addVnf; msg=add error, because id is already exist.");
63             throw new ServiceException(ResourceUtil
64                     .getMessage("org.openo.nfvo.resmanagement.service.group.impl.VnfServiceImpl.add.id.check"));
65         }
66         if(StringUtils.isEmpty(vnfEntity.getId())) {
67             vnfEntity.setId(UUID.randomUUID().toString());
68         }
69         int result = vnfDao.addVnf(vnfEntity);
70         JSONObject restJson = new JSONObject();
71         if(result > 0) {
72             restJson.put("id", vnfEntity.getId());
73             restJson.put("name", vnfEntity.getName());
74         } else {
75             LOGGER.error("function=addVnf; msg=add vnf into DB error.");
76             restJson.put("message", "Add vnf into DB error.");
77         }
78         return restJson;
79     }
80
81     /**
82      * <br>
83      * 
84      * @param id
85      * @return
86      * @since NFVO 0.5
87      */
88     private boolean checkId(String id) {
89         VnfEntity vnf = vnfDao.getVnf(id);
90         if(null == vnf) {
91             return true;
92         }
93         return false;
94     }
95
96     /**
97      * <br>
98      * 
99      * @param map
100      * @return
101      * @throws ServiceException
102      * @since NFVO 0.5
103      */
104     @Override
105     public List<VnfEntity> getList(Map<String, Object> map) throws ServiceException {
106         return vnfDao.getVnfs(map);
107     }
108
109     /**
110      * <br>
111      * 
112      * @param id
113      * @return
114      * @throws ServiceException
115      * @since NFVO 0.5
116      */
117     @Override
118     public int delete(String id) throws ServiceException {
119         deleteVnfInfo(id);
120         return vnfDao.deleteVnfById(id);
121     }
122
123     /**
124      * <br>
125      * 
126      * @param id
127      * @throws ServiceException
128      * @since NFVO 0.5
129      */
130     private void deleteVnfInfo(String vnfInstanceId) throws ServiceException {
131         vnfInfoService.delete(vnfInstanceId);
132     }
133
134     public void setVnfDao(VnfDao vnfDao) {
135         this.vnfDao = vnfDao;
136     }
137
138     public void setVnfInfoService(VnfInfoService vnfInfoService) {
139         this.vnfInfoService = vnfInfoService;
140     }
141 }