2 * Copyright 2016 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.openo.nfvo.resmanagement.service.group.impl;
19 import java.util.HashMap;
20 import java.util.List;
22 import java.util.UUID;
24 import org.apache.commons.lang3.StringUtils;
25 import org.openo.baseservice.remoteservice.exception.ServiceException;
26 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
27 import org.openo.nfvo.resmanagement.common.constant.ParamConstant;
28 import org.openo.nfvo.resmanagement.common.constant.UrlConstant;
29 import org.openo.nfvo.resmanagement.common.util.RestfulUtil;
30 import org.openo.nfvo.resmanagement.service.dao.inf.VmDao;
31 import org.openo.nfvo.resmanagement.service.entity.VmEntity;
32 import org.openo.nfvo.resmanagement.service.group.inf.VmService;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 import net.sf.json.JSONArray;
37 import net.sf.json.JSONObject;
45 * @version NFVO 0.5 Oct 29, 2016
47 public class VmServiceImpl implements VmService {
49 private static final Logger LOGGER = LoggerFactory.getLogger(VmServiceImpl.class);
58 * @throws ServiceException
62 public JSONObject addVm(VmEntity vmEntity) throws ServiceException {
64 if(!checkId(vmEntity.getVmId())) {
65 result = vmDao.updateVm(vmEntity);
66 sendMsgMonitor("create", vmEntity);
68 if(StringUtils.isEmpty(vmEntity.getVmId())) {
69 vmEntity.setVmId(UUID.randomUUID().toString());
71 result = vmDao.addVm(vmEntity);
72 sendMsgMonitor("update", vmEntity);
74 JSONObject restJson = new JSONObject();
76 restJson.put("id", vmEntity.getVmId());
77 restJson.put("name", vmEntity.getVmName());
79 LOGGER.error("function=addVm; msg=add vm into DB error.");
80 restJson.put("message", "Add vm into DB error.");
92 private boolean checkId(String vmId) {
93 VmEntity vm = vmDao.getVm(vmId);
100 public void sendMsgMonitor(String operateType, VmEntity vmEntity) throws ServiceException {
101 JSONObject msgObj = new JSONObject();
102 msgObj.put("operationType", operateType);
103 msgObj.put("resourceType", "VDU");
104 msgObj.put("label", vmEntity.getVmName());
105 if("delete".equals(operateType)) {
106 JSONArray deleteIds = new JSONArray();
107 deleteIds.add(vmEntity.getVmId());
108 msgObj.put("deleteIds", deleteIds);
110 JSONArray data = new JSONArray();
111 JSONObject obj = JSONObject.fromObject(vmEntity);
112 obj.put("oid", vmEntity.getVmId());
113 obj.put("moc", "nfv.vdu.linux");
115 msgObj.put("data", data);
117 LOGGER.info("sendMsgMonitor msgObj: {}", msgObj);
118 RestfulParametes restfulParametes = new RestfulParametes();
119 Map<String, String> headerMap = new HashMap<>(3);
120 headerMap.put("Content-Type", "application/json");
121 restfulParametes.setHeaderMap(headerMap);
122 restfulParametes.setRawData(msgObj.toString());
123 String result = RestfulUtil.getResponseContent(UrlConstant.SEND_MSG_MONITOR, restfulParametes,
124 ParamConstant.PARAM_POST);
134 * @throws ServiceException
138 public List<VmEntity> getList(Map<String, Object> map) throws ServiceException {
139 return vmDao.getVms(map);
147 * @throws ServiceException
151 public int delete(String id) throws ServiceException {
152 return vmDao.deleteVmById(id);
155 public void setVmDao(VmDao vmDao) {
162 * @param vnfInstanceId
163 * @throws ServiceException
167 public int deleteByVnfId(String vnfInstanceId) throws ServiceException {
168 Map<String, Object> map = new HashMap<>(10);
169 map.put("vnfInstanceId", vnfInstanceId);
170 List<VmEntity> vms = vmDao.getVms(map);
171 for(int i = 0; i < vms.size(); i++) {
172 VmEntity vm = vms.get(i);
173 sendMsgMonitor("delete", vm);
175 return vmDao.deleteVmByVnfId(vnfInstanceId);