81e79c8c905fdfaad5a2ddd324e91922e048419d
[vfc/nfvo/resmanagement.git] /
1 /*\r
2  * Copyright 2017 Huawei Technologies Co., Ltd.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.onap.vfc.nfvo.resmanagement.service.group.impl;\r
18 \r
19 import java.util.List;\r
20 import java.util.Map;\r
21 import java.util.UUID;\r
22 \r
23 import org.apache.commons.lang3.StringUtils;\r
24 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.NsDao;\r
25 import org.onap.vfc.nfvo.resmanagement.service.entity.NsEntity;\r
26 import org.onap.vfc.nfvo.resmanagement.service.group.inf.NsService;\r
27 import org.openo.baseservice.remoteservice.exception.ServiceException;\r
28 import org.slf4j.Logger;\r
29 import org.slf4j.LoggerFactory;\r
30 \r
31 import net.sf.json.JSONObject;\r
32 \r
33 /**\r
34  * <br>\r
35  * <p>\r
36  * </p>\r
37  * \r
38  * @author\r
39  * @version VFC 1.0 Sep 4, 2017\r
40  */\r
41 public class NsServiceImpl implements NsService {\r
42 \r
43     private static final Logger LOGGER = LoggerFactory.getLogger(NsServiceImpl.class);\r
44 \r
45     private NsDao nsDao;\r
46 \r
47     @Override\r
48     public JSONObject addNs(JSONObject object) {\r
49         NsEntity nsEntity = NsEntity.toEntity(object);\r
50         JSONObject restJson = new JSONObject();\r
51 \r
52         if(!checkId(nsEntity.getId())) {\r
53             LOGGER.error("function=addVnf; msg=add error, because id is already exist.");\r
54             restJson.put("message", "Ns id is already exist.");\r
55             return restJson;\r
56         }\r
57 \r
58         if(StringUtils.isEmpty(nsEntity.getId())) {\r
59             nsEntity.setId(UUID.randomUUID().toString());\r
60         }\r
61         int result = nsDao.addNs(nsEntity);\r
62 \r
63         if(result > 0) {\r
64             restJson.put("id", nsEntity.getId());\r
65             restJson.put("name", nsEntity.getName());\r
66         } else {\r
67             LOGGER.error("function=addNs; msg=add ns into DB error.");\r
68             restJson.put("message", "Add ns into DB error.");\r
69         }\r
70         return restJson;\r
71     }\r
72 \r
73     private boolean checkId(String id) {\r
74         NsEntity nsEntity = nsDao.getNs(id);\r
75         if(null == nsEntity) {\r
76             return true;\r
77         }\r
78         return false;\r
79     }\r
80 \r
81     public void setNsDao(NsDao nsDao) {\r
82         this.nsDao = nsDao;\r
83     }\r
84 \r
85     @Override\r
86     public List<NsEntity> getList(Map<String, Object> map) throws ServiceException {\r
87         return nsDao.getAllNs(map);\r
88     }\r
89 \r
90     @Override\r
91     public int delete(String id) throws ServiceException {\r
92         return nsDao.deleteNsById(id);\r
93     }\r
94 \r
95 }\r