Code changes in BPMN infra for RAN Slice Use case
[so.git] / bpmn / so-bpmn-moi / src / main / java / org / onap / so / bpmn / moi / tasks / AssignRANNssiBBTasks.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21
22 package org.onap.so.bpmn.moi.tasks;
23
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.moi.util.AAISliceProfileUtil;
33 import org.onap.so.bpmn.moi.util.SliceProfileAaiToMoiMapperUtil;
34 import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
35 import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
36 import org.onap.so.moi.Attributes;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.springframework.beans.factory.annotation.Autowired;
40 import org.springframework.stereotype.Component;
41 import java.util.*;
42 import java.util.stream.Collectors;
43
44 @Component
45 public class AssignRANNssiBBTasks {
46
47     private static final Logger LOGGER = LoggerFactory.getLogger(AssignRANNssiBBTasks.class);
48
49     @Autowired
50     private InjectionHelper injectionHelper;
51
52     private ObjectMapper mapper = new ObjectMapper();
53
54     private AAIRestClientImpl aaiRestClient = new AAIRestClientImpl();
55
56     @Autowired
57     AAISliceProfileUtil aaiSliceProfileUtil;
58
59     @Autowired
60     private SliceProfileAaiToMoiMapperUtil mapperUtil;
61
62     public void createNssi(BuildingBlockExecution execution) throws Exception {
63
64
65         GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
66
67         String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();
68
69         ModelInfoServiceInstance modelInfoServiceInstance = gBB.getServiceInstance().getModelInfoServiceInstance();
70
71         // for NON-SHARED check if its Already present
72         if (checkNSSI(execution)) {
73             if (aaiSliceProfileUtil.getServiceInstance(execution).isPresent()) {
74                 throw new RuntimeException("Invalid NSSI, Slice subnet already exists");
75             }
76         }
77         ServiceInstance serviceInstance = new ServiceInstance();
78         serviceInstance.setServiceInstanceId(serviceInstanceId);
79         serviceInstance.setServiceInstanceName("ran_" + serviceInstanceId);
80         serviceInstance.setOrchestrationStatus("Assigned");
81         serviceInstance.setServiceType("nssi");
82         serviceInstance.setModelInvariantId(modelInfoServiceInstance.getModelInvariantUuid());
83         serviceInstance.setModelVersionId(modelInfoServiceInstance.getModelUuid());
84         serviceInstance.setOperationalStatus("LOCKED");
85
86         Customer customer = getCustomer(execution);
87
88         AAIResourceUri serviceInstanceURI =
89                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
90                         .serviceSubscription(
91                                 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
92                         .serviceInstance(serviceInstance.getServiceInstanceId()));
93
94         injectionHelper.getAaiClient().createIfNotExists(serviceInstanceURI, Optional.of(serviceInstance));
95
96     }
97
98     private boolean checkNSSI(BuildingBlockExecution execution) {
99
100         Optional<ServiceInstance> serviceInstance = aaiSliceProfileUtil.getServiceInstance(execution);
101
102
103         GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
104
105         String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();
106
107         List<Map<String, Object>> mapUserParams = gBB.getRequestContext().getRequestParameters().getUserParams();
108
109
110         Attributes attributes = null;
111
112         for (Map<String, Object> userParamData : mapUserParams) {
113             if (userParamData.get("nssi") != null) {
114                 Map<String, Object> mapParam = (Map<String, Object>) userParamData.get("nssi");
115                 attributes = mapper.convertValue(mapParam, Attributes.class);
116             }
117         }
118         if (attributes.getSliceProfileList().get(0).getRANSliceSubnetProfile().getResourceSharingLevel()
119                 .equalsIgnoreCase("NON-SHARED"))
120             return true;
121         else
122             return false;
123     }
124
125
126     public void createSliceProfileInstance(BuildingBlockExecution execution) {
127         GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
128
129         String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();
130
131         Customer customer = getCustomer(execution);
132
133         List<Map<String, Object>> mapUserParams = gBB.getRequestContext().getRequestParameters().getUserParams();
134
135
136         Attributes attributes = null;
137
138         for (Map<String, Object> userParamData : mapUserParams) {
139             if (userParamData.get("nssi") != null) {
140                 Map<String, Object> mapParam = (Map<String, Object>) userParamData.get("nssi");
141                 attributes = mapper.convertValue(mapParam, Attributes.class);
142             }
143         }
144         // Create SliceProfile Instance
145         ServiceInstance sliceProfileServiceInstance = new ServiceInstance();
146         String sliceProfileInstanceId = UUID.randomUUID().toString();
147         sliceProfileServiceInstance.setServiceInstanceId(sliceProfileInstanceId);
148         sliceProfileServiceInstance.setServiceInstanceName("slice-profile-" + serviceInstanceId);
149         sliceProfileServiceInstance.setServiceRole("slice-profile");
150
151         sliceProfileServiceInstance =
152                 mapperUtil.fillSliceProfileInstanceFromMoiRequest(attributes, sliceProfileServiceInstance);
153
154         AAIResourceUri serviceInstanceURI =
155                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
156                         .serviceSubscription(
157                                 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
158                         .serviceInstance(sliceProfileServiceInstance.getServiceInstanceId()));
159
160         injectionHelper.getAaiClient().createIfNotExists(serviceInstanceURI, Optional.of(sliceProfileServiceInstance));
161
162         List<Map<String, Object>> sliceProfilesData = gBB.getRequestContext().getRequestParameters().getUserParams();
163
164         // sliceProfile
165         SliceProfile sliceProfile = mapperUtil.extractAaiSliceProfileFromMoiRequest(attributes);
166         String sliceProfileId = UUID.randomUUID().toString();
167         sliceProfile.setProfileId(sliceProfileId);
168
169         AAIResourceUri sliceProfileURI =
170                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
171                         .serviceSubscription(
172                                 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
173                         .serviceInstance(sliceProfileInstanceId).sliceProfile(sliceProfile.getProfileId()));
174
175         injectionHelper.getAaiClient().createIfNotExists(sliceProfileURI, Optional.of(sliceProfile));
176
177         execution.setVariable("sliceProfileServiceInstanceId", sliceProfileServiceInstance.getServiceInstanceId());
178     }
179
180     public void allotResources(BuildingBlockExecution execution) {
181
182         GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
183
184         String sliceProfileServiceInstanceId = execution.getVariable("sliceProfileServiceInstanceId");
185
186         LOGGER.debug("sliceProfileServiceInstanceId: {}", sliceProfileServiceInstanceId);
187
188         Customer customer = getCustomer(execution);
189
190         org.onap.aai.domain.yang.v23.AllottedResource allottedResource =
191                 new org.onap.aai.domain.yang.v23.AllottedResource();
192
193         UUID allottedResourceUuid = UUID.randomUUID();
194         allottedResource.setId(allottedResourceUuid.toString());
195
196         AAIResourceUri allotedResourceURI =
197                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
198                         .serviceSubscription(
199                                 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
200                         .serviceInstance(sliceProfileServiceInstanceId).allottedResource(allottedResource.getId()));
201
202         injectionHelper.getAaiClient().createIfNotExists(allotedResourceURI, Optional.of(allottedResource));
203
204         execution.setVariable("allottedResourceUuid", allottedResource.getId());
205
206     }
207
208
209     public void addSliceProfileToNssi(BuildingBlockExecution execution) {
210         LOGGER.info("Entering into addSliceProfileToNssi");
211
212         String sliceProfileServiceInstanceId = execution.getVariable("sliceProfileServiceInstanceId");
213         String allottedResourceUuid = execution.getVariable("allottedResourceUuid");
214         GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
215         String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();
216
217         Customer customer = getCustomer(execution);
218
219
220         AAIResourceUri nssiUri =
221                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
222                         .serviceSubscription(
223                                 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
224                         .serviceInstance(serviceInstanceId));
225
226         AAIResourceUri allotedResourceURI =
227                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
228                         .serviceSubscription(
229                                 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
230                         .serviceInstance(sliceProfileServiceInstanceId).allottedResource(allottedResourceUuid));
231
232         try {
233             injectionHelper.getAaiClient().connect(allotedResourceURI, nssiUri);
234         } catch (Exception e) {
235             LOGGER.error(">>>>> Error in creating Relationship: {} ", e);
236         }
237     }
238
239     public void activateNssi(BuildingBlockExecution execution) {
240         GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
241         String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();
242
243         Customer customer = getCustomer(execution);
244
245         AAIResourceUri serviceInstanceURI =
246                 AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
247                         .serviceSubscription(
248                                 customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
249                         .serviceInstance(serviceInstanceId));
250
251         Optional<ServiceInstance> serviceInstanceReturned =
252                 injectionHelper.getAaiClient().get(ServiceInstance.class, serviceInstanceURI);
253
254         ServiceInstance serviceInstance = null;
255         if (serviceInstanceReturned.isPresent()) {
256             serviceInstance = serviceInstanceReturned.get();
257             serviceInstance.setOperationalStatus("UNLOCKED");
258             serviceInstance.setOrchestrationStatus("Active");
259
260             try {
261                 injectionHelper.getAaiClient().update(serviceInstanceURI, serviceInstance);
262             } catch (Exception e) {
263                 LOGGER.error("Nssi  couldnot be activated: {}", e);
264             }
265         } else {
266             LOGGER.debug("Service Instance not present with Id: {}", serviceInstanceId);
267         }
268
269         // SliceProfile
270         List<org.onap.so.moi.SliceProfile> sliceProfileList = new ArrayList<org.onap.so.moi.SliceProfile>();
271         ServiceInstance serviceInstanceObj;
272         List<Relationship> listOfNssiRelationship = serviceInstance.getRelationshipList().getRelationship();
273
274         List<Relationship> listOfNssiRelationshipAR = listOfNssiRelationship.stream()
275                 .filter(relationship -> relationship.getRelatedTo().equalsIgnoreCase("allotted-resource"))
276                 .collect(Collectors.toList());
277
278         for (Relationship relationship : listOfNssiRelationshipAR) {
279             org.onap.so.moi.SliceProfile sliceProfile = new org.onap.so.moi.SliceProfile();
280             for (RelationshipData relationshipData : relationship.getRelationshipData()) {
281                 if (relationshipData.getRelationshipKey().equalsIgnoreCase("service-instance.service-instance-id")) {
282                     String sliceProfileInstanceId = relationshipData.getRelationshipValue();
283
284                     Optional<ServiceInstance> sliceProfileServiceInstance =
285                             aaiRestClient.getServiceInstanceById(sliceProfileInstanceId, "5G", "5GCustomer");
286                     if (sliceProfileServiceInstance.isPresent()) {
287                         ServiceInstance sliceProflieInstance = sliceProfileServiceInstance.get();
288                         sliceProflieInstance.setOperationalStatus("UNLOCKED");
289                         sliceProflieInstance.setOrchestrationStatus("ACTIVE");
290
291                         AAIResourceUri sliceProfileInstanceURI = AAIUriFactory.createResourceUri(
292                                 AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
293                                         .serviceSubscription(customer.getServiceSubscriptions().getServiceSubscription()
294                                                 .get(0).getServiceType())
295                                         .serviceInstance(sliceProflieInstance.getServiceInstanceId()));
296                         try {
297                             injectionHelper.getAaiClient().update(sliceProfileInstanceURI, sliceProflieInstance);
298                         } catch (Exception e) {
299                             LOGGER.error("SliceProfile couldnot be activated: {}", e);
300                         }
301                     } else {
302                         LOGGER.debug("Slice Profile Instance not present with Id: {}", serviceInstanceId);
303                     }
304
305
306                 }
307             }
308
309         }
310     }
311
312
313     private Customer getCustomer(BuildingBlockExecution execution) {
314
315         GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
316
317         String serviceType = gBB.getCustomer().getServiceSubscription().getServiceType();
318
319         String globalCustomerId = gBB.getCustomer().getGlobalCustomerId();
320
321         ServiceSubscription serviceSubscription = new ServiceSubscription();
322         serviceSubscription.setServiceType(serviceType);
323
324         ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
325         serviceSubscriptions.getServiceSubscription().add(serviceSubscription);
326
327         Customer customer = new Customer();
328         customer.setGlobalCustomerId(globalCustomerId);
329         customer.setServiceSubscriptions(serviceSubscriptions);
330
331         return customer;
332
333     }
334
335 }