aboutsummaryrefslogtreecommitdiffstats
path: root/bpmn/so-bpmn-moi/src/main/java/org/onap/so/bpmn/moi/util/AAISliceProfileUtil.java
blob: a23fec8cc7ec95c03dfecb1eacd857643781d880 (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
/*-
 * ============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.util;

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.servicedecomposition.entities.GeneralBuildingBlock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class AAISliceProfileUtil {

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

    private static final ObjectMapper mapper = new ObjectMapper();

    @Autowired
    private InjectionHelper injectionHelper;

    private AAIRestClientImpl aaiRestClient = new AAIRestClientImpl();


    public Optional<ServiceInstance> getServiceInstance(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));
        return injectionHelper.getAaiClient().get(ServiceInstance.class, serviceInstanceURI);
    }

    public void deleteSliceProfile(BuildingBlockExecution execution, String profileId) {
        Optional<ServiceInstance> getServiceInstance = getServiceInstance(execution);
        if (getServiceInstance.isPresent()) {
            ServiceInstance serviceInstance = getServiceInstance.get();
            String NssiId = serviceInstance.getServiceInstanceId();
            LOGGER.info("NSSID {}", NssiId);
            List<Relationship> listOfNssiRelationship = serviceInstance.getRelationshipList().getRelationship();

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

            List<SliceProfile> sliceProfileList;
            LOGGER.info("ProfileID from Request: {}", profileId);
            boolean isDeleted = false;
            for (Relationship relationship : listOfNssiRelationshipAR) {
                for (RelationshipData relationshipData : relationship.getRelationshipData()) {
                    if (relationshipData.getRelationshipKey()
                            .equalsIgnoreCase("service-instance.service-instance-id")) {
                        String sliceProfileInstanceId = relationshipData.getRelationshipValue();
                        LOGGER.debug(">>> sliceProfileInstance: {}", sliceProfileInstanceId);
                        Optional<ServiceInstance> sliceProfile1 = aaiRestClient
                                .getServiceInstanceByIdWithDepth(sliceProfileInstanceId, "5G", "5GCustomer");
                        if (sliceProfile1.isPresent()) {
                            sliceProfileList = sliceProfile1.get().getSliceProfiles().getSliceProfile();
                            LOGGER.info("sliceProfileList {}", sliceProfileList);
                            for (SliceProfile slice : sliceProfileList) {
                                if (slice.getProfileId().equalsIgnoreCase(profileId)) {
                                    LOGGER.info("ProfileID matched Deleting slice profile");
                                    deleteSliceProfileFromAAI(sliceProfileInstanceId, size, execution, NssiId);
                                    isDeleted = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (isDeleted)
                        break;
                }
                if (isDeleted)
                    break;
            }
        }
    }

    public void updateSliceProfile(BuildingBlockExecution execution, String profileId, SliceProfile updatedSlice) {
        GeneralBuildingBlock gBB = execution.getGeneralBuildingBlock();
        List<Map<String, Object>> sliceProfilesData = gBB.getRequestContext().getRequestParameters().getUserParams();
        LOGGER.info(">>> mapParam: {}", sliceProfilesData);

        Optional<ServiceInstance> getServiceInstance = getServiceInstance(execution);
        if (getServiceInstance.isPresent()) {
            ServiceInstance serviceInstance = getServiceInstance.get();
            List<Relationship> listOfNssiRelationship = serviceInstance.getRelationshipList().getRelationship();

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

            List<SliceProfile> sliceProfileList;
            LOGGER.info("ProfileID : {}", profileId);
            for (Relationship relationship : listOfNssiRelationshipAR) {
                for (RelationshipData relationshipData : relationship.getRelationshipData()) {
                    if (relationshipData.getRelationshipKey()
                            .equalsIgnoreCase("service-instance.service-instance-id")) {
                        String sliceProfileInstanceId = relationshipData.getRelationshipValue();
                        LOGGER.debug(">>> sliceProfileInstance: {}", sliceProfileInstanceId);

                        Optional<ServiceInstance> sliceProfile1 = aaiRestClient
                                .getServiceInstanceByIdWithDepth(sliceProfileInstanceId, "5G", "5GCustomer");

                        Optional<ServiceInstance> sliceProfileInstanceNodepth =
                                aaiRestClient.getServiceInstanceById(sliceProfileInstanceId, "5G", "5GCustomer");

                        if (sliceProfile1.isPresent()) {
                            sliceProfileList = sliceProfile1.get().getSliceProfiles().getSliceProfile();
                            int size = sliceProfileList.size();
                            ServiceInstance updatedSliceInstance = sliceProfileInstanceNodepth.get();

                            for (SliceProfile slice : sliceProfileList) {
                                if (slice.getProfileId().equalsIgnoreCase(profileId)) {
                                    LOGGER.info("Profile ID matched... updating slice profile");
                                    updateSliceProfileInAAI(execution, sliceProfileInstanceId, updatedSlice);

                                    // for update in administrativeState
                                    updatedSliceInstance =
                                            mapUserParamsToServiceInstance(updatedSliceInstance, sliceProfilesData);
                                    LOGGER.info(("Updating Slice-profile Instance"));
                                    updateSliceProfileInstance(execution, updatedSliceInstance);
                                }

                            }
                        }
                    }

                }
            }
        }
    }

    public void updateSliceProfileInAAI(BuildingBlockExecution execution, String sliceProfileInstanceId,
            SliceProfile sliceProfile1) {
        Customer customer = getCustomer(execution);
        AAIResourceUri updateSliceURI =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(sliceProfileInstanceId).sliceProfile(sliceProfile1.getProfileId()));
        try {
            injectionHelper.getAaiClient().update(updateSliceURI, sliceProfile1);
        } catch (Exception e) {
            LOGGER.info("Error in updating Slice Profile {}", e);
        }
    }

    public void updateSliceProfileInstance(BuildingBlockExecution execution, ServiceInstance sliceProfileinstance) {
        Customer customer = getCustomer(execution);
        AAIResourceUri updateSliceURI =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(sliceProfileinstance.getServiceInstanceId()));
        try {
            injectionHelper.getAaiClient().update(updateSliceURI, sliceProfileinstance);
        } catch (Exception e) {
            LOGGER.info("Error in updating Slice Profile instance {}", e);
        }
    }

    public void deleteSliceProfileFromAAI(String serviceInstanceID, int size, BuildingBlockExecution execution,
            String NssiId) {
        Customer customer = getCustomer(execution);

        AAIResourceUri deleteInstanceURI =
                AAIUriFactory.createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                        .serviceSubscription(
                                customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                        .serviceInstance(serviceInstanceID));
        try {
            injectionHelper.getAaiClient().delete(deleteInstanceURI);
            LOGGER.info("Slice Profile Instance with ID {} deleted", serviceInstanceID);
        } catch (Exception e) {
            LOGGER.info("Error in deleting Slice Profile instace {}", e);
        }
        LOGGER.info(">>>> Size : {}", size);
        if (size == 1) {
            AAIResourceUri serviceInstanceURI = AAIUriFactory
                    .createResourceUri(AAIFluentTypeBuilder.business().customer(customer.getGlobalCustomerId())
                            .serviceSubscription(
                                    customer.getServiceSubscriptions().getServiceSubscription().get(0).getServiceType())
                            .serviceInstance(NssiId));
            // Delete NSSI
            try {
                injectionHelper.getAaiClient().delete(serviceInstanceURI);
                LOGGER.info("deleted Slice Prfile as well ass NSSI {}", NssiId);
            } catch (Exception e) {
                LOGGER.info("Error in deleting NSSI {}", e);
            }

        }

    }

    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;

    }

    private ServiceInstance mapUserParamsToServiceInstance(ServiceInstance sliceProfileServiceInstanceObj,
            List<Map<String, Object>> sliceProfilesData) {
        Map<String, Object> mapParam = (Map<String, Object>) sliceProfilesData.get(0).get("nssi");
        LOGGER.info(">>> mapParam: {}", mapParam);

        // update administrative State
        String administrativeState = (String) mapParam.get("administrativeState");
        if (administrativeState != null) {
            LOGGER.info(">>> administrativeState: {}", administrativeState);
            sliceProfileServiceInstanceObj.setOperationalStatus(administrativeState);
        }

        String operationalState = (String) mapParam.get("operationalState");
        if (operationalState != null) {
            LOGGER.info(">>> operationalState: {}", operationalState);
            sliceProfileServiceInstanceObj.setOrchestrationStatus(operationalState);
        }
        List<Object> list = (ArrayList<Object>) mapParam.get("sliceProfileList");
        LOGGER.info(">>> sliceProfile List: {}", list);
        Map<String, Object> idMap = (Map<String, Object>) list.get(0);
        LOGGER.info("Keys of Id Map {} ", idMap.keySet());

        return sliceProfileServiceInstanceObj;
    }
}