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.base.openstack.impl;
19 import java.math.BigDecimal;
20 import java.util.HashMap;
21 import java.util.List;
23 import java.util.UUID;
25 import org.apache.commons.lang.StringUtils;
26 import org.apache.logging.log4j.LogManager;
27 import org.apache.logging.log4j.Logger;
28 import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
29 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
30 import org.onap.vfc.nfvo.resmanagement.common.util.JsonUtil;
31 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Sites;
32 import org.onap.vfc.nfvo.resmanagement.service.business.inf.LimitsBusiness;
33 import org.onap.vfc.nfvo.resmanagement.service.business.inf.SitesBusiness;
34 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
35 import org.onap.vfc.nfvo.resmanagement.service.group.inf.ResOperateService;
36 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
38 import net.sf.json.JSONObject;
41 * DC info interface.<br/>
46 * @version VFC 1.0 Aug 24, 2016
48 public class SitesImpl implements Sites {
50 private static final Logger LOGGER = LogManager.getLogger(SitesImpl.class);
52 private SitesBusiness sitesBusiness;
54 private LimitsBusiness limitsBusiness;
56 private ResOperateService resOperateService;
59 public int add(JSONObject jsonObject) throws ServiceException {
60 LOGGER.info("Add datacenter jsonObject: {}", jsonObject);
61 SitesEntity sitesEntity = SitesEntity.toEntity(jsonObject);
62 sitesEntity.setStatus(ParamConstant.PARAM_ACTIVE);
63 String vimName = jsonObject.getString(ParamConstant.PARAM_VIMNAME);
64 String vimId = VimUtil.getVimIdByName(vimName);
65 sitesEntity.setVimId(vimId);
66 JSONObject resource = limitsBusiness.getLimits(vimId);
67 sitesEntity.setVimName(resource.getString(ParamConstant.PARAM_VIMNAME));
68 sitesEntity.setTotalCPU(resource.getString(ParamConstant.TOTAL_CPU));
69 sitesEntity.setUsedCPU(resource.getString(ParamConstant.USED_CPU));
70 sitesEntity.setTotalMemory(resource.getString(ParamConstant.TOTAL_MEMORY));
71 sitesEntity.setUsedMemory(resource.getString(ParamConstant.USED_MEMORY));
72 sitesEntity.setTotalDisk(resource.getString(ParamConstant.TOTAL_DISK));
73 sitesEntity.setUsedDisk(resource.getString(ParamConstant.USED_DISK));
74 if(StringUtils.isEmpty(sitesEntity.getId())) {
75 sitesEntity.setId(UUID.randomUUID().toString());
76 jsonObject.put(ParamConstant.PARAM_ID, sitesEntity.getId());
78 LOGGER.info("Add datacenter sitesEntity: {}", sitesEntity.toString());
79 return sitesBusiness.addSite(sitesEntity);
86 * @throws ServiceException
90 public void sendToMonitor(JSONObject jsonObject) throws ServiceException {
91 LOGGER.info("SitesImpl sendToMonitor jsonObject: {}", jsonObject);
92 String vimName = jsonObject.getString(ParamConstant.PARAM_VIMNAME);
93 String vimId = VimUtil.getVimIdByName(vimName);
94 JSONObject vimInfo = VimUtil.getVimById(vimId);
95 LOGGER.info("SitesImpl sendToMonitor vimInfo: {}", vimInfo);
96 String tenant = vimInfo.getString("tenant");
97 String tenantId = VimUtil.getTenantIdByName(tenant, vimId);
98 JSONObject json = new JSONObject();
99 json.put("header", null);
100 LOGGER.info("tenantId:{}, vimId:{}", tenantId, vimId);
101 resOperateService.addIRes(tenantId, vimId, json);
102 resOperateService.sendMsgMonitor("create", vimId);
106 public int update(SitesEntity sitesEntity) throws ServiceException {
107 return sitesBusiness.updateSiteSelective(sitesEntity);
111 public int update(JSONObject jsonObject) throws ServiceException {
112 LOGGER.info("grantResource jsonObject: {}", jsonObject);
113 JSONObject sitesObj = dataParse(jsonObject);
114 return sitesBusiness.updateSiteSelective(SitesEntity.toEntity(sitesObj));
117 private JSONObject dataParse(JSONObject jsonObject) throws ServiceException {
118 String vimId = jsonObject.getString(ParamConstant.PARAM_VIMID);
119 Map<String, Object> condition = new HashMap<>();
120 condition.put("vimId", vimId);
121 SitesEntity sitesEntity = get(condition);
122 if(null == sitesEntity) {
123 LOGGER.error("Get sites null, vimId={}", vimId);
126 return computeSiteUsed(jsonObject, sitesEntity);
129 private JSONObject computeSiteUsed(JSONObject jsonObject, SitesEntity sitesEntity) throws ServiceException {
130 String action = JsonUtil.getJsonFieldStr(jsonObject, "action");
131 String usedCpu = JsonUtil.getJsonFieldStr(jsonObject, ParamConstant.USED_CPU);
132 String usedMemory = JsonUtil.getJsonFieldStr(jsonObject, ParamConstant.USED_MEMORY);
133 String usedDisk = JsonUtil.getJsonFieldStr(jsonObject, ParamConstant.USED_DISK);
134 String oldCpu = sitesEntity.getUsedCPU();
135 String oldMemory = sitesEntity.getUsedMemory();
136 String oldDisk = sitesEntity.getUsedDisk();
137 String newCpu = accumOrFreeRes(usedCpu, oldCpu, action, sitesEntity.getTotalCPU(), "cpu");
138 String newMemory = accumOrFreeRes(usedMemory, oldMemory, action, sitesEntity.getTotalMemory(), "memory");
139 String newDisk = accumOrFreeRes(usedDisk, oldDisk, action, sitesEntity.getTotalDisk(), "disk");
141 JSONObject resUsed = new JSONObject();
142 resUsed.put(ParamConstant.USED_CPU, newCpu);
143 resUsed.put(ParamConstant.USED_MEMORY, newMemory);
144 resUsed.put(ParamConstant.USED_DISK, newDisk);
145 resUsed.put("id", sitesEntity.getId());
146 resUsed.put("name", sitesEntity.getName());
147 resUsed.put("status", sitesEntity.getStatus());
148 resUsed.put("location", sitesEntity.getLocation());
149 resUsed.put("country", sitesEntity.getCountry());
150 resUsed.put(ParamConstant.PARAM_VIMID, sitesEntity.getVimId());
151 resUsed.put(ParamConstant.PARAM_VIMNAME, sitesEntity.getVimName());
152 resUsed.put(ParamConstant.TOTAL_CPU, sitesEntity.getTotalCPU());
153 resUsed.put(ParamConstant.TOTAL_MEMORY, sitesEntity.getTotalMemory());
154 resUsed.put(ParamConstant.TOTAL_DISK, sitesEntity.getTotalDisk());
158 private String accumOrFreeRes(String resUsed, String resOld, String action, String total, String type)
159 throws ServiceException {
160 BigDecimal iResUsed = new BigDecimal(resUsed);
161 BigDecimal iResOld = new BigDecimal(resOld);
162 BigDecimal itotal = new BigDecimal(total);
163 if("online".equals(action)) {
164 if(iResOld.add(iResUsed).compareTo(itotal) > 0) {
165 throw new ServiceException("Grant resource fail! The " + type + " resource not enough.");
167 return String.valueOf(iResOld.add(iResUsed));
169 if(iResOld.subtract(iResUsed).compareTo(BigDecimal.ZERO) < 0) {
170 throw new ServiceException("Grant resource fail! The " + type + " resource used below zero.");
172 return String.valueOf(iResOld.subtract(iResUsed));
177 public int updateResource(JSONObject jsonObject) throws ServiceException {
178 return sitesBusiness.updateSiteResource(SitesEntity.toEntity(jsonObject));
182 public int delete(String id) throws ServiceException {
183 Map<String, Object> map = new HashMap<String, Object>(10);
184 map.put(ParamConstant.PARAM_ID, id);
185 List<SitesEntity> datacenters = getList(map);
186 SitesEntity site = datacenters.get(0);
187 LOGGER.info("site: {}", site);
188 String vimId = site.getVimId();
189 LOGGER.info("vimId: {}", vimId);
190 resOperateService.sendMsgMonitor("delete", vimId);
191 resOperateService.deleteIRes(vimId);
192 return sitesBusiness.deleteSite(id);
196 public int updateStatusByVimId(JSONObject jsonObject) throws ServiceException {
197 return sitesBusiness.updateSiteByVimId(SitesEntity.toEntity(jsonObject));
201 public List<SitesEntity> getList(Map<String, Object> condition) throws ServiceException {
202 return sitesBusiness.getSites(condition);
206 public SitesEntity get(Map<String, Object> condition) throws ServiceException {
207 List<SitesEntity> siteList = sitesBusiness.getSites(condition);
208 if(null == siteList || siteList.isEmpty()) {
211 return siteList.get(0);
215 public int deleteResByVimId(String vimId) throws ServiceException {
219 public void setSitesBusiness(SitesBusiness sitesBusiness) {
220 this.sitesBusiness = sitesBusiness;
223 public void setLimitsBusiness(LimitsBusiness limitsBusiness) {
224 this.limitsBusiness = limitsBusiness;
227 public void setResOperateService(ResOperateService resOperateService) {
228 this.resOperateService = resOperateService;