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.apache.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21 import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
22 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
23 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
24 import org.onap.vfc.nfvo.resmanagement.service.business.impl.LimitsBusinessImpl;
25 import org.onap.vfc.nfvo.resmanagement.service.business.inf.LimitsBusiness;
26 import org.onap.vfc.nfvo.resmanagement.service.group.inf.GrantResService;
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 = LogManager.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);
75 private String getVimId(JSONObject object) {
77 if (object.containsKey(ParamConstant.PARAM_VIMID)){
78 vimId = object.getString(ParamConstant.PARAM_VIMID);
80 // TODO: Need integrate OOF to get vimID in R3.
84 public JSONObject grantResourceReal(JSONObject object) throws ServiceException {
85 LOGGER.info("function=grantResource; object: {}", object.toString());
86 String vimId = getVimId(object);
87 JSONObject vimJson = VimUtil.getVimById(vimId);
88 JSONObject vim = parseVim(vimJson);
90 JSONArray resArr = new JSONArray();
91 if(object.containsKey(ADD_RESOURCE)) {
92 resType = ADD_RESOURCE;
93 resArr = parseResource(object, resType);
94 } else if(object.containsKey(REMOVE_RESOURCE)) {
95 resType = REMOVE_RESOURCE;
96 resArr = parseResource(object, resType);
98 JSONObject resInfo = getResInfo(object, resType);
99 boolean compareResult = queryLimitsAndCompare(vimId, resInfo);
100 JSONObject result = new JSONObject();
102 result.put("message", "Resource is not enough, grant resource failed.");
105 result.put("vim", vim);
106 result.put("zone", "");
107 result.put("zoneGroup", "");
108 result.put(resType, resArr);
109 result.put("tempResource", "");
110 result.put("updateResource", "");
111 result.put("vimAssets", new JSONObject());
112 result.put("additionalParam", "");
113 LOGGER.info("function=grantResource; result: {}", result.toString());
117 private boolean queryLimitsAndCompare(String vimId, JSONObject resInfo) throws ServiceException {
118 LimitsBusiness limitsBusiness = new LimitsBusinessImpl();
119 JSONObject limits = limitsBusiness.getLimits(vimId);
120 LOGGER.info("function=QueryLimitsAndCompare; limits: {}", limits);
121 JSONObject availableResourse = getAvailableFromLimits(limits);
122 return compareResourceWithLimits(resInfo, availableResourse);
125 private boolean compareResourceWithLimits(JSONObject resInfo, JSONObject availableResourse) {
126 LOGGER.info("function=compareResourceWithLimits; resInfo: {}, availableResourse: {}", resInfo,
128 if(Float.parseFloat(resInfo.getString(ParamConstant.USED_CPU))
129 - Float.parseFloat(availableResourse.getString("availableCPU")) > 0) {
130 LOGGER.info("function=compareResourceWithLimits; CPU is not enough.");
133 if(Float.parseFloat(resInfo.getString(ParamConstant.USED_MEMORY))
134 - Float.parseFloat(availableResourse.getString("availableMemory")) > 0) {
135 LOGGER.info("function=compareResourceWithLimits; Memory is not enough.");
138 if(Float.parseFloat(resInfo.getString(ParamConstant.USED_DISK))
139 - Float.parseFloat(availableResourse.getString("availableDisk")) > 0) {
140 LOGGER.info("function=compareResourceWithLimits; Disk is not enough.");
147 private JSONObject getAvailableFromLimits(JSONObject limits) {
148 JSONObject available = new JSONObject();
149 String availableCPU = String.valueOf(Float.parseFloat(limits.getString("totalCPU"))
150 - Float.parseFloat(limits.getString(ParamConstant.USED_CPU)));
151 String availableMemory = String.valueOf(Float.parseFloat(limits.getString("totalMemory"))
152 - Float.parseFloat(limits.getString(ParamConstant.USED_MEMORY)));
153 String availableDisk = String.valueOf(Float.parseFloat(limits.getString("totalDisk"))
154 - Float.parseFloat(limits.getString(ParamConstant.USED_DISK)));
155 available.put("availableCPU", availableCPU);
156 available.put("availableMemory", availableMemory);
157 available.put("availableDisk", availableDisk);
161 private JSONObject getResInfo(JSONObject object, String type) {
162 JSONArray arr = object.getJSONArray(type);
163 LOGGER.info("function=getResInfo; arr: {}, type: {}", arr, type);
164 JSONObject resourceObj = new JSONObject();
165 if(ADD_RESOURCE.equals(type)) {
166 resourceObj = getGrantResource(arr);
167 resourceObj.put("action", "online");
168 } else if(REMOVE_RESOURCE.equals(type)) {
169 resourceObj = getGrantResource(arr);
170 resourceObj.put("action", "offline");
172 LOGGER.info("function=getResInfo; resutl: {}", resourceObj.toString());
179 * @param getGrantResource
183 private JSONObject getGrantResource(JSONArray resource) {
187 for(int i = 0; i < resource.size(); i++) {
188 JSONObject res = resource.getJSONObject(i);
189 JSONObject vCpu = res.getJSONObject(RESOURCE_TEMPLATE).getJSONObject("virtualComputeDescriptor")
190 .getJSONObject("virtualCpu");
191 int vCpuNum = vCpu.getInt("numVirtualCpu");
192 JSONObject vMem = res.getJSONObject(RESOURCE_TEMPLATE).getJSONObject("virtualComputeDescriptor")
193 .getJSONObject("virtualMemory");
194 int vMemNum = vMem.getInt("virtualMemSize");
195 JSONObject vDisk = res.getJSONObject(RESOURCE_TEMPLATE).getJSONObject("virtualStorageDescriptor");
196 int vDiskNum = vDisk.getInt("sizeOfStorage");
197 cpuNum = cpuNum + vCpuNum;
198 memNum = memNum + vMemNum;
199 diskNum = diskNum + vDiskNum;
201 JSONObject obj = new JSONObject();
202 obj.put(ParamConstant.USED_CPU, String.valueOf(cpuNum));
203 obj.put(ParamConstant.USED_MEMORY, String.valueOf(memNum));
204 obj.put(ParamConstant.USED_DISK, String.valueOf(diskNum));
215 private JSONArray parseResource(JSONObject object, String key) {
216 JSONArray newResources = new JSONArray();
217 JSONArray oldResource = object.getJSONArray(key);
218 LOGGER.info("function=parseResource; Resource: {}", oldResource.toString());
219 for(int i = 0; i < oldResource.size(); i++) {
220 JSONObject res = oldResource.getJSONObject(i);
221 JSONObject obj = new JSONObject();
222 obj.put("reservationId", "");
223 obj.put("resourceProviderId", "");
224 obj.put("zoneId", "");
225 obj.put(ParamConstant.PARAM_VIMID, object.getString(ParamConstant.PARAM_VIMID));
226 obj.put("resourceDefinitionId", res.getString("resourceDefinitionId"));
227 newResources.add(obj);
229 LOGGER.info("function=parseResource; Parse Resource result: {}", newResources.toString());
240 private JSONObject parseVim(JSONObject vimJson) {
241 LOGGER.info("function=grantResource; vimJson: {}", vimJson.toString());
242 JSONObject interfaceInfo = new JSONObject();
243 interfaceInfo.put("vimType", vimJson.getString("type"));
244 interfaceInfo.put("apiVersion", "v2");
245 interfaceInfo.put("protocolType", "http");
246 JSONObject accessInfo = new JSONObject();
247 accessInfo.put(ParamConstant.PARAM_TENANT, vimJson.getString(ParamConstant.PARAM_TENANT));
248 accessInfo.put("username", vimJson.getString("userName"));
249 accessInfo.put("password", vimJson.getString("password"));
250 JSONObject vim = new JSONObject();
251 vim.put("vimInfoId", vimJson.getString(ParamConstant.PARAM_VIMID));
252 vim.put(ParamConstant.PARAM_VIMID, vimJson.getString(ParamConstant.PARAM_VIMID));
253 vim.put("interfaceInfo", interfaceInfo);
254 vim.put("accessInfo", accessInfo);
255 vim.put("interfaceEndpoint", vimJson.getString("url"));