1afbcbb205018837341c2234fa1b3cacc5363625
[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 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;
27
28 import net.sf.json.JSONArray;
29 import net.sf.json.JSONObject;
30
31 /**
32  * <br>
33  * <p>
34  * </p>
35  * 
36  * @author
37  * @version VFC 1.0 Oct 29, 2016
38  */
39 public class GrantResServiceImpl implements GrantResService {
40
41     public static final String ADD_RESOURCE = "addResource";
42
43     public static final String REMOVE_RESOURCE = "removeResource";
44
45     public static final String RESOURCE_TEMPLATE = "resourceTemplate";
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(GrantResServiceImpl.class);
48
49     /**
50      * <br>
51      * 
52      * @param object
53      * @return
54      * @throws ServiceException
55      * @since VFC 1.0
56      */
57     @Override
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);
72         return result;
73     }
74
75     @Override
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);
81         String resType = "";
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);
89         }
90         JSONObject resInfo = getResInfo(object, resType);
91         boolean compareResult = queryLimitsAndCompare(vimId, resInfo);
92         JSONObject result = new JSONObject();
93         if(!compareResult) {
94             result.put("message", "Resource is not enough, grant resource failed.");
95             return result;
96         }
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());
106         return result;
107     }
108
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);
115     }
116
117     private boolean compareResourceWithLimits(JSONObject resInfo, JSONObject availableResourse) {
118         LOGGER.info("function=compareResourceWithLimits; resInfo: {}, availableResourse: {}", resInfo,
119                 availableResourse);
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.");
123             return false;
124         }
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.");
128             return false;
129         }
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.");
133             return false;
134         }
135
136         return true;
137     }
138
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);
150         return available;
151     }
152
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");
163         }
164         LOGGER.info("function=getResInfo; resutl: {}", resourceObj.toString());
165         return resourceObj;
166     }
167
168     /**
169      * <br>
170      * 
171      * @param addResource
172      * @return
173      * @since VFC 1.0
174      */
175     private JSONObject getGrantResource(JSONArray resource) {
176         int cpuNum = 0;
177         int memNum = 0;
178         int diskNum = 0;
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;
192         }
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));
197         return obj;
198     }
199
200     /**
201      * <br>
202      * 
203      * @param object
204      * @return
205      * @since VFC 1.0
206      */
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);
220         }
221         LOGGER.info("function=parseResource; Parse Resource result: {}", newResources.toString());
222         return newResources;
223     }
224
225     /**
226      * <br>
227      * 
228      * @param vimJson
229      * @return
230      * @since VFC 1.0
231      */
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"));
248         return vim;
249     }
250
251 }