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.HashMap;
20 import java.util.List;
23 import org.apache.commons.lang3.StringUtils;
24 import org.onap.vfc.nfvo.resmanagement.common.ResourceUtil;
25 import org.onap.vfc.nfvo.resmanagement.common.constant.Constant;
26 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
27 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
28 import org.onap.vfc.nfvo.resmanagement.common.util.JsonUtil;
29 import org.onap.vfc.nfvo.resmanagement.common.util.RestfulUtil;
30 import org.onap.vfc.nfvo.resmanagement.common.util.StringUtil;
31 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Host;
32 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.InterfaceResManagement;
33 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Network;
34 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Port;
35 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Sites;
36 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Vim;
37 import org.onap.vfc.nfvo.resmanagement.service.entity.HostEntity;
38 import org.onap.vfc.nfvo.resmanagement.service.entity.VimEntity;
39 import org.onap.vfc.nfvo.resmanagement.service.group.inf.ResOperateService;
40 import org.openo.baseservice.remoteservice.exception.ServiceException;
41 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44 import org.springframework.transaction.annotation.Transactional;
46 import net.sf.json.JSONArray;
47 import net.sf.json.JSONObject;
50 * Resource operation service implementation class.<br>
55 * @version NFVO 0.5 Sep 10, 2016
57 public class ResOperateServiceImpl implements ResOperateService {
59 private static final Logger LOGGER = LoggerFactory.getLogger(ResOperateServiceImpl.class);
63 private Network network;
71 private IResourceAddServiceImpl iResourceAddServiceImpl;
73 private IResourceUpdateServiceImpl iResourceUpdateServiceImpl;
75 private IResourceDelServiceImpl iResourceDelServiceImpl;
78 @Transactional(rollbackFor = ServiceException.class)
79 public void addIRes(String tenantId, String vimId, JSONObject header) throws ServiceException {
80 LOGGER.warn("function=addIRes; msg=add IResource by vimId:[{}], tenantId:[{}]", vimId, tenantId);
81 if(!StringUtil.isValidString(vimId) || !StringUtil.isValidString(tenantId)) {
82 LOGGER.warn("function=addIRes; msg=vimId[{}] is valid", vimId);
83 throw new ServiceException(
84 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.add.res.no.vimId"));
87 RestfulParametes restParametes = createRestfulParametes(tenantId, vimId, header);
88 HashMap<String, InterfaceResManagement> iResMap = createMap();
90 if(vim.add(vimId) <= 0) {
91 LOGGER.error("VimId exists");
92 throw new ServiceException("VimId exists");
95 iResourceAddServiceImpl.addIRes(restParametes, iResMap);
99 @Transactional(rollbackFor = ServiceException.class)
100 public void updateIRes(String tenantId, String vimId, JSONObject header) throws ServiceException {
101 if(!StringUtil.isValidString(vimId)) {
102 LOGGER.error("function=updateIRes; msg=vimId is not exist");
103 throw new ServiceException(
104 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.update.res.no.vimId"));
107 RestfulParametes restParametes = createRestfulParametes(tenantId, vimId, header);
108 HashMap<String, InterfaceResManagement> iResMap = createMap();
109 iResourceUpdateServiceImpl.updateIRes(vimId, restParametes, iResMap);
113 public void updateAllIRes() throws ServiceException {
114 LOGGER.warn("function=updateAllIRes; msg=update all IResource");
115 List<VimEntity> vims = vim.getList();
117 LOGGER.error("function=updateAllIRes; msg=vimId is not exist");
118 throw new ServiceException(
119 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.update.res.no.vimId"));
121 for(VimEntity vimEntity : vims) {
122 String vimId = vimEntity.getId();
123 LOGGER.warn("function=updateAllIRes; msg=start update vimId:{}", vimId);
124 updateIRes(null, vimId, new JSONObject());
129 @Transactional(rollbackFor = ServiceException.class)
130 public int deleteIRes(String vimId) throws ServiceException {
131 LOGGER.warn("function=deleteAllRes; msg=deleteResPool vimId: {}", vimId);
132 if(StringUtils.isEmpty(vimId)) {
133 LOGGER.error("function=deleteAllRes; msg=vimId is null");
134 throw new ServiceException(
135 ResourceUtil.getMessage("org.openo.nfvo.resmanage.service.group.resoperate.res.no.vimId"));
138 return iResourceDelServiceImpl.deleteIRes(vimId, createMap(), vim);
141 private RestfulParametes createRestfulParametes(String tenantId, String vimId, JSONObject header) {
142 RestfulParametes restParametes = new RestfulParametes();
143 restParametes.put("vimId", vimId);
144 restParametes.put("tenantId", tenantId);
145 restParametes.putHttpContextHeader("Content-Type", "application/json");
146 JSONObject headers = JsonUtil.getJsonFieldJson(header, "header");
147 if(null == headers || !headers.has("x-auth-token")) {
148 String token = "TestToken";
149 LOGGER.warn("function=createRestfulParametes; msg=create token.");
150 restParametes.putHttpContextHeader(Constant.IAM_AUTH_TOKEN, token);
151 return restParametes;
153 restParametes.putHttpContextHeader(Constant.IAM_AUTH_TOKEN,
154 JsonUtil.getJsonFieldStr(headers, Constant.IAM_AUTH_TOKEN));
155 return restParametes;
158 private HashMap<String, InterfaceResManagement> createMap() {
159 HashMap<String, InterfaceResManagement> iResMap = new HashMap<String, InterfaceResManagement>(10);
160 iResMap.put(ParamConstant.PARAM_NETWORK, network);
161 iResMap.put(ParamConstant.PARAM_HOST, host);
162 iResMap.put(ParamConstant.PARAM_PORT, port);
166 public void setSites(Sites sites) {
170 public void setNetwork(Network network) {
171 this.network = network;
174 public void setHost(Host host) {
178 public void setPort(Port port) {
182 public void setVim(Vim vim) {
187 * Set iResource Add service implemtation.<br>
189 * @param iResourceAddServiceImpl
192 public void setiResourceAddServiceImpl(IResourceAddServiceImpl iResourceAddServiceImpl) {
193 this.iResourceAddServiceImpl = iResourceAddServiceImpl;
197 * Set iResource update service implementation.<br>
199 * @param iResourceUpdateServiceImpl
202 public void setiResourceUpdateServiceImpl(IResourceUpdateServiceImpl iResourceUpdateServiceImpl) {
203 this.iResourceUpdateServiceImpl = iResourceUpdateServiceImpl;
207 * Set iresource delete service implementation.<br>
209 * @param iResourceDelServiceImpl
212 public void setiResourceDelServiceImpl(IResourceDelServiceImpl iResourceDelServiceImpl) {
213 this.iResourceDelServiceImpl = iResourceDelServiceImpl;
220 * @throws ServiceException
224 public void sendMsgMonitor(String operateType, String vimId) throws ServiceException {
225 LOGGER.info("Enter sendMsgMonitor!");
226 Map<String, Object> map = new HashMap<>(10);
227 map.put("vimId", vimId);
228 List<HostEntity> hosts = host.getList(map);
229 for(HostEntity entity : hosts) {
230 JSONObject msgObj = new JSONObject();
231 msgObj.put("operationType", operateType);
232 msgObj.put("resourceType", "HOST");
233 msgObj.put("label", entity.getName());
234 if("delete".equals(operateType)) {
235 JSONArray deleteIds = new JSONArray();
236 deleteIds.add(entity.getId());
237 msgObj.put("deleteIds", deleteIds);
239 JSONArray data = new JSONArray();
240 JSONObject obj = JSONObject.fromObject(entity);
241 obj.put("oid", entity.getId());
242 obj.put("moc", "nfv.host.linux");
244 msgObj.put("data", data);
246 RestfulParametes restfulParametes = new RestfulParametes();
247 Map<String, String> headerMap = new HashMap<>(3);
248 headerMap.put("Content-Type", "application/json");
249 restfulParametes.setHeaderMap(headerMap);
250 restfulParametes.setRawData(msgObj.toString());
251 LOGGER.info("sendMsgMonitor msgObj: {}", msgObj);
252 String result = RestfulUtil.getResponseContent(UrlConstant.SEND_MSG_MONITOR, restfulParametes,
253 ParamConstant.PARAM_POST);