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.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
27 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
28 import org.onap.vfc.nfvo.resmanagement.common.util.JsonUtil;
29 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Location;
30 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Sites;
31 import org.onap.vfc.nfvo.resmanagement.service.business.inf.LocationBusiness;
32 import org.onap.vfc.nfvo.resmanagement.service.entity.LocationEntity;
33 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
34 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
36 import net.sf.json.JSONArray;
37 import net.sf.json.JSONObject;
40 * Location Implementation Class.<br>
45 * @version VFC 1.0 Sep 10, 2016
47 public class LocationImpl implements Location {
49 private LocationBusiness locationBusiness;
53 private static final Logger LOGGER = LogManager.getLogger(LocationImpl.class);
56 public int add(JSONObject jsonObject) throws ServiceException {
57 return locationBusiness.addLocation(LocationEntity.toEntity(jsonObject));
61 public int update(JSONObject jsonObject) throws ServiceException {
62 return locationBusiness.updateLocationSelective(LocationEntity.toEntity(jsonObject));
66 public int delete(String location) throws ServiceException {
67 return locationBusiness.deleteLocation(location);
71 public Map<String, Object> get(String id) throws ServiceException {
72 Map<String, Object> map = new HashMap<>();
73 map.put(ParamConstant.PARAM_ID, id);
74 List<LocationEntity> sodores = locationBusiness.getLocations(map);
76 map.put(ParamConstant.PARAM_DATA, sodores);
81 public List<LocationEntity> get(Map<String, Object> condition) throws ServiceException {
82 return locationBusiness.getLocations(condition);
86 public List<String> getCountry() throws ServiceException {
87 return locationBusiness.getCountry();
91 public List<String> getCloudservice() throws ServiceException {
92 LOGGER.info("get cloud service from external system");
93 JSONArray vims = VimUtil.getVims();
94 LOGGER.info("vims: " + vims.toString());
95 List<String> cloudService = new ArrayList<>();
96 for(int i = 0; i < vims.size(); i++) {
97 String vimName = vims.getJSONObject(i).getString("name");
98 cloudService.add(vimName);
104 public List<String> getLocationByCountry(Map<String, Object> condition) throws ServiceException {
105 return locationBusiness.getLocationByCountry(condition);
109 public LocationEntity getLocation(Map<String, Object> condition) throws ServiceException {
110 List<LocationEntity> locationlist = locationBusiness.getLocations(condition);
111 if(null == locationlist || locationlist.isEmpty()) {
114 return locationlist.get(0);
118 public List<JSONObject> getLocationInfo(List<LocationEntity> locationInfo) throws ServiceException {
119 ArrayList<JSONObject> newSites = new ArrayList<>();
120 Map<String, Object> condition = new HashMap<>();
121 for(int i = 0; i < locationInfo.size(); i++) {
122 LocationEntity locationEntity = locationInfo.get(i);
123 String latitude = locationEntity.getLatitude();
124 String longitude = locationEntity.getLongitude();
125 String locatSite = locationEntity.getLocation();
126 condition.put(ParamConstant.PARAM_LOCATION, locatSite);
127 LOGGER.info("LocationRoa::getLocation condition:{}", condition);
128 List<SitesEntity> sitesEntity = sites.getList(condition);
129 LOGGER.info("LocationRoa::getLocation sitesEntity:{}", sitesEntity);
130 if(null != sitesEntity && !sitesEntity.isEmpty()) {
131 for(SitesEntity entity : sitesEntity) {
132 JSONObject site = JSONObject.fromObject(entity.toString());
133 JSONObject resTotalJo = site.getJSONObject("total");
134 JSONObject resUsedlJo = site.getJSONObject("used");
135 JSONObject ressiteJo = computingSite(resTotalJo, resUsedlJo);
136 site.element("latitude", latitude);
137 site.element("longitude", longitude);
138 site.element("siteDetail", ressiteJo);
139 LOGGER.info("LocationRoa::getLocation latitude:{}, longitude:{}", latitude, longitude);
140 LOGGER.info("LocationRoa::getLocation site:{}", site);
149 * Computing site.<br>
156 public JSONObject computingSite(JSONObject total, JSONObject used) {
157 String vcpus = JsonUtil.getJsonFieldStr(total, ParamConstant.PARAM_VCPUS);
158 String memorys = JsonUtil.getJsonFieldStr(total, ParamConstant.PARAM_MEMORY);
159 String disks = JsonUtil.getJsonFieldStr(total, ParamConstant.PARAM_DISK);
160 String vcpusused = JsonUtil.getJsonFieldStr(used, ParamConstant.PARAM_VCPUS);
161 String memoryused = JsonUtil.getJsonFieldStr(used, ParamConstant.PARAM_MEMORY);
162 String diskused = JsonUtil.getJsonFieldStr(used, ParamConstant.PARAM_DISK);
163 float cpu = Float.parseFloat(vcpusused) / Float.parseFloat(vcpus);
164 float memory = Float.parseFloat(memoryused) / Integer.parseInt(memorys);
165 float disk = Float.parseFloat(diskused) / Float.parseFloat(disks);
166 JSONObject resTotalJo = new JSONObject();
167 resTotalJo.put(ParamConstant.PARAM_VCPUS, String.valueOf(Math.round(cpu * 100)) + "%");
168 resTotalJo.put(ParamConstant.PARAM_MEMORY, String.valueOf(Math.round(memory * 100)) + "%");
169 resTotalJo.put(ParamConstant.PARAM_DISK, String.valueOf(Math.round(disk * 100)) + "%");
174 public LocationBusiness getLocationBusiness() {
175 return locationBusiness;
178 public void setLocationBusiness(LocationBusiness locationBusiness) {
179 this.locationBusiness = locationBusiness;
182 public void setSites(Sites sites) {