9a0be3be39e4709deacbf814a5669e91940f5236
[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.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.ncmp.impl.datajobs.subscription.client_to_ncmp.DataSelector;
32 import org.onap.cps.ncmp.impl.datajobs.subscription.dmi.DmiInEventMapper;
33 import org.onap.cps.ncmp.impl.datajobs.subscription.dmi.EventProducer;
34 import org.onap.cps.ncmp.impl.datajobs.subscription.ncmp_to_dmi.DataJobSubscriptionDmiInEvent;
35 import org.onap.cps.ncmp.impl.datajobs.subscription.utils.CmDataJobSubscriptionPersistenceService;
36 import org.onap.cps.ncmp.impl.inventory.InventoryPersistence;
37 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
38 import org.onap.cps.ncmp.impl.utils.AlternateIdMatcher;
39 import org.onap.cps.ncmp.impl.utils.JexParser;
40 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
41 import org.springframework.stereotype.Service;
42
43 @Service
44 @RequiredArgsConstructor
45 @Slf4j
46 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
47 public class CmSubscriptionHandlerImpl implements CmSubscriptionHandler {
48
49     private final CmDataJobSubscriptionPersistenceService cmDataJobSubscriptionPersistenceService;
50     private final DmiInEventMapper dmiInEventMapper;
51     private final EventProducer eventProducer;
52     private final InventoryPersistence inventoryPersistence;
53     private final AlternateIdMatcher alternateIdMatcher;
54
55     @Override
56     public void processSubscriptionCreate(final DataSelector dataSelector,
57                                           final String subscriptionId, final List<String> dataNodeSelectors) {
58         for (final String dataNodeSelector : dataNodeSelectors) {
59             cmDataJobSubscriptionPersistenceService.add(subscriptionId, dataNodeSelector);
60         }
61         sendCreateEventToDmis(subscriptionId, dataSelector);
62     }
63
64     private void sendCreateEventToDmis(final String subscriptionId, final DataSelector dataSelector) {
65         final List<String> dataNodeSelectors =
66                 cmDataJobSubscriptionPersistenceService.getInactiveDataNodeSelectors(subscriptionId);
67         final Map<String, CmHandleIdsAndDataNodeSelectors> cmHandleIdsAndDataNodeSelectorsPerDmi =
68                 createDmiInEventTargetsPerDmi(dataNodeSelectors);
69
70         for (final Map.Entry<String, CmHandleIdsAndDataNodeSelectors> cmHandleIdsAndDataNodeSelectorsEntry :
71                 cmHandleIdsAndDataNodeSelectorsPerDmi.entrySet()) {
72             final String dmiServiceName = cmHandleIdsAndDataNodeSelectorsEntry.getKey();
73             final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors =
74                     cmHandleIdsAndDataNodeSelectorsEntry.getValue();
75             final DataJobSubscriptionDmiInEvent dmiInEvent =
76                     buildDmiInEvent(cmHandleIdsAndDataNodeSelectors, dataSelector);
77             eventProducer.send(subscriptionId, dmiServiceName, "subscriptionCreateRequest", dmiInEvent);
78         }
79     }
80
81
82     private DataJobSubscriptionDmiInEvent buildDmiInEvent(
83             final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors,
84             final DataSelector dataSelector) {
85         final List<String> cmHandleIds = new ArrayList<>(cmHandleIdsAndDataNodeSelectors.cmHandleIds);
86         final List<String> dataNodeSelectors = new ArrayList<>(cmHandleIdsAndDataNodeSelectors.dataNodeSelectors);
87         final List<String> notificationTypes = dataSelector.getNotificationTypes();
88         final String notificationFilter = dataSelector.getNotificationFilter();
89         return dmiInEventMapper.toDmiInEvent(cmHandleIds, dataNodeSelectors, notificationTypes, notificationFilter);
90     }
91
92     private Map<String, CmHandleIdsAndDataNodeSelectors> createDmiInEventTargetsPerDmi(
93             final List<String> dataNodeSelectors) {
94         final Map<String, CmHandleIdsAndDataNodeSelectors> dmiInEventTargetsPerDmi = new HashMap<>();
95         for (final String dataNodeSelector : dataNodeSelectors) {
96             final String cmHandleId = getCmHandleId(dataNodeSelector);
97             if (cmHandleId == null) {
98                 log.info("Failed to resolve cm handle ID for dataNodeSelector {}", dataNodeSelector);
99             } else {
100                 final String dmiServiceName = getDmiServiceName(cmHandleId);
101                 final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors;
102                 if (dmiInEventTargetsPerDmi.get(dmiServiceName) == null) {
103                     cmHandleIdsAndDataNodeSelectors =
104                             new CmHandleIdsAndDataNodeSelectors(new HashSet<>(), new HashSet<>());
105                     dmiInEventTargetsPerDmi.put(dmiServiceName, cmHandleIdsAndDataNodeSelectors);
106                 } else {
107                     cmHandleIdsAndDataNodeSelectors = dmiInEventTargetsPerDmi.get(dmiServiceName);
108                 }
109                 cmHandleIdsAndDataNodeSelectors.cmHandleIds.add(cmHandleId);
110                 cmHandleIdsAndDataNodeSelectors.dataNodeSelectors.add(dataNodeSelector);
111             }
112         }
113         return dmiInEventTargetsPerDmi;
114     }
115
116     private String getCmHandleId(final String dataNodeSelector) {
117         final String alternateId = JexParser.extractFdnPrefix(dataNodeSelector).orElse("");
118         if (alternateId.isEmpty()) {
119             return null;
120         }
121         return alternateIdMatcher.getCmHandleId(alternateId);
122     }
123
124     private String getDmiServiceName(final String cmHandleId) {
125         final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
126         return yangModelCmHandle.getDmiServiceName();
127     }
128
129     private record CmHandleIdsAndDataNodeSelectors(Set<String> cmHandleIds, Set<String> dataNodeSelectors) {}
130
131 }