e64e54039930d13d95757aa1c4691b830dd352b8
[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
22 import org.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.VnfStatusDao;
25 import org.onap.vfc.nfvo.resmanagement.service.entity.VnfStatusEntity;
26 import org.onap.vfc.nfvo.resmanagement.service.group.inf.VnfStatusService;
27 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
28
29 import net.sf.json.JSONObject;
30
31 /**
32  * <br>
33  * <p>
34  * </p>
35  * 
36  * @author
37  * @version VFC 1.0 Oct 29, 2016
38  */
39 public class VnfStatusServiceImpl implements VnfStatusService {
40
41     private static final Logger LOGGER = LogManager.getLogger(VnfStatusServiceImpl.class);
42
43     private VnfStatusDao vnfStatusDao;
44
45     /**
46      * <br>
47      * 
48      * @param object
49      * @return
50      * @throws ServiceException
51      * @since VFC 1.0
52      */
53     @Override
54     public JSONObject addVnfStatus(JSONObject object) throws ServiceException {
55         LOGGER.info("function=addVnfStatus; object: {}", object);
56         VnfStatusEntity vnfStatusEntity = VnfStatusEntity.toEntity(object);
57         int result;
58         if(!checkId(vnfStatusEntity.getVnfInstanceId())) {
59             result = vnfStatusDao.updateVnfStatus(vnfStatusEntity);
60         } else {
61             result = vnfStatusDao.addVnfStatus(vnfStatusEntity);
62         }
63         JSONObject resultObj = new JSONObject();
64         if(result > 0) {
65             resultObj.put("vnfInstanceId", object.get("vnfInstanceId"));
66         } else {
67             LOGGER.error("function=addVnfStatus; msg=add vnfStatus into DB error.");
68             resultObj.put("message", "Add vnfStatus into DB error.");
69         }
70         return resultObj;
71     }
72
73     /**
74      * <br>
75      * 
76      * @param vnfInstanceId
77      * @return
78      * @since VFC 1.0
79      */
80     private boolean checkId(String vnfInstanceId) {
81         VnfStatusEntity vnfStatus = vnfStatusDao.getVnfStatus(vnfInstanceId);
82         if(null == vnfStatus) {
83             return true;
84         }
85         return false;
86     }
87
88     /**
89      * <br>
90      * 
91      * @param map
92      * @return
93      * @throws ServiceException
94      * @since VFC 1.0
95      */
96     @Override
97     public List<VnfStatusEntity> getList(Map<String, Object> map) throws ServiceException {
98         return vnfStatusDao.getVnfStatuss(map);
99     }
100
101     /**
102      * <br>
103      * 
104      * @param id
105      * @return
106      * @throws ServiceException
107      * @since VFC 1.0
108      */
109     @Override
110     public int delete(String id) throws ServiceException {
111         return vnfStatusDao.deleteVnfStatusById(id);
112     }
113
114     public void setVnfStatusDao(VnfStatusDao vnfStatusDao) {
115         this.vnfStatusDao = vnfStatusDao;
116     }
117 }