2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (c) 2022 Deutsche telekom
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 package org.onap.so.bpmn.moi.util;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.onap.aai.domain.yang.*;
26 import org.onap.aaiclient.client.aai.AAIRestClientImpl;
27 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
28 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
29 import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
30 import org.onap.so.bpmn.common.BuildingBlockExecution;
31 import org.onap.so.bpmn.common.InjectionHelper;
32 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.beans.factory.annotation.Autowired;
36 import org.springframework.stereotype.Component;
37 import java.util.ArrayList;
38 import java.util.List;
40 import java.util.Optional;
41 import java.util.stream.Collectors;
44 public class AAISliceProfileUtil {
46 private static final Logger LOGGER = LoggerFactory.getLogger(AAISliceProfileUtil.class);
48 private static final ObjectMapper mapper = new ObjectMapper();
51 private InjectionHelper injectionHelper;
53 private AAIRestClientImpl aaiRestClient = new AAIRestClientImpl();
56 public Optional<ServiceInstance> getServiceInstance(BuildingBlockExecution execution) {
57 GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
58 String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();
59 Customer customer = getCustomer(execution);
61 AAIResourceUri serviceInstanceURI =
62 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
64 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
65 .serviceInstance(serviceInstanceId));
66 return injectionHelper.getAaiClient().get(ServiceInstance.class, serviceInstanceURI);
69 public void deleteSliceProfile(BuildingBlockExecution execution, String profileId) {
70 Optional<ServiceInstance> getServiceInstance = getServiceInstance(execution);
71 if (getServiceInstance.isPresent()) {
72 ServiceInstance serviceInstance = getServiceInstance.get();
73 String NssiId = serviceInstance.getServiceInstanceId();
74 LOGGER.info("NSSID {}", NssiId);
75 List<Relationship> listOfNssiRelationship = serviceInstance.getRelationshipList().getRelationship();
77 List<Relationship> listOfNssiRelationshipAR = listOfNssiRelationship.stream()
78 .filter(relationship -> relationship.getRelatedTo().equalsIgnoreCase("allotted-resource"))
79 .collect(Collectors.toList());
80 int size = listOfNssiRelationshipAR.size();
82 List<SliceProfile> sliceProfileList;
83 LOGGER.info("ProfileID from Request: {}", profileId);
84 boolean isDeleted = false;
85 for (Relationship relationship : listOfNssiRelationshipAR) {
86 for (RelationshipData relationshipData : relationship.getRelationshipData()) {
87 if (relationshipData.getRelationshipKey()
88 .equalsIgnoreCase("service-instance.service-instance-id")) {
89 String sliceProfileInstanceId = relationshipData.getRelationshipValue();
90 LOGGER.debug(">>> sliceProfileInstance: {}", sliceProfileInstanceId);
91 Optional<ServiceInstance> sliceProfile1 = aaiRestClient
92 .getServiceInstanceByIdWithDepth(sliceProfileInstanceId, "5G", "5GCustomer");
93 if (sliceProfile1.isPresent()) {
94 sliceProfileList = sliceProfile1.get().getSliceProfiles().getSliceProfile();
95 LOGGER.info("sliceProfileList {}", sliceProfileList);
96 for (SliceProfile slice : sliceProfileList) {
97 if (slice.getProfileId().equalsIgnoreCase(profileId)) {
98 LOGGER.info("ProfileID matched Deleting slice profile");
99 deleteSliceProfileFromAAI(sliceProfileInstanceId, size, execution, NssiId);
115 public void updateSliceProfile(BuildingBlockExecution execution, String profileId, SliceProfile updatedSlice) {
116 GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
117 List<Map<String, Object>> sliceProfilesData = gBB.getRequestContext().getRequestParameters().getUserParams();
118 LOGGER.info(">>> mapParam: {}", sliceProfilesData);
120 Optional<ServiceInstance> getServiceInstance = getServiceInstance(execution);
121 if (getServiceInstance.isPresent()) {
122 ServiceInstance serviceInstance = getServiceInstance.get();
123 List<Relationship> listOfNssiRelationship = serviceInstance.getRelationshipList().getRelationship();
125 List<Relationship> listOfNssiRelationshipAR = listOfNssiRelationship.stream()
126 .filter(relationship -> relationship.getRelatedTo().equalsIgnoreCase("allotted-resource"))
127 .collect(Collectors.toList());
129 List<SliceProfile> sliceProfileList;
130 LOGGER.info("ProfileID : {}", profileId);
131 for (Relationship relationship : listOfNssiRelationshipAR) {
132 for (RelationshipData relationshipData : relationship.getRelationshipData()) {
133 if (relationshipData.getRelationshipKey()
134 .equalsIgnoreCase("service-instance.service-instance-id")) {
135 String sliceProfileInstanceId = relationshipData.getRelationshipValue();
136 LOGGER.debug(">>> sliceProfileInstance: {}", sliceProfileInstanceId);
138 Optional<ServiceInstance> sliceProfile1 = aaiRestClient
139 .getServiceInstanceByIdWithDepth(sliceProfileInstanceId, "5G", "5GCustomer");
141 Optional<ServiceInstance> sliceProfileInstanceNodepth =
142 aaiRestClient.getServiceInstanceById(sliceProfileInstanceId, "5G", "5GCustomer");
144 if (sliceProfile1.isPresent()) {
145 sliceProfileList = sliceProfile1.get().getSliceProfiles().getSliceProfile();
146 int size = sliceProfileList.size();
147 ServiceInstance updatedSliceInstance = sliceProfileInstanceNodepth.get();
149 for (SliceProfile slice : sliceProfileList) {
150 if (slice.getProfileId().equalsIgnoreCase(profileId)) {
151 LOGGER.info("Profile ID matched... updating slice profile");
152 updateSliceProfileInAAI(execution, sliceProfileInstanceId, updatedSlice);
154 // for update in administrativeState
155 updatedSliceInstance =
156 mapUserParamsToServiceInstance(updatedSliceInstance, sliceProfilesData);
157 LOGGER.info(("Updating Slice-profile Instance"));
158 updateSliceProfileInstance(execution, updatedSliceInstance);
170 public void updateSliceProfileInAAI(BuildingBlockExecution execution, String sliceProfileInstanceId,
171 SliceProfile sliceProfile1) {
172 Customer customer = getCustomer(execution);
173 AAIResourceUri updateSliceURI =
174 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
175 .serviceSubscription(
176 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
177 .serviceInstance(sliceProfileInstanceId).sliceProfile(sliceProfile1.getProfileId()));
179 injectionHelper.getAaiClient().update(updateSliceURI, sliceProfile1);
180 } catch (Exception e) {
181 LOGGER.info("Error in updating Slice Profile {}", e);
185 public void updateSliceProfileInstance(BuildingBlockExecution execution, ServiceInstance sliceProfileinstance) {
186 Customer customer = getCustomer(execution);
187 AAIResourceUri updateSliceURI =
188 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
189 .serviceSubscription(
190 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
191 .serviceInstance(sliceProfileinstance.getServiceInstanceId()));
193 injectionHelper.getAaiClient().update(updateSliceURI, sliceProfileinstance);
194 } catch (Exception e) {
195 LOGGER.info("Error in updating Slice Profile instance {}", e);
199 public void deleteSliceProfileFromAAI(String serviceInstanceID, int size, BuildingBlockExecution execution,
201 Customer customer = getCustomer(execution);
203 AAIResourceUri deleteInstanceURI =
204 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
205 .serviceSubscription(
206 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
207 .serviceInstance(serviceInstanceID));
209 injectionHelper.getAaiClient().delete(deleteInstanceURI);
210 LOGGER.info("Slice Profile Instance with ID {} deleted", serviceInstanceID);
211 } catch (Exception e) {
212 LOGGER.info("Error in deleting Slice Profile instace {}", e);
214 LOGGER.info(">>>> Size : {}", size);
216 AAIResourceUri serviceInstanceURI = AAIUriFactory
217 .createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
218 .serviceSubscription(
219 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
220 .serviceInstance(NssiId));
223 injectionHelper.getAaiClient().delete(serviceInstanceURI);
224 LOGGER.info("deleted Slice Prfile as well ass NSSI {}", NssiId);
225 } catch (Exception e) {
226 LOGGER.info("Error in deleting NSSI {}", e);
233 private Customer getCustomer(BuildingBlockExecution execution) {
235 GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
237 String serviceType = gBB.getCustomer().getServiceSubscription().getServiceType();
239 String globalCustomerId = gBB.getCustomer().getGlobalCustomerId();
241 ServiceSubscription serviceSubscription = new ServiceSubscription();
242 serviceSubscription.setServiceType(serviceType);
244 ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
245 serviceSubscriptions.getServiceSubscription().add(serviceSubscription);
247 Customer customer = new Customer();
248 customer.setGlobalCustomerId(globalCustomerId);
249 customer.setServiceSubscriptions(serviceSubscriptions);
255 private ServiceInstance mapUserParamsToServiceInstance(ServiceInstance sliceProfileServiceInstanceObj,
256 List<Map<String, Object>> sliceProfilesData) {
257 Map<String, Object> mapParam = (Map<String, Object>) sliceProfilesData.get(0).get("nssi");
258 LOGGER.info(">>> mapParam: {}", mapParam);
260 // update administrative State
261 String administrativeState = (String) mapParam.get("administrativeState");
262 if (administrativeState != null) {
263 LOGGER.info(">>> administrativeState: {}", administrativeState);
264 sliceProfileServiceInstanceObj.setOperationalStatus(administrativeState);
267 String operationalState = (String) mapParam.get("operationalState");
268 if (operationalState != null) {
269 LOGGER.info(">>> operationalState: {}", operationalState);
270 sliceProfileServiceInstanceObj.setOrchestrationStatus(operationalState);
272 List<Object> list = (ArrayList<Object>) mapParam.get("sliceProfileList");
273 LOGGER.info(">>> sliceProfile List: {}", list);
274 Map<String, Object> idMap = (Map<String, Object>) list.get(0);
275 LOGGER.info("Keys of Id Map {} ", idMap.keySet());
277 return sliceProfileServiceInstanceObj;