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;
22 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
23 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
24 import org.onap.vfc.nfvo.resmanagement.common.util.RestfulUtil;
25 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.InterfaceResManagement;
26 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
27 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulParametes;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.transaction.annotation.Transactional;
32 import net.sf.json.JSONArray;
33 import net.sf.json.JSONObject;
36 * iResource update service implementation class.<br>
41 * @version NFVO 0.5 Sep 10, 2016
43 public class IResourceUpdateServiceImpl {
45 private static final Logger LOGGER = LoggerFactory.getLogger(IResourceUpdateServiceImpl.class);
48 * Update iResource.<br>
51 * @param restParametes
54 * @throws ServiceException
57 @Transactional(rollbackFor = ServiceException.class)
58 public void updateIRes(String vimId, RestfulParametes restParametes, Map<String, InterfaceResManagement> iResMap)
59 throws ServiceException {
61 updateIResByName(iResMap, createResUrlMap(), restParametes, vimId);
64 private void updateIResByName(Map<String, InterfaceResManagement> iResMap, HashMap<String, String> updateUrlMap,
65 RestfulParametes restParametes, String vimId) throws ServiceException {
66 for(String resName : updateUrlMap.keySet()) {
67 if(ParamConstant.PARAM_HOST.equals(resName)) {
68 updateHostResource(iResMap, restParametes, String.format(updateUrlMap.get(resName),
69 restParametes.get(ParamConstant.PARAM_VIMID), restParametes.get(ParamConstant.PARAM_TENANTID)),
71 } else if(iResMap.get(resName).deleteResByVimId(vimId) >= 0) {
72 String url = String.format(updateUrlMap.get(resName), restParametes.get(ParamConstant.PARAM_VIMID),
73 restParametes.get(ParamConstant.PARAM_TENANTID));
74 JSONArray iResArray = RestfulUtil.getResponseRes(new RestfulParametes(), url, resName);
75 LOGGER.warn("function=addIResources; iResArray={}", iResArray);
76 for(Object object : iResArray) {
77 JSONObject iRes = JSONObject.fromObject(object);
78 int result = iResMap.get(resName).add(iRes);
79 LOGGER.warn("function=updateIResByName; msg=iRes name is [{}],result is [{}]", resName, result);
86 private void updateHostResource(Map<String, InterfaceResManagement> iResMap, RestfulParametes restParametes,
87 String url, String iResName) throws ServiceException {
89 JSONArray hostResArray = RestfulUtil.getResponseRes(new RestfulParametes(), url, iResName);
90 LOGGER.warn("function=updateHostResource; hostResArray={}", hostResArray);
91 for(Object object : hostResArray) {
92 JSONObject hostRes = JSONObject.fromObject(object);
93 String hostZone = hostRes.getString("zone");
94 if("internal".equals(hostZone)) {
97 String hostName = hostRes.getString("name");
98 String hostUrl = String.format(UrlConstant.GET_HOSTDETAIL_URL, restParametes.get(ParamConstant.PARAM_VIMID),
99 restParametes.get(ParamConstant.PARAM_TENANTID), hostName);
101 String result = RestfulUtil.getResponseContent(hostUrl, new RestfulParametes(), ParamConstant.PARAM_GET);
102 JSONObject hostObj = JSONObject.fromObject(result);
103 JSONObject host = IResourceAddServiceImpl.hostDataParse(hostObj, hostName);
104 int res = iResMap.get(ParamConstant.PARAM_HOST).update(host);
105 LOGGER.warn("function=updateHostResource; result={}, res={}", result, res);
107 LOGGER.error("function=updateHostResource; add into DB fail!");
113 private HashMap<String, String> createResUrlMap() {
114 HashMap<String, String> updateUrlMap = new HashMap<String, String>(10);
115 updateUrlMap.put(ParamConstant.PARAM_NETWORK, UrlConstant.GET_NETWORK_URL);
116 updateUrlMap.put(ParamConstant.PARAM_HOST, UrlConstant.GET_HOST_URL);
117 updateUrlMap.put(ParamConstant.PARAM_PORT, UrlConstant.GET_PORT_URL);