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.apache.logging.log4j.LogManager;
23 import org.apache.logging.log4j.Logger;
24 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
25 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
26 import org.onap.vfc.nfvo.resmanagement.service.dao.inf.VnfInfoDao;
27 import org.onap.vfc.nfvo.resmanagement.service.entity.VmEntity;
28 import org.onap.vfc.nfvo.resmanagement.service.entity.VnfInfoEntity;
29 import org.onap.vfc.nfvo.resmanagement.service.group.inf.VmService;
30 import org.onap.vfc.nfvo.resmanagement.service.group.inf.VnfInfoService;
32 import net.sf.json.JSONArray;
33 import net.sf.json.JSONObject;
41 * @version VFC 1.0 Oct 28, 2016
43 public class VnfInfoServiceImpl implements VnfInfoService {
45 private static final Logger LOGGER = LogManager.getLogger(VnfInfoServiceImpl.class);
47 private VnfInfoDao vnfInfoDao;
49 private VmService vmService;
56 * @throws ServiceException
60 public JSONObject addVnfInfo(JSONObject object) throws ServiceException {
61 LOGGER.info("function=addVnfInfo; object: {}", 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);
69 if(!checkId(vnfInfoEntity.getVnfInstanceId())) {
70 result = vnfInfoDao.updateVnfInfo(vnfInfoEntity);
72 result = vnfInfoDao.addVnfInfo(vnfInfoEntity);
74 JSONObject resultObj = new JSONObject();
76 resultObj.put(ParamConstant.VNF_INSTANCEID, object.get(ParamConstant.VNF_INSTANCEID));
78 LOGGER.error("function=addVnfInfo; msg=add vnfInfo into DB error.");
79 resultObj.put("message", "Add vnfInfo into DB error.");
87 * @param vnfInstanceId
91 private boolean checkId(String vnfInstanceId) {
92 VnfInfoEntity vnf = vnfInfoDao.getVnfInfo(vnfInstanceId);
103 * @throws ServiceException
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));
122 * @throws ServiceException
126 public List<VnfInfoEntity> getList(Map<String, Object> map) throws ServiceException {
127 return vnfInfoDao.getVnfInfos(map);
135 * @throws ServiceException
139 public int delete(String id) throws ServiceException {
140 vmService.deleteByVnfId(id);
141 return vnfInfoDao.deleteVnfInfoById(id);
144 public void setVnfInfoDao(VnfInfoDao vnfInfoDao) {
145 this.vnfInfoDao = vnfInfoDao;
148 public void setVmService(VmService vmService) {
149 this.vmService = vmService;