f4fc169c54ee2b5abe474c4d183fefe947f5cff8
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.impl.datajobs.subscription.ncmp;
22
23 import static org.onap.cps.ncmp.impl.datajobs.subscription.models.CmSubscriptionStatus.REJECTED;
24
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import lombok.RequiredArgsConstructor;
33 import lombok.extern.slf4j.Slf4j;
34 import org.onap.cps.ncmp.impl.datajobs.subscription.client_to_ncmp.DataSelector;
35 import org.onap.cps.ncmp.impl.datajobs.subscription.dmi.DmiInEventMapper;
36 import org.onap.cps.ncmp.impl.datajobs.subscription.dmi.EventProducer;
37 import org.onap.cps.ncmp.impl.datajobs.subscription.models.CmSubscriptionStatus;
38 import org.onap.cps.ncmp.impl.datajobs.subscription.ncmp_to_dmi.DataJobSubscriptionDmiInEvent;
39 import org.onap.cps.ncmp.impl.datajobs.subscription.utils.CmDataJobSubscriptionPersistenceService;
40 import org.onap.cps.ncmp.impl.inventory.InventoryPersistence;
41 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
42 import org.onap.cps.ncmp.impl.utils.AlternateIdMatcher;
43 import org.onap.cps.ncmp.impl.utils.JexParser;
44 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
45 import org.springframework.stereotype.Service;
46
47 @Service
48 @RequiredArgsConstructor
49 @Slf4j
50 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
51 public class CmSubscriptionHandlerImpl implements CmSubscriptionHandler {
52
53     private final CmDataJobSubscriptionPersistenceService cmDataJobSubscriptionPersistenceService;
54     private final DmiInEventMapper dmiInEventMapper;
55     private final EventProducer eventProducer;
56     private final InventoryPersistence inventoryPersistence;
57     private final AlternateIdMatcher alternateIdMatcher;
58
59     @Override
60     public void createSubscription(final DataSelector dataSelector,
61                                    final String subscriptionId, final List<String> dataNodeSelectors) {
62         if (cmDataJobSubscriptionPersistenceService.isNewSubscriptionId(subscriptionId)) {
63             for (final String dataNodeSelector : dataNodeSelectors) {
64                 cmDataJobSubscriptionPersistenceService.add(subscriptionId, dataNodeSelector);
65             }
66             sendEventToDmis(subscriptionId,
67                     cmDataJobSubscriptionPersistenceService.getInactiveDataNodeSelectors(subscriptionId),
68                     dataSelector, "subscriptionCreateRequest");
69         }
70     }
71
72     @Override
73     public void deleteSubscription(final String subscriptionId) {
74         final Collection<String> dataNodeSelectors =
75                 cmDataJobSubscriptionPersistenceService.getDataNodeSelectors(subscriptionId);
76         final List<String> dataNodeSelectorsWithoutAnySubscriber = new ArrayList<>();
77         for (final String dataNodeSelector : dataNodeSelectors) {
78             cmDataJobSubscriptionPersistenceService.delete(subscriptionId, dataNodeSelector);
79             if (cmDataJobSubscriptionPersistenceService.getSubscriptionIds(dataNodeSelector).isEmpty()) {
80                 dataNodeSelectorsWithoutAnySubscriber.add(dataNodeSelector);
81             }
82         }
83         sendEventToDmis(subscriptionId, dataNodeSelectorsWithoutAnySubscriber, null, "subscriptionDeleteRequest");
84     }
85
86     @Override
87     public void updateCmSubscriptionStatus(final String subscriptionId,
88                                            final String dmiServiceName,
89                                            final CmSubscriptionStatus cmSubscriptionStatus) {
90         final List<String> dataNodeSelectors =
91                 cmDataJobSubscriptionPersistenceService.getInactiveDataNodeSelectors(subscriptionId);
92         final List<String> rejectedDataNodeSelectors = new ArrayList<>();
93         for (final String dataNodeSelector : dataNodeSelectors) {
94             final String cmHandleId = getCmHandleId(dataNodeSelector);
95             if (cmHandleId == null) {
96                 log.info("Ignoring dataNodeSelector={} because no matching CM Handle ID found",
97                         dataNodeSelector);
98                 continue;
99             }
100             final String resolvedDmiServiceName = getDmiServiceName(cmHandleId);
101             if (resolvedDmiServiceName.equals(dmiServiceName)) {
102                 cmDataJobSubscriptionPersistenceService.updateCmSubscriptionStatus(dataNodeSelector,
103                         cmSubscriptionStatus);
104                 if (cmSubscriptionStatus.equals(REJECTED)) {
105                     rejectedDataNodeSelectors.add(dataNodeSelector);
106                 }
107             }
108
109         }
110         if (!rejectedDataNodeSelectors.isEmpty()) {
111             logRejectedDataNodeSelectors(subscriptionId, dmiServiceName, rejectedDataNodeSelectors);
112         }
113     }
114
115     private static void logRejectedDataNodeSelectors(final String subscriptionId, final String dmiServiceName,
116                                                      final List<String> rejectedDataNodeSelectors) {
117         final String dataNodeSelectorAsString =
118                 JexParser.toJsonExpressionsAsString(rejectedDataNodeSelectors);
119         log.info("DMI plugin {} rejected dataNodeSelector '{}' for dataJobId:{}",
120                 dmiServiceName, dataNodeSelectorAsString, subscriptionId);
121     }
122
123     private void sendEventToDmis(final String subscriptionId,
124                                  final List<String> dataNodeSelectors,
125                                  final DataSelector dataSelector,
126                                  final String eventType) {
127         final Map<String, CmHandleIdsAndDataNodeSelectors> cmHandleIdsAndDataNodeSelectorsPerDmi =
128                 createDmiInEventTargetsPerDmi(dataNodeSelectors);
129         for (final Map.Entry<String, CmHandleIdsAndDataNodeSelectors> cmHandleIdsAndDataNodeSelectorsEntry :
130                 cmHandleIdsAndDataNodeSelectorsPerDmi.entrySet()) {
131             final String dmiServiceName = cmHandleIdsAndDataNodeSelectorsEntry.getKey();
132             final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors =
133                     cmHandleIdsAndDataNodeSelectorsEntry.getValue();
134
135             final DataJobSubscriptionDmiInEvent dmiInEvent;
136             dmiInEvent = buildDmiInEvent(cmHandleIdsAndDataNodeSelectors, dataSelector);
137             eventProducer.send(subscriptionId, dmiServiceName, eventType, dmiInEvent);
138         }
139     }
140
141     private DataJobSubscriptionDmiInEvent buildDmiInEvent(
142             final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors,
143             final DataSelector dataSelector) {
144         final List<String> cmHandleIds = new ArrayList<>(cmHandleIdsAndDataNodeSelectors.cmHandleIds);
145         final List<String> dataNodeSelectors = new ArrayList<>(cmHandleIdsAndDataNodeSelectors.dataNodeSelectors);
146         final List<String> notificationTypes;
147         final String notificationFilter;
148         if (dataSelector != null) {
149             notificationTypes = dataSelector.getNotificationTypes();
150             notificationFilter = dataSelector.getNotificationFilter();
151         } else {
152             notificationTypes = null;
153             notificationFilter = null;
154         }
155
156         return dmiInEventMapper.toDmiInEvent(cmHandleIds, dataNodeSelectors, notificationTypes, notificationFilter);
157     }
158
159     private Map<String, CmHandleIdsAndDataNodeSelectors> createDmiInEventTargetsPerDmi(
160             final List<String> dataNodeSelectors) {
161         final Map<String, CmHandleIdsAndDataNodeSelectors> dmiInEventTargetsPerDmi = new HashMap<>();
162         for (final String dataNodeSelector : dataNodeSelectors) {
163             final String cmHandleId = getCmHandleId(dataNodeSelector);
164             if (cmHandleId == null) {
165                 log.info("Failed to resolve cm handle ID for dataNodeSelector {}", dataNodeSelector);
166             } else {
167                 final String dmiServiceName = getDmiServiceName(cmHandleId);
168                 final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors;
169                 if (dmiInEventTargetsPerDmi.get(dmiServiceName) == null) {
170                     cmHandleIdsAndDataNodeSelectors =
171                             new CmHandleIdsAndDataNodeSelectors(new HashSet<>(), new HashSet<>());
172                     dmiInEventTargetsPerDmi.put(dmiServiceName, cmHandleIdsAndDataNodeSelectors);
173                 } else {
174                     cmHandleIdsAndDataNodeSelectors = dmiInEventTargetsPerDmi.get(dmiServiceName);
175                 }
176                 cmHandleIdsAndDataNodeSelectors.cmHandleIds.add(cmHandleId);
177                 cmHandleIdsAndDataNodeSelectors.dataNodeSelectors.add(dataNodeSelector);
178             }
179         }
180         return dmiInEventTargetsPerDmi;
181     }
182
183     private String getCmHandleId(final String dataNodeSelector) {
184         final String alternateId = JexParser.extractFdnPrefix(dataNodeSelector).orElse("");
185         if (alternateId.isEmpty()) {
186             return null;
187         }
188         return alternateIdMatcher.getCmHandleId(alternateId);
189     }
190
191     private String getDmiServiceName(final String cmHandleId) {
192         final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
193         return yangModelCmHandle.getDmiServiceName();
194     }
195
196     private record CmHandleIdsAndDataNodeSelectors(Set<String> cmHandleIds, Set<String> dataNodeSelectors) {
197     }
198 }