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.service.base.openstack.inf.Sites;
21 import org.onap.vfc.nfvo.resmanagement.service.group.inf.GrantResService;
22 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import net.sf.json.JSONArray;
27 import net.sf.json.JSONObject;
35 * @version NFVO 0.5 Oct 29, 2016
37 public class GrantResServiceImpl implements GrantResService {
39 private static final Logger LOGGER = LoggerFactory.getLogger(GrantResServiceImpl.class);
48 * @throws ServiceException
52 public JSONObject grantResource(JSONObject object) throws ServiceException {
53 LOGGER.info("function=grantResource; object: {}", object.toString());
54 JSONObject additionalparam = object.getJSONObject("additionalParam");
55 String vimId = additionalparam.getString("vimid");
56 JSONObject vimJson = VimUtil.getVimById(vimId);
57 String tenant = vimJson.getString("tenant");
58 JSONObject accessinfo = new JSONObject();
59 accessinfo.put("tenant", tenant);
60 JSONObject vim = new JSONObject();
61 vim.put("vimid", vimId);
62 vim.put("accessinfo", accessinfo);
63 LOGGER.info("function=grantResource; vim: {}", vim.toString());
64 JSONObject result = new JSONObject();
65 result.put("vim", vim);
70 public JSONObject grantResourceReal(JSONObject object) throws ServiceException {
71 LOGGER.info("function=grantResource; object: {}", object.toString());
72 String vimId = object.getString("vimId");
73 JSONObject vimJson = VimUtil.getVimById(vimId);
74 JSONObject vim = parseVim(vimJson);
76 JSONArray resArr = new JSONArray();
77 if(object.containsKey("addResource")) {
78 resType = "addResource";
79 resArr = parseResource(object, resType);
80 } else if(object.containsKey("removeResource")) {
81 resType = "removeResource";
82 resArr = parseResource(object, resType);
84 JSONObject resInfo = getResInfo(object, resType);
85 resInfo.put("vimId", vimId);
86 sites.update(resInfo);
88 JSONObject result = new JSONObject();
89 result.put("vim", vim);
90 result.put("zone", "");
91 result.put("zoneGroup", "");
92 result.put(resType, resArr);
93 result.put("tempResource", "");
94 result.put("updateResource", "");
95 result.put("vimAssets", new JSONObject());
96 result.put("additionalParam", "");
97 LOGGER.info("function=grantResource; result: {}", result.toString());
101 private JSONObject getResInfo(JSONObject object, String type) {
102 JSONArray arr = object.getJSONArray(type);
103 LOGGER.info("function=getResInfo; arr: {}, type: {}", arr, type);
104 JSONObject resourceObj = new JSONObject();
105 if("addResource".equals(type)) {
106 resourceObj = getGrantResource(arr);
107 resourceObj.put("action", "online");
108 } else if("removeResource".equals(type)) {
109 resourceObj = getGrantResource(arr);
110 resourceObj.put("action", "offline");
112 LOGGER.info("function=getResInfo; resutl: {}", resourceObj.toString());
123 private JSONObject getGrantResource(JSONArray resource) {
127 for(int i = 0; i < resource.size(); i++) {
128 JSONObject res = resource.getJSONObject(i);
129 JSONObject vCpu = res.getJSONObject("resourceTemplate").getJSONObject("virtualComputeDescriptor")
130 .getJSONObject("virtualCpu");
131 int vCpuNum = vCpu.getInt("numVirtualCpu");
132 JSONObject vMem = res.getJSONObject("resourceTemplate").getJSONObject("virtualComputeDescriptor")
133 .getJSONObject("virtualMemory");
134 int vMemNum = vMem.getInt("virtualMemSize");
135 JSONObject vDisk = res.getJSONObject("resourceTemplate").getJSONObject("virtualStorageDescriptor");
136 int vDiskNum = vDisk.getInt("sizeOfStorage");
137 cpuNum = cpuNum + vCpuNum;
138 memNum = memNum + vMemNum;
139 diskNum = diskNum + vDiskNum;
141 JSONObject obj = new JSONObject();
142 obj.put("usedCPU", String.valueOf(cpuNum));
143 obj.put("usedMemory", String.valueOf(memNum));
144 obj.put("usedDisk", String.valueOf(diskNum));
155 private JSONArray parseResource(JSONObject object, String key) {
156 JSONArray newResources = new JSONArray();
157 JSONArray oldResource = object.getJSONArray(key);
158 LOGGER.info("function=parseResource; Resource: {}", oldResource.toString());
159 for(int i = 0; i < oldResource.size(); i++) {
160 JSONObject res = oldResource.getJSONObject(i);
161 JSONObject obj = new JSONObject();
162 obj.put("reservationId", "");
163 obj.put("resourceProviderId", "");
164 obj.put("zoneId", "");
165 obj.put("vimId", object.getString("vimId"));
166 obj.put("resourceDefinitionId", res.getString("resourceDefinitionId"));
167 newResources.add(obj);
169 LOGGER.info("function=parseResource; Parse Resource result: {}", newResources.toString());
180 private JSONObject parseVim(JSONObject vimJson) {
181 LOGGER.info("function=grantResource; vimJson: {}", vimJson.toString());
182 JSONObject interfaceInfo = new JSONObject();
183 interfaceInfo.put("vimType", vimJson.getString("type"));
184 interfaceInfo.put("apiVersion", "v2");
185 interfaceInfo.put("protocolType", "http");
186 JSONObject accessInfo = new JSONObject();
187 accessInfo.put("tenant", vimJson.getString("tenant"));
188 accessInfo.put("username", vimJson.getString("userName"));
189 accessInfo.put("password", vimJson.getString("password"));
190 JSONObject vim = new JSONObject();
191 vim.put("vimInfoId", vimJson.getString("vimId"));
192 vim.put("vimId", vimJson.getString("vimId"));
193 vim.put("interfaceInfo", interfaceInfo);
194 vim.put("accessInfo", accessInfo);
195 vim.put("interfaceEndpoint", vimJson.getString("url"));
200 * @param sites The sites to set.
202 public void setSites(Sites sites) {