fef97b2ec631acd406b7122362728adee64ff9de
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 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.res.service.group.impl;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.UUID;
23
24 import org.apache.commons.lang3.StringUtils;
25 import org.onap.vfc.nfvo.res.common.constant.ParamConstant;
26 import org.onap.vfc.nfvo.res.common.constant.UrlConstant;
27 import org.onap.vfc.nfvo.res.common.util.RestfulUtil;
28 import org.onap.vfc.nfvo.res.service.dao.inf.VmDao;
29 import org.onap.vfc.nfvo.res.service.entity.VmEntity;
30 import org.onap.vfc.nfvo.res.service.group.inf.VmService;
31 import org.openo.baseservice.remoteservice.exception.ServiceException;
32 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import net.sf.json.JSONArray;
37 import net.sf.json.JSONObject;
38
39 /**
40  * <br>
41  * <p>
42  * </p>
43  * 
44  * @author
45  * @version NFVO 0.5 Oct 29, 2016
46  */
47 public class VmServiceImpl implements VmService {
48
49     private static final Logger LOGGER = LoggerFactory.getLogger(VmServiceImpl.class);
50
51     private VmDao vmDao;
52
53     /**
54      * <br>
55      * 
56      * @param vmEntity
57      * @return
58      * @throws ServiceException
59      * @since NFVO 0.5
60      */
61     @Override
62     public JSONObject addVm(VmEntity vmEntity) throws ServiceException {
63         int result;
64         if(!checkId(vmEntity.getVmId())) {
65             result = vmDao.updateVm(vmEntity);
66             sendMsgMonitor("create", vmEntity);
67         } else {
68             if(StringUtils.isEmpty(vmEntity.getVmId())) {
69                 vmEntity.setVmId(UUID.randomUUID().toString());
70             }
71             result = vmDao.addVm(vmEntity);
72             sendMsgMonitor("update", vmEntity);
73         }
74         JSONObject restJson = new JSONObject();
75         if(result > 0) {
76             restJson.put("id", vmEntity.getVmId());
77             restJson.put("name", vmEntity.getVmName());
78         } else {
79             LOGGER.error("function=addVm; msg=add vm into DB error.");
80             restJson.put("message", "Add vm into DB error.");
81         }
82         return restJson;
83     }
84
85     /**
86      * <br>
87      * 
88      * @param vmId
89      * @return
90      * @since NFVO 0.5
91      */
92     private boolean checkId(String vmId) {
93         VmEntity vm = vmDao.getVm(vmId);
94         if(null == vm) {
95             return true;
96         }
97         return false;
98     }
99
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);
109         } else {
110             JSONArray data = new JSONArray();
111             JSONObject obj = JSONObject.fromObject(vmEntity);
112             obj.put("oid", vmEntity.getVmId());
113             obj.put("moc", "nfv.vdu.linux");
114             data.add(obj);
115             msgObj.put("data", data);
116         }
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);
125         LOGGER.warn(result);
126
127     }
128
129     /**
130      * <br>
131      * 
132      * @param map
133      * @return
134      * @throws ServiceException
135      * @since NFVO 0.5
136      */
137     @Override
138     public List<VmEntity> getList(Map<String, Object> map) throws ServiceException {
139         return vmDao.getVms(map);
140     }
141
142     /**
143      * <br>
144      * 
145      * @param id
146      * @return
147      * @throws ServiceException
148      * @since NFVO 0.5
149      */
150     @Override
151     public int delete(String id) throws ServiceException {
152         return vmDao.deleteVmById(id);
153     }
154
155     public void setVmDao(VmDao vmDao) {
156         this.vmDao = vmDao;
157     }
158
159     /**
160      * <br>
161      * 
162      * @param vnfInstanceId
163      * @throws ServiceException
164      * @since NFVO 0.5
165      */
166     @Override
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);
174         }
175         return vmDao.deleteVmByVnfId(vnfInstanceId);
176     }
177 }