092bff7b10b44d75d15b206c3935a1f973151039
[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 java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.List;
28 import java.util.Map;
29 import java.util.Set;
30 import lombok.RequiredArgsConstructor;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.ncmp.impl.datajobs.subscription.client_to_ncmp.DataSelector;
33 import org.onap.cps.ncmp.impl.datajobs.subscription.dmi.DmiInEventMapper;
34 import org.onap.cps.ncmp.impl.datajobs.subscription.dmi.EventProducer;
35 import org.onap.cps.ncmp.impl.datajobs.subscription.models.CmSubscriptionStatus;
36 import org.onap.cps.ncmp.impl.datajobs.subscription.ncmp_to_dmi.DataJobSubscriptionDmiInEvent;
37 import org.onap.cps.ncmp.impl.datajobs.subscription.utils.CmDataJobSubscriptionPersistenceService;
38 import org.onap.cps.ncmp.impl.inventory.InventoryPersistence;
39 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
40 import org.onap.cps.ncmp.impl.utils.AlternateIdMatcher;
41 import org.onap.cps.ncmp.impl.utils.JexParser;
42 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
43 import org.springframework.stereotype.Service;
44
45
46 @Service
47 @RequiredArgsConstructor
48 @Slf4j
49 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
50 public class CmSubscriptionHandlerImpl implements CmSubscriptionHandler {
51
52     private final CmDataJobSubscriptionPersistenceService cmDataJobSubscriptionPersistenceService;
53     private final DmiInEventMapper dmiInEventMapper;
54     private final EventProducer eventProducer;
55     private final InventoryPersistence inventoryPersistence;
56     private final AlternateIdMatcher alternateIdMatcher;
57
58     @Override
59     public void createSubscription(final DataSelector dataSelector,
60                                    final String subscriptionId, final List<String> dataNodeSelectors) {
61         for (final String dataNodeSelector : dataNodeSelectors) {
62             cmDataJobSubscriptionPersistenceService.add(subscriptionId, dataNodeSelector);
63         }
64         sendEventToDmis(subscriptionId,
65                 cmDataJobSubscriptionPersistenceService.getInactiveDataNodeSelectors(subscriptionId),
66                 dataSelector, "subscriptionCreateRequest");
67     }
68
69     @Override
70     public void deleteSubscription(final String subscriptionId) {
71         final Collection<String> dataNodeSelectors =
72                 cmDataJobSubscriptionPersistenceService.getDataNodeSelectors(subscriptionId);
73         final List<String> dataNodeSelectorsWithoutAnySubscriber = new ArrayList<>();
74         for (final String dataNodeSelector : dataNodeSelectors) {
75             cmDataJobSubscriptionPersistenceService.delete(subscriptionId, dataNodeSelector);
76             if (cmDataJobSubscriptionPersistenceService.getSubscriptionIds(dataNodeSelector).isEmpty()) {
77                 dataNodeSelectorsWithoutAnySubscriber.add(dataNodeSelector);
78             }
79         }
80         sendEventToDmis(subscriptionId, dataNodeSelectorsWithoutAnySubscriber, null, "subscriptionDeleteRequest");
81     }
82
83     @Override
84     public void updateCmSubscriptionStatus(final String subscriptionId,
85                                            final String dmiServiceName,
86                                            final CmSubscriptionStatus cmSubscriptionStatus) {
87         final List<String> dataNodeSelectors =
88                 cmDataJobSubscriptionPersistenceService.getInactiveDataNodeSelectors(subscriptionId);
89         for (final String dataNodeSelector : dataNodeSelectors) {
90             final String cmHandleId = getCmHandleId(dataNodeSelector);
91             if (cmHandleId == null) {
92                 log.info("Failed to resolve cm handle ID for dataNodeSelector {}", dataNodeSelector);
93             } else {
94                 final String resolvedDmiServiceName = getDmiServiceName(cmHandleId);
95                 if (resolvedDmiServiceName.equals(dmiServiceName)) {
96                     cmDataJobSubscriptionPersistenceService.updateCmSubscriptionStatus(dataNodeSelector,
97                             cmSubscriptionStatus);
98                 }
99             }
100         }
101     }
102
103     private void sendEventToDmis(final String subscriptionId,
104                                  final List<String> dataNodeSelectors,
105                                  final DataSelector dataSelector,
106                                  final String eventType) {
107         final Map<String, CmHandleIdsAndDataNodeSelectors> cmHandleIdsAndDataNodeSelectorsPerDmi =
108                 createDmiInEventTargetsPerDmi(dataNodeSelectors);
109         for (final Map.Entry<String, CmHandleIdsAndDataNodeSelectors> cmHandleIdsAndDataNodeSelectorsEntry :
110                 cmHandleIdsAndDataNodeSelectorsPerDmi.entrySet()) {
111             final String dmiServiceName = cmHandleIdsAndDataNodeSelectorsEntry.getKey();
112             final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors =
113                     cmHandleIdsAndDataNodeSelectorsEntry.getValue();
114
115             final DataJobSubscriptionDmiInEvent dmiInEvent;
116             dmiInEvent = buildDmiInEvent(cmHandleIdsAndDataNodeSelectors, dataSelector);
117             eventProducer.send(subscriptionId, dmiServiceName, eventType, dmiInEvent);
118         }
119     }
120
121     private DataJobSubscriptionDmiInEvent buildDmiInEvent(
122             final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors,
123             final DataSelector dataSelector) {
124         final List<String> cmHandleIds = new ArrayList<>(cmHandleIdsAndDataNodeSelectors.cmHandleIds);
125         final List<String> dataNodeSelectors = new ArrayList<>(cmHandleIdsAndDataNodeSelectors.dataNodeSelectors);
126         final List<String> notificationTypes;
127         final String notificationFilter;
128         if (dataSelector != null) {
129             notificationTypes = dataSelector.getNotificationTypes();
130             notificationFilter = dataSelector.getNotificationFilter();
131         } else {
132             notificationTypes = null;
133             notificationFilter = null;
134         }
135
136         return dmiInEventMapper.toDmiInEvent(cmHandleIds, dataNodeSelectors, notificationTypes, notificationFilter);
137     }
138
139     private Map<String, CmHandleIdsAndDataNodeSelectors> createDmiInEventTargetsPerDmi(
140             final List<String> dataNodeSelectors) {
141         final Map<String, CmHandleIdsAndDataNodeSelectors> dmiInEventTargetsPerDmi = new HashMap<>();
142         for (final String dataNodeSelector : dataNodeSelectors) {
143             final String cmHandleId = getCmHandleId(dataNodeSelector);
144             if (cmHandleId == null) {
145                 log.info("Failed to resolve cm handle ID for dataNodeSelector {}", dataNodeSelector);
146             } else {
147                 final String dmiServiceName = getDmiServiceName(cmHandleId);
148                 final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors;
149                 if (dmiInEventTargetsPerDmi.get(dmiServiceName) == null) {
150                     cmHandleIdsAndDataNodeSelectors =
151                             new CmHandleIdsAndDataNodeSelectors(new HashSet<>(), new HashSet<>());
152                     dmiInEventTargetsPerDmi.put(dmiServiceName, cmHandleIdsAndDataNodeSelectors);
153                 } else {
154                     cmHandleIdsAndDataNodeSelectors = dmiInEventTargetsPerDmi.get(dmiServiceName);
155                 }
156                 cmHandleIdsAndDataNodeSelectors.cmHandleIds.add(cmHandleId);
157                 cmHandleIdsAndDataNodeSelectors.dataNodeSelectors.add(dataNodeSelector);
158             }
159         }
160         return dmiInEventTargetsPerDmi;
161     }
162
163     private String getCmHandleId(final String dataNodeSelector) {
164         final String alternateId = JexParser.extractFdnPrefix(dataNodeSelector).orElse("");
165         if (alternateId.isEmpty()) {
166             return null;
167         }
168         return alternateIdMatcher.getCmHandleId(alternateId);
169     }
170
171     private String getDmiServiceName(final String cmHandleId) {
172         final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
173         return yangModelCmHandle.getDmiServiceName();
174     }
175
176     private record CmHandleIdsAndDataNodeSelectors(Set<String> cmHandleIds, Set<String> dataNodeSelectors) {
177     }
178 }