2 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.vfc.nfvo.resmanagement.service.group.impl;
19 import java.util.List;
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;
31 import net.sf.json.JSONArray;
32 import net.sf.json.JSONObject;
40 * @version NFVO 0.5 Oct 28, 2016
42 public class VnfInfoServiceImpl implements VnfInfoService {
44 private static final Logger LOGGER = LoggerFactory.getLogger(VnfInfoServiceImpl.class);
46 private VnfInfoDao vnfInfoDao;
48 private VmService vmService;
55 * @throws ServiceException
59 public JSONObject addVnfInfo(JSONObject object) throws ServiceException {
60 LOGGER.info("function=addVnfInfo; object: {}", 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);
68 if(!checkId(vnfInfoEntity.getVnfInstanceId())) {
69 result = vnfInfoDao.updateVnfInfo(vnfInfoEntity);
71 result = vnfInfoDao.addVnfInfo(vnfInfoEntity);
73 JSONObject resultObj = new JSONObject();
75 resultObj.put("vnfInstanceId", object.get("vnfInstanceId"));
77 LOGGER.error("function=addVnfInfo; msg=add vnfInfo into DB error.");
78 resultObj.put("message", "Add vnfInfo into DB error.");
86 * @param vnfInstanceId
90 private boolean checkId(String vnfInstanceId) {
91 VnfInfoEntity vnf = vnfInfoDao.getVnfInfo(vnfInstanceId);
102 * @throws ServiceException
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));
121 * @throws ServiceException
125 public List<VnfInfoEntity> getList(Map<String, Object> map) throws ServiceException {
126 return vnfInfoDao.getVnfInfos(map);
134 * @throws ServiceException
138 public int delete(String id) throws ServiceException {
139 vmService.deleteByVnfId(id);
140 return vnfInfoDao.deleteVnfInfoById(id);
143 public void setVnfInfoDao(VnfInfoDao vnfInfoDao) {
144 this.vnfInfoDao = vnfInfoDao;
147 public void setVmService(VmService vmService) {
148 this.vmService = vmService;