4a8ae3910e556721cbb229684150bbdbdc9b8707
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016-2017 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.resmanagement.service.group.impl;
18
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22
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;
45
46 import net.sf.json.JSONArray;
47 import net.sf.json.JSONObject;
48
49 /**
50  * Resource operation service implementation class.<br>
51  * <p>
52  * </p>
53  *
54  * @author
55  * @version NFVO 0.5 Sep 10, 2016
56  */
57 public class ResOperateServiceImpl implements ResOperateService {
58
59     private static final Logger LOGGER = LoggerFactory.getLogger(ResOperateServiceImpl.class);
60
61     private Sites sites;
62
63     private Network network;
64
65     private Host host;
66
67     private Port port;
68
69     private Vim vim;
70
71     private IResourceAddServiceImpl iResourceAddServiceImpl;
72
73     private IResourceUpdateServiceImpl iResourceUpdateServiceImpl;
74
75     private IResourceDelServiceImpl iResourceDelServiceImpl;
76
77     @Override
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"));
85         }
86
87         RestfulParametes restParametes = createRestfulParametes(tenantId, vimId, header);
88         HashMap<String, InterfaceResManagement> iResMap = createMap();
89
90         if(vim.add(vimId) <= 0) {
91             LOGGER.error("VimId exists");
92             throw new ServiceException("VimId exists");
93         }
94
95         iResourceAddServiceImpl.addIRes(restParametes, iResMap);
96     }
97
98     @Override
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"));
105         }
106
107         RestfulParametes restParametes = createRestfulParametes(tenantId, vimId, header);
108         HashMap<String, InterfaceResManagement> iResMap = createMap();
109         iResourceUpdateServiceImpl.updateIRes(vimId, restParametes, iResMap);
110     }
111
112     @Override
113     public void updateAllIRes() throws ServiceException {
114         LOGGER.warn("function=updateAllIRes; msg=update all IResource");
115         List<VimEntity> vims = vim.getList();
116         if(vims.isEmpty()) {
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"));
120         }
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());
125         }
126     }
127
128     @Override
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"));
136         }
137
138         return iResourceDelServiceImpl.deleteIRes(vimId, createMap(), vim);
139     }
140
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;
152         }
153         restParametes.putHttpContextHeader(Constant.IAM_AUTH_TOKEN,
154                 JsonUtil.getJsonFieldStr(headers, Constant.IAM_AUTH_TOKEN));
155         return restParametes;
156     }
157
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);
163         return iResMap;
164     }
165
166     public void setSites(Sites sites) {
167         this.sites = sites;
168     }
169
170     public void setNetwork(Network network) {
171         this.network = network;
172     }
173
174     public void setHost(Host host) {
175         this.host = host;
176     }
177
178     public void setPort(Port port) {
179         this.port = port;
180     }
181
182     public void setVim(Vim vim) {
183         this.vim = vim;
184     }
185
186     /**
187      * Set iResource Add service implemtation.<br>
188      *
189      * @param iResourceAddServiceImpl
190      * @since NFVO 0.5
191      */
192     public void setiResourceAddServiceImpl(IResourceAddServiceImpl iResourceAddServiceImpl) {
193         this.iResourceAddServiceImpl = iResourceAddServiceImpl;
194     }
195
196     /**
197      * Set iResource update service implementation.<br>
198      *
199      * @param iResourceUpdateServiceImpl
200      * @since NFVO 0.5
201      */
202     public void setiResourceUpdateServiceImpl(IResourceUpdateServiceImpl iResourceUpdateServiceImpl) {
203         this.iResourceUpdateServiceImpl = iResourceUpdateServiceImpl;
204     }
205
206     /**
207      * Set iresource delete service implementation.<br>
208      *
209      * @param iResourceDelServiceImpl
210      * @since NFVO 0.5
211      */
212     public void setiResourceDelServiceImpl(IResourceDelServiceImpl iResourceDelServiceImpl) {
213         this.iResourceDelServiceImpl = iResourceDelServiceImpl;
214     }
215
216     /**
217      * <br>
218      * 
219      * @param string
220      * @throws ServiceException
221      * @since NFVO 0.5
222      */
223     @Override
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);
238             } else {
239                 JSONArray data = new JSONArray();
240                 JSONObject obj = JSONObject.fromObject(entity);
241                 obj.put("oid", entity.getId());
242                 obj.put("moc", "nfv.host.linux");
243                 data.add(obj);
244                 msgObj.put("data", data);
245             }
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);
254             LOGGER.warn(result);
255         }
256     }
257 }