08b7af17728ee9cd7a21a3920a6acb5b0603cd43
[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.business.impl;
18
19 import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
20 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
21 import org.onap.vfc.nfvo.resmanagement.common.constant.UrlConstant;
22 import org.onap.vfc.nfvo.resmanagement.common.util.RestfulUtil;
23 import org.onap.vfc.nfvo.resmanagement.service.business.inf.LimitsBusiness;
24 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 import net.sf.json.JSONObject;
29
30 /**
31  * <br>
32  * <p>
33  * </p>
34  *
35  * @author
36  * @version VFC 1.0 Sep 10, 2016
37  */
38 public class LimitsBusinessImpl implements LimitsBusiness {
39
40     private static final Logger LOGGER = LoggerFactory.getLogger(LimitsBusinessImpl.class);
41
42     /**
43      * <br>
44      *
45      * @param vimId
46      * @param tenantId
47      * @return
48      * @since VFC 1.0
49      */
50     private JSONObject getResponse(String vimId, String tenantId) {
51         LOGGER.info("function=getResponse; vimId={}, tenantId={}", vimId, tenantId);
52         String url = String.format(UrlConstant.GET_LIMITS_URL, vimId, tenantId);
53         JSONObject result = RestfulUtil.getResponseObj(url, ParamConstant.PARAM_GET);
54         LOGGER.warn("function=getResponse; result={}", result);
55         if(null == result) {
56             JSONObject obj = new JSONObject();
57             obj.put("msg", "getLimits fail!");
58             return obj;
59         }
60         return result;
61     }
62
63     @Override
64     public JSONObject getLimits(String vimId) throws ServiceException {
65         JSONObject vimInfo = VimUtil.getVimById(vimId);
66         LOGGER.info("GetLimits vimInfo: {}", vimInfo);
67         String vimName = vimInfo.getString("name");
68         String tenant = vimInfo.getString("tenant");
69         String tenantId = VimUtil.getTenantIdByName(tenant, vimId);
70
71         JSONObject limits = getResponse(vimId, tenantId);
72
73         String totalCPU = String.valueOf(limits.get("maxTotalCores"));
74         String totalMemory = String.valueOf(limits.get("maxTotalRAMSize"));
75         String totalDisk = String.valueOf(limits.get("maxTotalVolumeGigabytes"));
76         String usedCPU = String.valueOf(limits.get("totalCoresUsed"));
77         String usedMemory = String.valueOf(limits.get("totalRAMUsed"));
78         String usedDisk = String.valueOf(limits.get("totalGigabytesUsed"));
79
80         JSONObject result = new JSONObject();
81         result.put("vimId", vimId);
82         result.put("vimName", vimName);
83         result.put("totalCPU", totalCPU);
84         result.put("totalMemory", totalMemory);
85         result.put("totalDisk", totalDisk);
86         result.put("usedCPU", usedCPU);
87         result.put("usedMemory", usedMemory);
88         result.put("usedDisk", usedDisk);
89         LOGGER.info("getLimits result:{}", result);
90         return result;
91     }
92
93 }