aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-moi/src/main/java/org/onap/so/bpmn/moi/tasks/AssignRANNssiBBTasks.java
blob: 1ef073ca68b993c63ed06f7765371fbc8ea3a836 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (c) 2022 Deutsche telekom
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */


package org.onap.so.bpmn.moi.tasks;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.onap.aai.domain.yang.*;
import org.onap.aaiclient.client.aai.AAIRestClientImpl;
import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
import org.onap.aaiclient.client.generated.fluentbuilders.AAIFluentTypeBuilder;
import org.onap.so.bpmn.common.BuildingBlockExecution;
import org.onap.so.bpmn.common.InjectionHelper;
import org.onap.so.bpmn.moi.util.AAISliceProfileUtil;
import org.onap.so.bpmn.moi.util.SliceProfileAaiToMoiMapperUtil;
import org.onap.so.bpmn.servicedecomposition.entities.GeneralBuildingBlock;
import org.onap.so.bpmn.servicedecomposition.modelinfo.ModelInfoServiceInstance;
import org.onap.so.moi.Attributes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;

@Component
public class AssignRANNssiBBTasks {

    private static final Logger LOGGER = LoggerFactory.getLogger(AssignRANNssiBBTasks.class);

    @Autowired
    private InjectionHelper injectionHelper;

    private ObjectMapper mapper = new ObjectMapper();

    private AAIRestClientImpl aaiRestClient = new AAIRestClientImpl();

    @Autowired
    AAISliceProfileUtil aaiSliceProfileUtil;

    @Autowired
    private SliceProfileAaiToMoiMapperUtil mapperUtil;

    public void createNssi(BuildingBlockExecution execution) throws Exception {


        GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();

        String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();

        ModelInfoServiceInstance modelInfoServiceInstance = gBB.getServiceInstance().getModelInfoServiceInstance();

        // for NON-SHARED check if its Already present
        if (checkNSSI(execution)) {
            if (aaiSliceProfileUtil.getServiceInstance(execution).isPresent()) {
                throw new RuntimeException("Invalid NSSI, Slice subnet already exists");
            }
        }
        ServiceInstance serviceInstance = new ServiceInstance();
        serviceInstance.setServiceInstanceId(serviceInstanceId);
        serviceInstance.setServiceInstanceName("ran_" + serviceInstanceId);
        serviceInstance.setOrchestrationStatus("Assigned");
        serviceInstance.setServiceType("nssi");
        serviceInstance.setModelInvariantId(modelInfoServiceInstance.getModelInvariantUuid());
        serviceInstance.setModelVersionId(modelInfoServiceInstance.getModelUuid());
        serviceInstance.setOperationalStatus("LOCKED");

        Customer customer = getCustomer(execution);

        AAIResourceUri serviceInstanceURI =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(serviceInstance.getServiceInstanceId()));

        injectionHelper.getAaiClient().createIfNotExists(serviceInstanceURI, Optional.of(serviceInstance));

    }

    private boolean checkNSSI(BuildingBlockExecution execution) {

        Optional<ServiceInstance> serviceInstance = aaiSliceProfileUtil.getServiceInstance(execution);


        GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();

        String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();

        List<Map<String, Object>> mapUserParams = gBB.getRequestContext().getRequestParameters().getUserParams();


        Attributes attributes = null;

        for (Map<String, Object> userParamData : mapUserParams) {
            if (userParamData.get("nssi") != null) {
                Map<String, Object> mapParam = (Map<String, Object>) userParamData.get("nssi");
                attributes = mapper.convertValue(mapParam, Attributes.class);
            }
        }
        if (attributes.getSliceProfileList().get(0).getRANSliceSubnetProfile().getResourceSharingLevel()
                .equalsIgnoreCase("NON-SHARED"))
            return true;
        else
            return false;
    }


    public void createSliceProfileInstance(BuildingBlockExecution execution) {
        GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();

        String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();

        Customer customer = getCustomer(execution);

        List<Map<String, Object>> mapUserParams = gBB.getRequestContext().getRequestParameters().getUserParams();


        Attributes attributes = null;

        for (Map<String, Object> userParamData : mapUserParams) {
            if (userParamData.get("nssi") != null) {
                Map<String, Object> mapParam = (Map<String, Object>) userParamData.get("nssi");
                attributes = mapper.convertValue(mapParam, Attributes.class);
            }
        }
        // Create SliceProfile Instance
        ServiceInstance sliceProfileServiceInstance = new ServiceInstance();
        String sliceProfileInstanceId = UUID.randomUUID().toString();
        sliceProfileServiceInstance.setServiceInstanceId(sliceProfileInstanceId);
        sliceProfileServiceInstance.setServiceInstanceName("slice-profile-" + serviceInstanceId);
        sliceProfileServiceInstance.setServiceRole("slice-profile");

        sliceProfileServiceInstance =
                mapperUtil.fillSliceProfileInstanceFromMoiRequest(attributes, sliceProfileServiceInstance);

        AAIResourceUri serviceInstanceURI =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(sliceProfileServiceInstance.getServiceInstanceId()));

        injectionHelper.getAaiClient().createIfNotExists(serviceInstanceURI, Optional.of(sliceProfileServiceInstance));

        List<Map<String, Object>> sliceProfilesData = gBB.getRequestContext().getRequestParameters().getUserParams();

        // sliceProfile
        SliceProfile sliceProfile = mapperUtil.extractAaiSliceProfileFromMoiRequest(attributes);
        String sliceProfileId = UUID.randomUUID().toString();
        sliceProfile.setProfileId(sliceProfileId);

        AAIResourceUri sliceProfileURI =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(sliceProfileInstanceId).sliceProfile(sliceProfile.getProfileId()));

        injectionHelper.getAaiClient().createIfNotExists(sliceProfileURI, Optional.of(sliceProfile));

        execution.setVariable("sliceProfileServiceInstanceId", sliceProfileServiceInstance.getServiceInstanceId());
    }

    public void allotResources(BuildingBlockExecution execution) {

        GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();

        String sliceProfileServiceInstanceId = execution.getVariable("sliceProfileServiceInstanceId");

        LOGGER.debug("sliceProfileServiceInstanceId: {}", sliceProfileServiceInstanceId);

        Customer customer = getCustomer(execution);

        org.onap.aai.domain.yang.v23.AllottedResource allottedResource =
                new org.onap.aai.domain.yang.v23.AllottedResource();

        UUID allottedResourceUuid = UUID.randomUUID();
        allottedResource.setId(allottedResourceUuid.toString());

        AAIResourceUri allotedResourceURI =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(sliceProfileServiceInstanceId).allottedResource(allottedResource.getId()));

        injectionHelper.getAaiClient().createIfNotExists(allotedResourceURI, Optional.of(allottedResource));

        execution.setVariable("allottedResourceUuid", allottedResource.getId());

    }


    public void addSliceProfileToNssi(BuildingBlockExecution execution) {
        LOGGER.info("Entering into addSliceProfileToNssi");

        String sliceProfileServiceInstanceId = execution.getVariable("sliceProfileServiceInstanceId");
        String allottedResourceUuid = execution.getVariable("allottedResourceUuid");
        GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
        String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();

        Customer customer = getCustomer(execution);


        AAIResourceUri nssiUri =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(serviceInstanceId));

        AAIResourceUri allotedResourceURI =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(sliceProfileServiceInstanceId).allottedResource(allottedResourceUuid));

        try {
            injectionHelper.getAaiClient().connect(allotedResourceURI, nssiUri);
        } catch (Exception e) {
            LOGGER.error(">>>>> Error in creating Relationship: {} ", e);
        }
    }

    public void activateNssi(BuildingBlockExecution execution) {
        GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
        String serviceInstanceId = gBB.getServiceInstance().getServiceInstanceId();

        Customer customer = getCustomer(execution);

        AAIResourceUri serviceInstanceURI =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(serviceInstanceId));

        Optional<ServiceInstance> serviceInstanceReturned =
                injectionHelper.getAaiClient().get(ServiceInstance.class, serviceInstanceURI);

        ServiceInstance serviceInstance = null;
        if (serviceInstanceReturned.isPresent()) {
            serviceInstance = serviceInstanceReturned.get();
            serviceInstance.setOperationalStatus("UNLOCKED");
            serviceInstance.setOrchestrationStatus("Active");

            try {
                injectionHelper.getAaiClient().update(serviceInstanceURI, serviceInstance);
            } catch (Exception e) {
                LOGGER.error("Nssi  couldnot be activated: {}", e);
            }
        } else {
            LOGGER.debug("Service Instance not present with Id: {}", serviceInstanceId);
        }

        // SliceProfile
        List<org.onap.so.moi.SliceProfile> sliceProfileList = new ArrayList<org.onap.so.moi.SliceProfile>();
        ServiceInstance serviceInstanceObj;
        List<Relationship> listOfNssiRelationship = serviceInstance.getRelationshipList().getRelationship();

        List<Relationship> listOfNssiRelationshipAR = listOfNssiRelationship.stream()
                .filter(relationship -> relationship.getRelatedTo().equalsIgnoreCase("allotted-resource"))
                .collect(Collectors.toList());

        for (Relationship relationship : listOfNssiRelationshipAR) {
            org.onap.so.moi.SliceProfile sliceProfile = new org.onap.so.moi.SliceProfile();
            for (RelationshipData relationshipData : relationship.getRelationshipData()) {
                if (relationshipData.getRelationshipKey().equalsIgnoreCase("service-instance.service-instance-id")) {
                    String sliceProfileInstanceId = relationshipData.getRelationshipValue();

                    Optional<ServiceInstance> sliceProfileServiceInstance =
                            aaiRestClient.getServiceInstanceById(sliceProfileInstanceId, "5G", "5GCustomer");
                    if (sliceProfileServiceInstance.isPresent()) {
                        ServiceInstance sliceProflieInstance = sliceProfileServiceInstance.get();
                        sliceProflieInstance.setOperationalStatus("UNLOCKED");
                        sliceProflieInstance.setOrchestrationStatus("ACTIVE");

                        AAIResourceUri sliceProfileInstanceURI = AAIUriFactory.createResourceUri(
                                AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                                        .serviceSubscription(customer.getServiceSubscriptions().getServiceSubscription()
                                                .get(0).getServiceType())
                                        .serviceInstance(sliceProflieInstance.getServiceInstanceId()));
                        try {
                            injectionHelper.getAaiClient().update(sliceProfileInstanceURI, sliceProflieInstance);
                        } catch (Exception e) {
                            LOGGER.error("SliceProfile couldnot be activated: {}", e);
                        }
                    } else {
                        LOGGER.debug("Slice Profile Instance not present with Id: {}", serviceInstanceId);
                    }


                }
            }

        }
    }


    private Customer getCustomer(BuildingBlockExecution execution) {

        GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();

        String serviceType = gBB.getCustomer().getServiceSubscription().getServiceType();

        String globalCustomerId = gBB.getCustomer().getGlobalCustomerId();

        ServiceSubscription serviceSubscription = new ServiceSubscription();
        serviceSubscription.setServiceType(serviceType);

        ServiceSubscriptions serviceSubscriptions = new ServiceSubscriptions();
        serviceSubscriptions.getServiceSubscription().add(serviceSubscription);

        Customer customer = new Customer();
        customer.setGlobalCustomerId(globalCustomerId);
        customer.setServiceSubscriptions(serviceSubscriptions);

        return customer;

    }

}