ddac68d6a23eadd32e92b47c2287d23b86d86b17
[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.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
23 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
24 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.VnfInfoDao;
25 import org.onap.vfc.nfvo.resmanagement.service.entity.VmEntity;
26 import org.onap.vfc.nfvo.resmanagement.service.entity.VnfInfoEntity;
27 import org.onap.vfc.nfvo.resmanagement.service.group.inf.VmService;
28 import org.onap.vfc.nfvo.resmanagement.service.group.inf.VnfInfoService;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 import net.sf.json.JSONArray;
33 import net.sf.json.JSONObject;
34
35 /**
36  * <br>
37  * <p>
38  * </p>
39  * 
40  * @author
41  * @version VFC 1.0 Oct 28, 2016
42  */
43 public class VnfInfoServiceImpl implements VnfInfoService {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(VnfInfoServiceImpl.class);
46
47     private VnfInfoDao vnfInfoDao;
48
49     private VmService vmService;
50
51     /**
52      * <br>
53      * 
54      * @param object
55      * @return
56      * @throws ServiceException
57      * @since VFC 1.0
58      */
59     @Override
60     public JSONObject addVnfInfo(JSONObject object) throws ServiceException {
61         LOGGER.info("function=addVnfInfo; object: {}", object);
62         saveVm(object);
63         JSONObject vnf = new JSONObject();
64         vnf.put(ParamConstant.VNF_INSTANCEID, object.get(ParamConstant.VNF_INSTANCEID));
65         vnf.put("nsId", object.get("nsId"));
66         vnf.put("vnfmId", object.get("vnfmId"));
67         VnfInfoEntity vnfInfoEntity = VnfInfoEntity.toEntity(vnf);
68         int result;
69         if(!checkId(vnfInfoEntity.getVnfInstanceId())) {
70             result = vnfInfoDao.updateVnfInfo(vnfInfoEntity);
71         } else {
72             result = vnfInfoDao.addVnfInfo(vnfInfoEntity);
73         }
74         JSONObject resultObj = new JSONObject();
75         if(result > 0) {
76             resultObj.put(ParamConstant.VNF_INSTANCEID, object.get(ParamConstant.VNF_INSTANCEID));
77         } else {
78             LOGGER.error("function=addVnfInfo; msg=add vnfInfo into DB error.");
79             resultObj.put("message", "Add vnfInfo into DB error.");
80         }
81         return resultObj;
82     }
83
84     /**
85      * <br>
86      * 
87      * @param vnfInstanceId
88      * @return
89      * @since VFC 1.0
90      */
91     private boolean checkId(String vnfInstanceId) {
92         VnfInfoEntity vnf = vnfInfoDao.getVnfInfo(vnfInstanceId);
93         if(null == vnf) {
94             return true;
95         }
96         return false;
97     }
98
99     /**
100      * <br>
101      * 
102      * @param object
103      * @throws ServiceException
104      * @since VFC 1.0
105      */
106     private void saveVm(JSONObject object) throws ServiceException {
107         String vnfInstanceId = object.getString(ParamConstant.VNF_INSTANCEID);
108         JSONArray vms = object.getJSONArray("vms");
109         for(int i = 0; i < vms.size(); i++) {
110             JSONObject vmObj = vms.getJSONObject(i);
111             vmObj.put(ParamConstant.VNF_INSTANCEID, vnfInstanceId);
112             vmService.addVm(VmEntity.toEntity(vmObj));
113         }
114
115     }
116
117     /**
118      * <br>
119      * 
120      * @param map
121      * @return
122      * @throws ServiceException
123      * @since VFC 1.0
124      */
125     @Override
126     public List<VnfInfoEntity> getList(Map<String, Object> map) throws ServiceException {
127         return vnfInfoDao.getVnfInfos(map);
128     }
129
130     /**
131      * <br>
132      * 
133      * @param id
134      * @return
135      * @throws ServiceException
136      * @since VFC 1.0
137      */
138     @Override
139     public int delete(String id) throws ServiceException {
140         vmService.deleteByVnfId(id);
141         return vnfInfoDao.deleteVnfInfoById(id);
142     }
143
144     public void setVnfInfoDao(VnfInfoDao vnfInfoDao) {
145         this.vnfInfoDao = vnfInfoDao;
146     }
147
148     public void setVmService(VmService vmService) {
149         this.vmService = vmService;
150     }
151 }