a228114b71c6438522e8c5b09f8e3ef66dfcd994
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 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.util.restclient.ServiceException;
25 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.NsDao;
26 import org.onap.vfc.nfvo.resmanagement.service.entity.NsEntity;
27 import org.onap.vfc.nfvo.resmanagement.service.group.inf.NsService;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import net.sf.json.JSONObject;
32
33 /**
34  * <br>
35  * <p>
36  * </p>
37  *
38  * @author
39  * @version VFC 1.0 Sep 4, 2017
40  */
41 public class NsServiceImpl implements NsService {
42
43     private static final Logger LOGGER = LoggerFactory.getLogger(NsServiceImpl.class);
44
45     private NsDao nsDao;
46
47     @Override
48     public JSONObject addNs(JSONObject object) {
49         NsEntity nsEntity = NsEntity.toEntity(object);
50         JSONObject restJson = new JSONObject();
51
52         if(!checkId(nsEntity.getId())) {
53             LOGGER.error("function=addNs; msg=add error, because id is already exist.");
54             restJson.put("message", "Ns id is already exist.");
55             return restJson;
56         }
57
58         if(StringUtils.isEmpty(nsEntity.getId())) {
59             nsEntity.setId(UUID.randomUUID().toString());
60         }
61         int result = nsDao.addNs(nsEntity);
62
63         if(result > 0) {
64             restJson.put("ns", nsEntity);
65         } else {
66             LOGGER.error("function=addNs; msg=add ns into DB error.");
67             restJson.put("message", "Add ns into DB error.");
68         }
69         return restJson;
70     }
71
72     private boolean checkId(String id) {
73         NsEntity nsEntity = nsDao.getNs(id);
74         if(nsEntity.getId() == null) {
75             return true;
76         }
77         return false;
78     }
79
80     public void setNsDao(NsDao nsDao) {
81         this.nsDao = nsDao;
82     }
83
84     @Override
85     public List<NsEntity> getList(Map<String, Object> map) throws ServiceException {
86         return nsDao.getAllNs(map);
87     }
88
89     @Override
90     public int delete(String id) throws ServiceException {
91         return nsDao.deleteNsById(id);
92     }
93
94 }