ac255fc8beaba77f716e6f5b65206b53961afb2d
[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.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;
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 = LogManager.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     private String getVimId(JSONObject object) {
76         String vimId = "";
77         if (object.containsKey(ParamConstant.PARAM_VIMID)){
78             vimId = object.getString(ParamConstant.PARAM_VIMID);
79         }
80         // TODO: Need integrate OOF to get vimID in R3.
81         return vimId;
82     }
83     @Override
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);
89         String resType = "";
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);
97         }
98         JSONObject resInfo = getResInfo(object, resType);
99         boolean compareResult = queryLimitsAndCompare(vimId, resInfo);
100         JSONObject result = new JSONObject();
101         if(!compareResult) {
102             result.put("message", "Resource is not enough, grant resource failed.");
103             return result;
104         }
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());
114         return result;
115     }
116
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);
123     }
124
125     private boolean compareResourceWithLimits(JSONObject resInfo, JSONObject availableResourse) {
126         LOGGER.info("function=compareResourceWithLimits; resInfo: {}, availableResourse: {}", resInfo,
127                 availableResourse);
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.");
131             return false;
132         }
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.");
136             return false;
137         }
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.");
141             return false;
142         }
143
144         return true;
145     }
146
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);
158         return available;
159     }
160
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");
171         }
172         LOGGER.info("function=getResInfo; resutl: {}", resourceObj.toString());
173         return resourceObj;
174     }
175
176     /**
177      * <br>
178      * 
179      * @param getGrantResource
180      * @return
181      * @since VFC 1.0
182      */
183     private JSONObject getGrantResource(JSONArray resource) {
184         int cpuNum = 0;
185         int memNum = 0;
186         int diskNum = 0;
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;
200         }
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));
205         return obj;
206     }
207
208     /**
209      * <br>
210      * 
211      * @param object
212      * @return
213      * @since VFC 1.0
214      */
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);
228         }
229         LOGGER.info("function=parseResource; Parse Resource result: {}", newResources.toString());
230         return newResources;
231     }
232
233     /**
234      * <br>
235      * 
236      * @param vimJson
237      * @return
238      * @since VFC 1.0
239      */
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"));
256         return vim;
257     }
258
259 }