d083f33ea0665f744a91e9123ad597fa93d82644
[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.base.openstack.impl;
18
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.List;
22 import java.util.Map;
23
24 import org.onap.vfc.nfvo.resmanagement.common.VimUtil;
25 import org.onap.vfc.nfvo.resmanagement.common.constant.ParamConstant;
26 import org.onap.vfc.nfvo.resmanagement.common.util.JsonUtil;
27 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Location;
28 import org.onap.vfc.nfvo.resmanagement.service.base.openstack.inf.Sites;
29 import org.onap.vfc.nfvo.resmanagement.service.business.inf.LocationBusiness;
30 import org.onap.vfc.nfvo.resmanagement.service.entity.LocationEntity;
31 import org.onap.vfc.nfvo.resmanagement.service.entity.SitesEntity;
32 import org.onap.vfc.nfvo.resmanagement.common.util.restclient.ServiceException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 import net.sf.json.JSONArray;
37 import net.sf.json.JSONObject;
38
39 /**
40  * Location Implementation Class.<br>
41  * <p>
42  * </p>
43  *
44  * @author
45  * @version NFVO 0.5 Sep 10, 2016
46  */
47 public class LocationImpl implements Location {
48
49     private LocationBusiness locationBusiness;
50
51     private Sites sites;
52
53     private static final Logger LOGGER = LoggerFactory.getLogger(LocationImpl.class);
54
55     @Override
56     public int add(JSONObject jsonObject) throws ServiceException {
57         return locationBusiness.addLocation(LocationEntity.toEntity(jsonObject));
58     }
59
60     @Override
61     public int update(JSONObject jsonObject) throws ServiceException {
62         return locationBusiness.updateLocationSelective(LocationEntity.toEntity(jsonObject));
63     }
64
65     @Override
66     public int delete(String location) throws ServiceException {
67         return locationBusiness.deleteLocation(location);
68     }
69
70     @Override
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);
75         map.clear();
76         map.put(ParamConstant.PARAM_DATA, sodores);
77         return map;
78     }
79
80     @Override
81     public List<LocationEntity> get(Map<String, Object> condition) throws ServiceException {
82         return locationBusiness.getLocations(condition);
83     }
84
85     @Override
86     public List<String> getCountry() throws ServiceException {
87         return locationBusiness.getCountry();
88     }
89
90     @Override
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);
99         }
100         return cloudService;
101     }
102
103     @Override
104     public List<String> getLocationByCountry(Map<String, Object> condition) throws ServiceException {
105         return locationBusiness.getLocationByCountry(condition);
106     }
107
108     @Override
109     public LocationEntity getLocation(Map<String, Object> condition) throws ServiceException {
110         List<LocationEntity> locationlist = locationBusiness.getLocations(condition);
111         if(null == locationlist || locationlist.isEmpty()) {
112             return null;
113         }
114         return locationlist.get(0);
115     }
116
117     @Override
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);
141                     newSites.add(site);
142                 }
143             }
144         }
145         return newSites;
146     }
147
148     /**
149      * Computing site.<br>
150      *
151      * @param total
152      * @param used
153      * @return
154      * @since NFVO 0.5
155      */
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)) + "%");
170
171         return resTotalJo;
172     }
173
174     public LocationBusiness getLocationBusiness() {
175         return locationBusiness;
176     }
177
178     public void setLocationBusiness(LocationBusiness locationBusiness) {
179         this.locationBusiness = locationBusiness;
180     }
181
182     public void setSites(Sites sites) {
183         this.sites = sites;
184     }
185
186 }