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