summaryrefslogtreecommitdiffstats
path: root/ResmanagementService/service/src/main/java/org/onap/vfc/nfvo/resmanagement/service/group/impl/GrantResServiceImpl.java
blob: 1afbcbb205018837341c2234fa1b3cacc5363625 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
 * Copyright 2016-2017 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.vfc.nfvo.resmanagement.service.group.impl;

import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
import org.onap.vfc.nfvo.resmanagement.service.business.impl.LimitsBusinessImpl;
import org.onap.vfc.nfvo.resmanagement.service.business.inf.LimitsBusiness;
import org.onap.vfc.nfvo.resmanagement.service.group.inf.GrantResService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * <br>
 * <p>
 * </p>
 * 
 * @author
 * @version VFC 1.0 Oct 29, 2016
 */
public class GrantResServiceImpl implements GrantResService {

    public static final String ADD_RESOURCE = "addResource";

    public static final String REMOVE_RESOURCE = "removeResource";

    public static final String RESOURCE_TEMPLATE = "resourceTemplate";

    private static final Logger LOGGER = LoggerFactory.getLogger(GrantResServiceImpl.class);

    /**
     * <br>
     * 
     * @param object
     * @return
     * @throws ServiceException
     * @since VFC 1.0
     */
    @Override
    public JSONObject grantResource(JSONObject object) throws ServiceException {
        LOGGER.info("function=grantResource; object: {}", object.toString());
        JSONObject additionalparam = object.getJSONObject("additionalParam");
        String vimId = additionalparam.getString(ParamConstant.PARAM_VIMID);
        JSONObject vimJson = VimUtil.getVimById(vimId);
        String tenant = vimJson.getString(ParamConstant.PARAM_TENANT);
        JSONObject accessinfo = new JSONObject();
        accessinfo.put(ParamConstant.PARAM_TENANT, tenant);
        JSONObject vim = new JSONObject();
        vim.put(ParamConstant.PARAM_VIMID, vimId);
        vim.put("accessinfo", accessinfo);
        LOGGER.info("function=grantResource; vim: {}", vim.toString());
        JSONObject result = new JSONObject();
        result.put("vim", vim);
        return result;
    }

    @Override
    public JSONObject grantResourceReal(JSONObject object) throws ServiceException {
        LOGGER.info("function=grantResource; object: {}", object.toString());
        String vimId = object.getString(ParamConstant.PARAM_VIMID);
        JSONObject vimJson = VimUtil.getVimById(vimId);
        JSONObject vim = parseVim(vimJson);
        String resType = "";
        JSONArray resArr = new JSONArray();
        if(object.containsKey(ADD_RESOURCE)) {
            resType = ADD_RESOURCE;
            resArr = parseResource(object, resType);
        } else if(object.containsKey(REMOVE_RESOURCE)) {
            resType = REMOVE_RESOURCE;
            resArr = parseResource(object, resType);
        }
        JSONObject resInfo = getResInfo(object, resType);
        boolean compareResult = queryLimitsAndCompare(vimId, resInfo);
        JSONObject result = new JSONObject();
        if(!compareResult) {
            result.put("message", "Resource is not enough, grant resource failed.");
            return result;
        }
        result.put("vim", vim);
        result.put("zone", "");
        result.put("zoneGroup", "");
        result.put(resType, resArr);
        result.put("tempResource", "");
        result.put("updateResource", "");
        result.put("vimAssets", new JSONObject());
        result.put("additionalParam", "");
        LOGGER.info("function=grantResource; result: {}", result.toString());
        return result;
    }

    private boolean queryLimitsAndCompare(String vimId, JSONObject resInfo) throws ServiceException {
        LimitsBusiness limitsBusiness = new LimitsBusinessImpl();
        JSONObject limits = limitsBusiness.getLimits(vimId);
        LOGGER.info("function=QueryLimitsAndCompare; limits: {}", limits);
        JSONObject availableResourse = getAvailableFromLimits(limits);
        return compareResourceWithLimits(resInfo, availableResourse);
    }

    private boolean compareResourceWithLimits(JSONObject resInfo, JSONObject availableResourse) {
        LOGGER.info("function=compareResourceWithLimits; resInfo: {}, availableResourse: {}", resInfo,
                availableResourse);
        if(Float.parseFloat(resInfo.getString(ParamConstant.USED_CPU))
                - Float.parseFloat(availableResourse.getString("availableCPU")) > 0) {
            LOGGER.info("function=compareResourceWithLimits; CPU is not enough.");
            return false;
        }
        if(Float.parseFloat(resInfo.getString(ParamConstant.USED_MEMORY))
                - Float.parseFloat(availableResourse.getString("availableMemory")) > 0) {
            LOGGER.info("function=compareResourceWithLimits; Memory is not enough.");
            return false;
        }
        if(Float.parseFloat(resInfo.getString(ParamConstant.USED_DISK))
                - Float.parseFloat(availableResourse.getString("availableDisk")) > 0) {
            LOGGER.info("function=compareResourceWithLimits; Disk is not enough.");
            return false;
        }

        return true;
    }

    private JSONObject getAvailableFromLimits(JSONObject limits) {
        JSONObject available = new JSONObject();
        String availableCPU = String.valueOf(Float.parseFloat(limits.getString("totalCPU"))
                - Float.parseFloat(limits.getString(ParamConstant.USED_CPU)));
        String availableMemory = String.valueOf(Float.parseFloat(limits.getString("totalMemory"))
                - Float.parseFloat(limits.getString(ParamConstant.USED_MEMORY)));
        String availableDisk = String.valueOf(Float.parseFloat(limits.getString("totalDisk"))
                - Float.parseFloat(limits.getString(ParamConstant.USED_DISK)));
        available.put("availableCPU", availableCPU);
        available.put("availableMemory", availableMemory);
        available.put("availableDisk", availableDisk);
        return available;
    }

    private JSONObject getResInfo(JSONObject object, String type) {
        JSONArray arr = object.getJSONArray(type);
        LOGGER.info("function=getResInfo; arr: {}, type: {}", arr, type);
        JSONObject resourceObj = new JSONObject();
        if(ADD_RESOURCE.equals(type)) {
            resourceObj = getGrantResource(arr);
            resourceObj.put("action", "online");
        } else if(REMOVE_RESOURCE.equals(type)) {
            resourceObj = getGrantResource(arr);
            resourceObj.put("action", "offline");
        }
        LOGGER.info("function=getResInfo; resutl: {}", resourceObj.toString());
        return resourceObj;
    }

    /**
     * <br>
     * 
     * @param addResource
     * @return
     * @since VFC 1.0
     */
    private JSONObject getGrantResource(JSONArray resource) {
        int cpuNum = 0;
        int memNum = 0;
        int diskNum = 0;
        for(int i = 0; i < resource.size(); i++) {
            JSONObject res = resource.getJSONObject(i);
            JSONObject vCpu = res.getJSONObject(RESOURCE_TEMPLATE).getJSONObject("virtualComputeDescriptor")
                    .getJSONObject("virtualCpu");
            int vCpuNum = vCpu.getInt("numVirtualCpu");
            JSONObject vMem = res.getJSONObject(RESOURCE_TEMPLATE).getJSONObject("virtualComputeDescriptor")
                    .getJSONObject("virtualMemory");
            int vMemNum = vMem.getInt("virtualMemSize");
            JSONObject vDisk = res.getJSONObject(RESOURCE_TEMPLATE).getJSONObject("virtualStorageDescriptor");
            int vDiskNum = vDisk.getInt("sizeOfStorage");
            cpuNum = cpuNum + vCpuNum;
            memNum = memNum + vMemNum;
            diskNum = diskNum + vDiskNum;
        }
        JSONObject obj = new JSONObject();
        obj.put(ParamConstant.USED_CPU, String.valueOf(cpuNum));
        obj.put(ParamConstant.USED_MEMORY, String.valueOf(memNum));
        obj.put(ParamConstant.USED_DISK, String.valueOf(diskNum));
        return obj;
    }

    /**
     * <br>
     * 
     * @param object
     * @return
     * @since VFC 1.0
     */
    private JSONArray parseResource(JSONObject object, String key) {
        JSONArray newResources = new JSONArray();
        JSONArray oldResource = object.getJSONArray(key);
        LOGGER.info("function=parseResource; Resource: {}", oldResource.toString());
        for(int i = 0; i < oldResource.size(); i++) {
            JSONObject res = oldResource.getJSONObject(i);
            JSONObject obj = new JSONObject();
            obj.put("reservationId", "");
            obj.put("resourceProviderId", "");
            obj.put("zoneId", "");
            obj.put(ParamConstant.PARAM_VIMID, object.getString(ParamConstant.PARAM_VIMID));
            obj.put("resourceDefinitionId", res.getString("resourceDefinitionId"));
            newResources.add(obj);
        }
        LOGGER.info("function=parseResource; Parse Resource result: {}", newResources.toString());
        return newResources;
    }

    /**
     * <br>
     * 
     * @param vimJson
     * @return
     * @since VFC 1.0
     */
    private JSONObject parseVim(JSONObject vimJson) {
        LOGGER.info("function=grantResource; vimJson: {}", vimJson.toString());
        JSONObject interfaceInfo = new JSONObject();
        interfaceInfo.put("vimType", vimJson.getString("type"));
        interfaceInfo.put("apiVersion", "v2");
        interfaceInfo.put("protocolType", "http");
        JSONObject accessInfo = new JSONObject();
        accessInfo.put(ParamConstant.PARAM_TENANT, vimJson.getString(ParamConstant.PARAM_TENANT));
        accessInfo.put("username", vimJson.getString("userName"));
        accessInfo.put("password", vimJson.getString("password"));
        JSONObject vim = new JSONObject();
        vim.put("vimInfoId", vimJson.getString(ParamConstant.PARAM_VIMID));
        vim.put(ParamConstant.PARAM_VIMID, vimJson.getString(ParamConstant.PARAM_VIMID));
        vim.put("interfaceInfo", interfaceInfo);
        vim.put("accessInfo", accessInfo);
        vim.put("interfaceEndpoint", vimJson.getString("url"));
        return vim;
    }

}