66d87d98852854ffc2e296a73889a6f5a18d5020
[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.Map;
21
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.JsonUtil;
25 import org.onap.vfc.nfvo.resmanagement.common.util.RestfulUtil;
26 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.InterfaceResManagement;
27 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
28 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.RestfulParametes;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.transaction.annotation.Transactional;
32
33 import net.sf.json.JSONArray;
34 import net.sf.json.JSONObject;
35
36 /**
37  * iResource add service implementation.<br>
38  * <p>
39  * </p>
40  *
41  * @author
42  * @version VFC 1.0 Sep 10, 2016
43  */
44 public class IResourceAddServiceImpl {
45
46     private static final Logger LOGGER = LoggerFactory.getLogger(IResourceAddServiceImpl.class);
47
48     /**
49      * Add iResource.<br>
50      *
51      * @param restParametes
52      * @param iResMap
53      * @throws ServiceException
54      * @since VFC 1.0
55      */
56     @Transactional(rollbackFor = ServiceException.class)
57     public void addIRes(RestfulParametes restParametes, Map<String, InterfaceResManagement> iResMap)
58             throws ServiceException {
59         addIResources(iResMap, createResUrlMap(), restParametes);
60     }
61
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)),
68                         iResName);
69             } else {
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);
78                 }
79             }
80         }
81     }
82
83     /**
84      * Add Host Resource.<br>
85      *
86      * @param iResMap
87      * @param restParametes
88      * @param url
89      * @param iResName
90      * @throws ServiceException
91      * @since VFC 1.0
92      */
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)) {
101                 continue;
102             }
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);
106
107             String result = RestfulUtil.getResponseContent(hostUrl, new RestfulParametes(), ParamConstant.PARAM_GET);
108             if(result == null) {
109                 continue;
110             }
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);
115             if(res < 0) {
116                 LOGGER.error("function=addHostResource; add into DB fail!");
117             }
118
119         }
120     }
121
122     /**
123      * <br>
124      *
125      * @param hostObj
126      * @param hostName
127      * @return
128      * @since VFC 1.0
129      */
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"));
146                 return host;
147             }
148         }
149         return new JSONObject();
150     }
151
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);
157         return urlMap;
158     }
159 }