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.res.service.group.impl;
19 import java.util.HashMap;
22 import org.onap.vfc.nfvo.res.common.constant.ParamConstant;
23 import org.onap.vfc.nfvo.res.common.constant.UrlConstant;
24 import org.onap.vfc.nfvo.res.common.util.JsonUtil;
25 import org.onap.vfc.nfvo.res.common.util.RestfulUtil;
26 import org.onap.vfc.nfvo.res.service.base.openstack.inf.InterfaceResManagement;
27 import org.openo.baseservice.remoteservice.exception.ServiceException;
28 import org.openo.baseservice.roa.util.restclient.RestfulParametes;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.transaction.annotation.Transactional;
33 import net.sf.json.JSONArray;
34 import net.sf.json.JSONObject;
37 * iResource add service implementation.<br>
42 * @version NFVO 0.5 Sep 10, 2016
44 public class IResourceAddServiceImpl {
46 private static final Logger LOGGER = LoggerFactory.getLogger(IResourceAddServiceImpl.class);
51 * @param restParametes
53 * @throws ServiceException
56 @Transactional(rollbackFor = ServiceException.class)
57 public void addIRes(RestfulParametes restParametes, Map<String, InterfaceResManagement> iResMap)
58 throws ServiceException {
59 addIResources(iResMap, createResUrlMap(), restParametes);
62 private void addIResources(Map<String, InterfaceResManagement> iResMap, HashMap<String, String> urlMap,
63 RestfulParametes restParametes) throws ServiceException {
64 for(String iResName : iResMap.keySet()) {
65 if(ParamConstant.PARAM_HOST.equals(iResName)) {
66 addHostResource(iResMap, restParametes, String.format(urlMap.get(iResName),
67 restParametes.get(ParamConstant.PARAM_VIMID), restParametes.get(ParamConstant.PARAM_TENANTID)),
70 String url = String.format(urlMap.get(iResName), restParametes.get(ParamConstant.PARAM_VIMID),
71 restParametes.get(ParamConstant.PARAM_TENANTID));
72 JSONArray iResArray = RestfulUtil.getResponseRes(new RestfulParametes(), url, iResName);
73 LOGGER.warn("function=addIResources; iResArray={}", iResArray);
74 for(Object object : iResArray) {
75 JSONObject iRes = JSONObject.fromObject(object);
76 int result = iResMap.get(iResName).add(iRes);
77 LOGGER.warn("function=addIResources; msg=iRes name is [{}],result is [{}]", iResName, result);
84 * Add Host Resource.<br>
87 * @param restParametes
90 * @throws ServiceException
93 public static void addHostResource(Map<String, InterfaceResManagement> iResMap, RestfulParametes restParametes,
94 String url, String iResName) throws ServiceException {
95 JSONArray hostResArray = RestfulUtil.getResponseRes(new RestfulParametes(), url, iResName);
96 LOGGER.warn("function=addHostResource; hostResArray={}", hostResArray);
97 for(Object object : hostResArray) {
98 JSONObject hostRes = JSONObject.fromObject(object);
99 String hostZone = hostRes.getString("zone");
100 if("internal".equals(hostZone)) {
103 String hostName = hostRes.getString("name");
104 String hostUrl = String.format(UrlConstant.GET_HOSTDETAIL_URL, restParametes.get(ParamConstant.PARAM_VIMID),
105 restParametes.get(ParamConstant.PARAM_TENANTID), hostName);
107 String result = RestfulUtil.getResponseContent(hostUrl, new RestfulParametes(), ParamConstant.PARAM_GET);
111 JSONObject hostObj = JSONObject.fromObject(result);
112 JSONObject host = hostDataParse(hostObj, hostName);
113 int res = iResMap.get(iResName).add(host);
114 LOGGER.warn("function=addHostResource; result={}, res={}", result, res);
116 LOGGER.error("function=addHostResource; add into DB fail!");
130 public static JSONObject hostDataParse(JSONObject hostObj, String hostName) {
131 LOGGER.warn("function=hostDataParse; hostObj={}, hostName={}", hostObj, hostName);
132 JSONArray hostArray = hostObj.getJSONArray("host");
133 for(Object object : hostArray) {
134 JSONObject hostObject = JSONObject.fromObject(object);
135 if(hostObject.getString("project").contains("total")) {
136 String vimId = JsonUtil.getJsonFieldStr(hostObj, "vimId");
137 String hostId = vimId + hostName;
138 JSONObject host = new JSONObject();
139 host.put("id", hostId);
140 host.put("name", hostName);
141 host.put("cpu", JsonUtil.getJsonFieldStr(hostObject, "cpu"));
142 host.put("memory", JsonUtil.getJsonFieldStr(hostObject, "memory_mb"));
143 host.put("disk", JsonUtil.getJsonFieldStr(hostObject, "disk_gb"));
144 host.put("vimId", JsonUtil.getJsonFieldStr(hostObj, "vimId"));
145 host.put("vimName", JsonUtil.getJsonFieldStr(hostObj, "vimName"));
149 return new JSONObject();
152 private HashMap<String, String> createResUrlMap() {
153 HashMap<String, String> urlMap = new HashMap<String, String>(10);
154 urlMap.put(ParamConstant.PARAM_NETWORK, UrlConstant.GET_NETWORK_URL);
155 urlMap.put(ParamConstant.PARAM_HOST, UrlConstant.GET_HOST_URL);
156 urlMap.put(ParamConstant.PARAM_PORT, UrlConstant.GET_PORT_URL);