4a8a633ca74d94ce7b304f5f785826cc0b8084e7
[vfc/nfvo/resmanagement.git] /
1 /*
2  * Copyright 2016 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.openo.nfvo.resmanagement.service.group.impl;
18
19 import org.openo.baseservice.remoteservice.exception.ServiceException;
20 import org.openo.nfvo.resmanagement.common.VimUtil;
21 import org.openo.nfvo.resmanagement.service.base.openstack.inf.Sites;
22 import org.openo.nfvo.resmanagement.service.group.inf.GrantResService;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import net.sf.json.JSONArray;
27 import net.sf.json.JSONObject;
28
29 /**
30  * <br>
31  * <p>
32  * </p>
33  * 
34  * @author
35  * @version NFVO 0.5 Oct 29, 2016
36  */
37 public class GrantResServiceImpl implements GrantResService {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(GrantResServiceImpl.class);
40
41     private Sites sites;
42
43     /**
44      * <br>
45      * 
46      * @param object
47      * @return
48      * @throws ServiceException
49      * @since NFVO 0.5
50      */
51     @Override
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);
66         return result;
67     }
68
69     @Override
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);
75         String resType = "";
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);
83         }
84         JSONObject resInfo = getResInfo(object, resType);
85         resInfo.put("vimId", vimId);
86         sites.update(resInfo);
87
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());
98         return result;
99     }
100
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");
111         }
112         LOGGER.info("function=getResInfo; resutl: {}", resourceObj.toString());
113         return resourceObj;
114     }
115
116     /**
117      * <br>
118      * 
119      * @param addResource
120      * @return
121      * @since NFVO 0.5
122      */
123     private JSONObject getGrantResource(JSONArray resource) {
124         int cpuNum = 0;
125         int memNum = 0;
126         int diskNum = 0;
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;
140         }
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));
145         return obj;
146     }
147
148     /**
149      * <br>
150      * 
151      * @param object
152      * @return
153      * @since NFVO 0.5
154      */
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);
168         }
169         LOGGER.info("function=parseResource; Parse Resource result: {}", newResources.toString());
170         return newResources;
171     }
172
173     /**
174      * <br>
175      * 
176      * @param vimJson
177      * @return
178      * @since NFVO 0.5
179      */
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"));
196         return vim;
197     }
198
199     /**
200      * @param sites The sites to set.
201      */
202     public void setSites(Sites sites) {
203         this.sites = sites;
204     }
205
206 }