1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021-2022 Wipro Limited.
6 * Copyright (C) 2022 Huawei Canada Limited.
7 * ==============================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 *******************************************************************************/
23 package org.onap.slice.analysis.ms.aai;
25 import com.fasterxml.jackson.databind.ObjectMapper;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.Objects;
33 import org.json.JSONArray;
34 import org.json.JSONObject;
35 import org.onap.slice.analysis.ms.models.Configuration;
36 import org.onap.slice.analysis.ms.models.aai.ServiceInstance;
37 import org.onap.slice.analysis.ms.restclients.AaiRestClient;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.beans.factory.annotation.Autowired;
41 import org.springframework.core.ParameterizedTypeReference;
42 import org.springframework.http.ResponseEntity;
43 import org.springframework.stereotype.Service;
47 * Service for AAI interfaces
51 public class AaiService implements AaiInterface {
53 private static Logger log = LoggerFactory.getLogger(Configuration.class);
56 AaiRestClient restclient;
58 private static ObjectMapper objectMapper = new ObjectMapper();
59 private static String globalSubscriberId;
60 private static String subscriptionServiceType;
61 private static String aaiBaseUrl = Configuration.getInstance().getAaiUrl();
64 * Fetches the details of a service
66 * @param snssai SNSSAI ID
67 * @return responseMap contains service details
69 public Map<String, String> fetchServiceDetails(String snssai) {
70 Map<String, String> responseMap = fetchSubscriberAndSubscriptionServiceType();
71 String serviceReqUrl = aaiBaseUrl + "/business/customers/customer/" + globalSubscriberId
72 + "/service-subscriptions/service-subscription/" + subscriptionServiceType + "/service-instances";
73 log.info("serviceReqUrl {}", serviceReqUrl);
74 String serviceRole = "AN-NF";
76 String serviceInstance =
77 restclient.sendGetRequest(serviceReqUrl, new ParameterizedTypeReference<String>() {}).getBody();
78 JSONObject serviceInstanceJson = new JSONObject(serviceInstance);
79 JSONArray serviceInstanceList = serviceInstanceJson.getJSONArray("service-instance");
80 for (int i = 0; i < serviceInstanceList.length(); i++) {
81 JSONObject serviceObj = serviceInstanceList.getJSONObject(i);
82 if (serviceObj.has("environment-context") && serviceObj.getString("environment-context").equalsIgnoreCase(snssai)) {
83 responseMap.put("sliceProfileId", serviceObj.getString("service-instance-id"));
87 String serviceRoleReqUrl = aaiBaseUrl + "/business/customers/customer/" + globalSubscriberId
88 + "/service-subscriptions/service-subscription/" + subscriptionServiceType
89 + "/service-instances/?service-role=nssi&depth=2";
90 log.info("serviceRoleReqUrl {}", serviceRoleReqUrl);
91 String serviceInstanceForServiceRole =
92 restclient.sendGetRequest(serviceRoleReqUrl, new ParameterizedTypeReference<String>() {}).getBody();
93 JSONObject serviceInstanceForServiceRoleJson = new JSONObject(serviceInstanceForServiceRole);
94 JSONArray serviceInstanceListForServiceRole =
95 serviceInstanceForServiceRoleJson.getJSONArray("service-instance");
96 for (int i = 0; i < serviceInstanceListForServiceRole.length(); i++) {
97 JSONObject serviceObj = serviceInstanceListForServiceRole.getJSONObject(i);
98 if (serviceObj.has("workload-context") && serviceObj.getString("workload-context").trim().equalsIgnoreCase(serviceRole)) {
99 responseMap.put("ranNFNSSIId", serviceObj.getString("service-instance-id"));
102 } catch (Exception e) {
103 log.info("Exception while fetching serviceDetails: " + e);
105 responseMap.put("sNSSAI", snssai);
106 log.info("subscriber details: " + responseMap);
111 * Fetches the current configuration of a Slice from AAI
113 * @param snssai SNSSAI ID
114 * @return responseMap contains slice configuration
116 public Map<String, Integer> fetchCurrentConfigurationOfSlice(String snssai) {
117 log.info("AAI fetch config Slice: " + aaiBaseUrl);
118 String serviceInstaneId = null;
119 String serviceReqUrl = aaiBaseUrl + "/business/customers/customer/" + globalSubscriberId
120 + "/service-subscriptions/service-subscription/" + subscriptionServiceType + "/service-instances";
121 log.info("serviceReqUrl {}", serviceReqUrl);
122 Map<String, Integer> responseMap = new HashMap<String, Integer>();
124 String serviceInstance =
125 restclient.sendGetRequest(serviceReqUrl, new ParameterizedTypeReference<String>() {}).getBody();
126 JSONObject serviceInstanceJson = new JSONObject(serviceInstance);
128 JSONArray serviceInstanceList = serviceInstanceJson.getJSONArray("service-instance");
129 for (int i = 0; i < serviceInstanceList.length(); i++) {
130 JSONObject serviceObj = serviceInstanceList.getJSONObject(i);
131 if (serviceObj.has("environment-context") && serviceObj.getString("environment-context").equalsIgnoreCase(snssai)) {
132 serviceInstaneId = serviceObj.getString("service-instance-id");
136 String sliceProfileReqUrl = aaiBaseUrl + "/business/customers/customer/" + globalSubscriberId
137 + "/service-subscriptions/service-subscription/" + subscriptionServiceType
138 + "/service-instances/service-instance/" + serviceInstaneId + "/slice-profiles";
139 log.info("sliceProfileReqUrl {}", sliceProfileReqUrl);
140 String sliceProfile = restclient
141 .sendGetRequest(sliceProfileReqUrl, new ParameterizedTypeReference<String>() {}).getBody();
142 JSONObject sliceProfileJson = new JSONObject(sliceProfile);
143 JSONArray sliceProfileList = sliceProfileJson.getJSONArray("slice-profile");
144 for (int i = 0; i < sliceProfileList.length(); i++) {
145 JSONObject sliceProfileObj = sliceProfileList.getJSONObject(i);
146 responseMap.put("dLThptPerSlice", sliceProfileObj.getInt("exp-data-rate-UL"));
147 responseMap.put("uLThptPerSlice", sliceProfileObj.getInt("exp-data-rate-DL"));
150 log.info("Slice configuration: " + responseMap);
151 } catch (Exception e) {
152 log.info("AAI Slice: " + e);
158 * Fetches the details of a subscriber and subscription Service
160 * @return responseMap contains details of subscriber and subscription service
162 public Map<String, String> fetchSubscriberAndSubscriptionServiceType() {
164 Map<String, String> responseMap = new HashMap<String, String>();
166 log.info("Get GlobalSubscriberId");
167 String subscriberReqUrl = aaiBaseUrl + "/business/customers";
168 log.info("subscriberReqUrl {}", subscriberReqUrl);
170 String subscriberReq =
171 restclient.sendGetRequest(subscriberReqUrl, new ParameterizedTypeReference<String>() {}).getBody();
172 JSONObject subscriberReqJson = new JSONObject(subscriberReq);
173 JSONArray subscriberReqJsonList = subscriberReqJson.getJSONArray("customer");
174 for (int i = 0; i < subscriberReqJsonList.length(); i++) {
175 JSONObject subscriberReqObj = subscriberReqJsonList.getJSONObject(i);
176 globalSubscriberId = subscriberReqObj.getString("global-customer-id");
177 responseMap.put("globalSubscriberId", globalSubscriberId);
181 log.info("Get subscriptionServiceType");
182 String subscriptionServiceReqUrl =
183 aaiBaseUrl + "/business/customers/customer/" + globalSubscriberId + "/service-subscriptions";
184 log.info("subscriptionServiceReqUrl: {}", subscriptionServiceReqUrl);
185 String subscriptionService = restclient
186 .sendGetRequest(subscriptionServiceReqUrl, new ParameterizedTypeReference<String>() {}).getBody();
187 JSONObject subscriptionServiceJson = new JSONObject(subscriptionService);
188 JSONArray subscriptionServiceListJson = subscriptionServiceJson.getJSONArray("service-subscription");
189 for (int i = 0; i < subscriptionServiceListJson.length(); i++) {
190 JSONObject subscriptionServiceObj = subscriptionServiceListJson.getJSONObject(i);
191 subscriptionServiceType = subscriptionServiceObj.getString("service-type");
192 responseMap.put("subscriptionServiceType", subscriptionServiceType);
195 } catch (Exception e) {
196 log.info("Exception while fetching subscriber and subscription: " + e);
199 log.info("responseMap: " + responseMap);
205 * Fetches the SNSSIs of a serviceInstanceId
207 * @param serviceInstanceId service instance ID
208 * @return snssaiList contains list of SNSSAIs
210 public List<String> getSnssaiList(String sliceInstanceId) {
211 fetchSubscriberAndSubscriptionServiceType();
212 List<String> allotedResource = new ArrayList<>();
213 List<String> sliceProfileList = new ArrayList<>();
214 List<String> snssaiList = new ArrayList<>();
216 log.info("fetch slice instance details");
217 String serviceReqUrl = aaiBaseUrl + "/business/customers/customer/" + globalSubscriberId
218 + "/service-subscriptions/service-subscription/" + subscriptionServiceType
219 + "/service-instances/service-instance/" + sliceInstanceId;
220 log.info("serviceReqUrl {}", serviceReqUrl);
222 String serviceInstanceString =
223 restclient.sendGetRequest(serviceReqUrl, new ParameterizedTypeReference<String>() {}).getBody();
224 ServiceInstance serviceInstance = objectMapper.readValue(serviceInstanceString, ServiceInstance.class);
225 if (serviceInstance.getServiceRole().equalsIgnoreCase("nsi")) {
226 serviceInstance.getRelationshipList().getRelationship().forEach(relationship -> {
227 if (Objects.nonNull(relationship.getRelatedTo()) && relationship.getRelatedTo().equalsIgnoreCase("allotted-resource")) {
228 relationship.getRelationshipData().forEach(data -> {
229 if (data.get("relationship-key").equalsIgnoreCase("service-instance.service-instance-id")) {
230 allotedResource.add(data.get("relationship-value"));
237 return fetchSnssaiOfSliceProfile(fetchSliceProfilesOfAllotedResourceData(allotedResource));
240 if (serviceInstance.getServiceRole().equalsIgnoreCase("nssi")) {
241 serviceInstance.getRelationshipList().getRelationship().forEach(relationship -> {
242 if (Objects.nonNull(relationship.getRelatedToProperty())) {
243 relationship.getRelatedToProperty().forEach(property -> {
244 if (property.get("property-value").contains("sliceprofile")) {
245 relationship.getRelationshipData().forEach(data -> {
246 if (data.get("relationship-key")
247 .equalsIgnoreCase("service-instance.service-instance-id")) {
248 sliceProfileList.add(data.get("relationship-value"));
258 return fetchSnssaiOfSliceProfile(sliceProfileList);
261 } catch (Exception e) {
262 log.info("Exception while fetching snssaiList: " + e);
270 * Fetches the sliceProfileList of a AllotedResource
272 * @param allotedResourceList contains list of allotedResource IDs
273 * @return sliceProfilesList contains list of SliceProfiles
275 public List<String> fetchSliceProfilesOfAllotedResourceData(List<String> allotedResourceList) {
277 List<String> sliceProfileList = new ArrayList<>();
279 log.info("fetch Alloted Resource Data");
281 allotedResourceList.forEach(serviceInstanceId -> {
283 String serviceReqUrl = aaiBaseUrl + "/business/customers/customer/" + globalSubscriberId
284 + "/service-subscriptions/service-subscription/" + subscriptionServiceType
285 + "/service-instances/service-instance/" + serviceInstanceId;
286 log.info("serviceReqUrl {}", serviceReqUrl);
287 String serviceInstanceString =
288 restclient.sendGetRequest(serviceReqUrl, new ParameterizedTypeReference<String>() {}).getBody();
289 ServiceInstance serviceInstance = objectMapper.readValue(serviceInstanceString, ServiceInstance.class);
291 serviceInstance.getRelationshipList().getRelationship().forEach(relationship -> {
292 relationship.getRelatedToProperty().forEach(property -> {
293 if (property.get("property-value").contains("sliceprofile")) {
294 relationship.getRelationshipData().forEach(data -> {
295 if (data.get("relationship-key")
296 .equalsIgnoreCase("service-instance.service-instance-id")) {
297 sliceProfileList.add(data.get("relationship-value"));
307 } catch (Exception e) {
308 log.info("Exception while fetching AllotedResourceData: " + e);
313 log.info("sliceProfileList: " + sliceProfileList);
315 return sliceProfileList;
320 * Fetches the snssaiList of a SliceProfile
322 * @param sliceProfileList contains list of sliceProfile IDs
323 * @return snssaiList contains list of SNSSAIs
325 public List<String> fetchSnssaiOfSliceProfile(List<String> sliceProfileList) {
326 List<String> snssaiList = new ArrayList<>();
328 log.info("fetch SliceProfile");
329 sliceProfileList.forEach(serviceInstanceId -> {
330 String serviceReqUrl = aaiBaseUrl + "/business/customers/customer/" + globalSubscriberId
331 + "/service-subscriptions/service-subscription/" + subscriptionServiceType + "/service-instances/"
332 + "service-instance/" + serviceInstanceId;
333 log.info("serviceReqUrl {}", serviceReqUrl);
335 String serviceInstanceString =
336 restclient.sendGetRequest(serviceReqUrl, new ParameterizedTypeReference<String>() {}).getBody();
337 ServiceInstance serviceInstance = objectMapper.readValue(serviceInstanceString, ServiceInstance.class);
339 if (serviceInstance.getServiceRole().equalsIgnoreCase("slice-profile")) {
340 if (!snssaiList.contains(serviceInstance.getEnvironmentContext())) {
341 snssaiList.add(serviceInstance.getEnvironmentContext());
345 } catch (Exception e) {
346 log.info("Exception while fetching sliceProfile data: " + e);
350 log.info("snssaiList: " + snssaiList);
357 * Get network bandwidth attribute of an ethernet service. These data is inside a network policy whose
358 * etwork-policy-fqdn equals to provided service-instance-id
359 * @param serviceId target service instance id
360 * @return Map contains maxBandwidth value of given service-instance
362 public Map<String, Integer> fetchMaxBandwidthOfService(String serviceId){
363 log.info("Fetching max-bandwidth from AAI network-policy");
364 String networkPolicyUrl = aaiBaseUrl + "/network/network-policies" + "?network-policy-fqdn="
366 Map<String, Integer> result = new HashMap<>();
368 ResponseEntity<String> resp = restclient.sendGetRequest(networkPolicyUrl, new ParameterizedTypeReference<String>() {
370 if (resp.getStatusCodeValue() == 200){
371 String networkPolicy = resp.getBody();
372 JSONObject networkPolicyJson = new JSONObject(networkPolicy);
373 JSONArray networkPolicyList = networkPolicyJson.optJSONArray("network-policy");
374 if (networkPolicyList != null){
375 JSONObject networkPolicyOjb = networkPolicyList.getJSONObject(0);
376 result.put("maxBandwidth", networkPolicyOjb.getInt("max-bandwidth"));
379 log.info("Successfully fetched max bandwidth {}: {}", serviceId, result);
381 } catch (Exception e){
382 log.warn("Error encountered when fetching maxbandwidth: " + e);