f8006c31a80de2f569c3b7e209c0224c5de9b9f7
[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.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;
31
32 import net.sf.json.JSONArray;
33 import net.sf.json.JSONObject;
34
35 /**
36  * iResource update service implementation class.<br>
37  * <p>
38  * </p>
39  *
40  * @author
41  * @version VFC 1.0 Sep 10, 2016
42  */
43 public class IResourceUpdateServiceImpl {
44
45     private static final Logger LOGGER = LoggerFactory.getLogger(IResourceUpdateServiceImpl.class);
46
47     /**
48      * Update iResource.<br>
49      *
50      * @param vimId
51      * @param restParametes
52      * @param iResMap
53      * @param sites
54      * @throws ServiceException
55      * @since VFC 1.0
56      */
57     @Transactional(rollbackFor = ServiceException.class)
58     public void updateIRes(String vimId, RestfulParametes restParametes, Map<String, InterfaceResManagement> iResMap)
59             throws ServiceException {
60
61         updateIResByName(iResMap, createResUrlMap(), restParametes, vimId);
62     }
63
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)),
70                         resName);
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);
80
81                 }
82             }
83         }
84     }
85
86     private void updateHostResource(Map<String, InterfaceResManagement> iResMap, RestfulParametes restParametes,
87             String url, String iResName) throws ServiceException {
88
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)) {
95                 continue;
96             }
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);
100
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);
106             if(res < 0) {
107                 LOGGER.error("function=updateHostResource; add into DB fail!");
108             }
109
110         }
111     }
112
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);
118         return updateUrlMap;
119     }
120 }