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 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.util.restclient.ServiceException;
22 import org.onap.vfc.nfvo.resmanagement.service.business.impl.LimitsBusinessImpl;
23 import org.onap.vfc.nfvo.resmanagement.service.business.inf.LimitsBusiness;
24 import org.onap.vfc.nfvo.resmanagement.service.group.inf.GrantResService;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import net.sf.json.JSONArray;
29 import net.sf.json.JSONObject;
37 * @version VFC 1.0 Oct 29, 2016
39 public class GrantResServiceImpl implements GrantResService {
41 public static final String ADD_RESOURCE = "addResource";
43 public static final String REMOVE_RESOURCE = "removeResource";
45 public static final String RESOURCE_TEMPLATE = "resourceTemplate";
47 private static final Logger LOGGER = LoggerFactory.getLogger(GrantResServiceImpl.class);
54 * @throws ServiceException
58 public JSONObject grantResource(JSONObject object) throws ServiceException {
59 LOGGER.info("function=grantResource; object: {}", object.toString());
60 JSONObject additionalparam = object.getJSONObject("additionalParam");
61 String vimId = additionalparam.getString(ParamConstant.PARAM_VIMID);
62 JSONObject vimJson = VimUtil.getVimById(vimId);
63 String tenant = vimJson.getString(ParamConstant.PARAM_TENANT);
64 JSONObject accessinfo = new JSONObject();
65 accessinfo.put(ParamConstant.PARAM_TENANT, tenant);
66 JSONObject vim = new JSONObject();
67 vim.put(ParamConstant.PARAM_VIMID, vimId);
68 vim.put("accessinfo", accessinfo);
69 LOGGER.info("function=grantResource; vim: {}", vim.toString());
70 JSONObject result = new JSONObject();
71 result.put("vim", vim);
76 public JSONObject grantResourceReal(JSONObject object) throws ServiceException {
77 LOGGER.info("function=grantResource; object: {}", object.toString());
78 String vimId = object.getString(ParamConstant.PARAM_VIMID);
79 JSONObject vimJson = VimUtil.getVimById(vimId);
80 JSONObject vim = parseVim(vimJson);
82 JSONArray resArr = new JSONArray();
83 if(object.containsKey(ADD_RESOURCE)) {
84 resType = ADD_RESOURCE;
85 resArr = parseResource(object, resType);
86 } else if(object.containsKey(REMOVE_RESOURCE)) {
87 resType = REMOVE_RESOURCE;
88 resArr = parseResource(object, resType);
90 JSONObject resInfo = getResInfo(object, resType);
91 boolean compareResult = queryLimitsAndCompare(vimId, resInfo);
92 JSONObject result = new JSONObject();
94 result.put("message", "Resource is not enough, grant resource failed.");
97 result.put("vim", vim);
98 result.put("zone", "");
99 result.put("zoneGroup", "");
100 result.put(resType, resArr);
101 result.put("tempResource", "");
102 result.put("updateResource", "");
103 result.put("vimAssets", new JSONObject());
104 result.put("additionalParam", "");
105 LOGGER.info("function=grantResource; result: {}", result.toString());
109 private boolean queryLimitsAndCompare(String vimId, JSONObject resInfo) throws ServiceException {
110 LimitsBusiness limitsBusiness = new LimitsBusinessImpl();
111 JSONObject limits = limitsBusiness.getLimits(vimId);
112 LOGGER.info("function=QueryLimitsAndCompare; limits: {}", limits);
113 JSONObject availableResourse = getAvailableFromLimits(limits);
114 return compareResourceWithLimits(resInfo, availableResourse);
117 private boolean compareResourceWithLimits(JSONObject resInfo, JSONObject availableResourse) {
118 LOGGER.info("function=compareResourceWithLimits; resInfo: {}, availableResourse: {}", resInfo,
120 if(Float.parseFloat(resInfo.getString(ParamConstant.USED_CPU))
121 - Float.parseFloat(availableResourse.getString("availableCPU")) > 0) {
122 LOGGER.info("function=compareResourceWithLimits; CPU is not enough.");
125 if(Float.parseFloat(resInfo.getString(ParamConstant.USED_MEMORY))
126 - Float.parseFloat(availableResourse.getString("availableMemory")) > 0) {
127 LOGGER.info("function=compareResourceWithLimits; Memory is not enough.");
130 if(Float.parseFloat(resInfo.getString(ParamConstant.USED_DISK))
131 - Float.parseFloat(availableResourse.getString("availableDisk")) > 0) {
132 LOGGER.info("function=compareResourceWithLimits; Disk is not enough.");
139 private JSONObject getAvailableFromLimits(JSONObject limits) {
140 JSONObject available = new JSONObject();
141 String availableCPU = String.valueOf(Float.parseFloat(limits.getString("totalCPU"))
142 - Float.parseFloat(limits.getString(ParamConstant.USED_CPU)));
143 String availableMemory = String.valueOf(Float.parseFloat(limits.getString("totalMemory"))
144 - Float.parseFloat(limits.getString(ParamConstant.USED_MEMORY)));
145 String availableDisk = String.valueOf(Float.parseFloat(limits.getString("totalDisk"))
146 - Float.parseFloat(limits.getString(ParamConstant.USED_DISK)));
147 available.put("availableCPU", availableCPU);
148 available.put("availableMemory", availableMemory);
149 available.put("availableDisk", availableDisk);
153 private JSONObject getResInfo(JSONObject object, String type) {
154 JSONArray arr = object.getJSONArray(type);
155 LOGGER.info("function=getResInfo; arr: {}, type: {}", arr, type);
156 JSONObject resourceObj = new JSONObject();
157 if(ADD_RESOURCE.equals(type)) {
158 resourceObj = getGrantResource(arr);
159 resourceObj.put("action", "online");
160 } else if(REMOVE_RESOURCE.equals(type)) {
161 resourceObj = getGrantResource(arr);
162 resourceObj.put("action", "offline");
164 LOGGER.info("function=getResInfo; resutl: {}", resourceObj.toString());
175 private JSONObject getGrantResource(JSONArray resource) {
179 for(int i = 0; i < resource.size(); i++) {
180 JSONObject res = resource.getJSONObject(i);
181 JSONObject vCpu = res.getJSONObject(RESOURCE_TEMPLATE).getJSONObject("virtualComputeDescriptor")
182 .getJSONObject("virtualCpu");
183 int vCpuNum = vCpu.getInt("numVirtualCpu");
184 JSONObject vMem = res.getJSONObject(RESOURCE_TEMPLATE).getJSONObject("virtualComputeDescriptor")
185 .getJSONObject("virtualMemory");
186 int vMemNum = vMem.getInt("virtualMemSize");
187 JSONObject vDisk = res.getJSONObject(RESOURCE_TEMPLATE).getJSONObject("virtualStorageDescriptor");
188 int vDiskNum = vDisk.getInt("sizeOfStorage");
189 cpuNum = cpuNum + vCpuNum;
190 memNum = memNum + vMemNum;
191 diskNum = diskNum + vDiskNum;
193 JSONObject obj = new JSONObject();
194 obj.put(ParamConstant.USED_CPU, String.valueOf(cpuNum));
195 obj.put(ParamConstant.USED_MEMORY, String.valueOf(memNum));
196 obj.put(ParamConstant.USED_DISK, String.valueOf(diskNum));
207 private JSONArray parseResource(JSONObject object, String key) {
208 JSONArray newResources = new JSONArray();
209 JSONArray oldResource = object.getJSONArray(key);
210 LOGGER.info("function=parseResource; Resource: {}", oldResource.toString());
211 for(int i = 0; i < oldResource.size(); i++) {
212 JSONObject res = oldResource.getJSONObject(i);
213 JSONObject obj = new JSONObject();
214 obj.put("reservationId", "");
215 obj.put("resourceProviderId", "");
216 obj.put("zoneId", "");
217 obj.put(ParamConstant.PARAM_VIMID, object.getString(ParamConstant.PARAM_VIMID));
218 obj.put("resourceDefinitionId", res.getString("resourceDefinitionId"));
219 newResources.add(obj);
221 LOGGER.info("function=parseResource; Parse Resource result: {}", newResources.toString());
232 private JSONObject parseVim(JSONObject vimJson) {
233 LOGGER.info("function=grantResource; vimJson: {}", vimJson.toString());
234 JSONObject interfaceInfo = new JSONObject();
235 interfaceInfo.put("vimType", vimJson.getString("type"));
236 interfaceInfo.put("apiVersion", "v2");
237 interfaceInfo.put("protocolType", "http");
238 JSONObject accessInfo = new JSONObject();
239 accessInfo.put(ParamConstant.PARAM_TENANT, vimJson.getString(ParamConstant.PARAM_TENANT));
240 accessInfo.put("username", vimJson.getString("userName"));
241 accessInfo.put("password", vimJson.getString("password"));
242 JSONObject vim = new JSONObject();
243 vim.put("vimInfoId", vimJson.getString(ParamConstant.PARAM_VIMID));
244 vim.put(ParamConstant.PARAM_VIMID, vimJson.getString(ParamConstant.PARAM_VIMID));
245 vim.put("interfaceInfo", interfaceInfo);
246 vim.put("accessInfo", accessInfo);
247 vim.put("interfaceEndpoint", vimJson.getString("url"));