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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.impl.datajobs.subscription.ncmp;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
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.models.CmSubscriptionStatus;
35 import org.onap.cps.ncmp.impl.datajobs.subscription.ncmp_to_dmi.DataJobSubscriptionDmiInEvent;
36 import org.onap.cps.ncmp.impl.datajobs.subscription.utils.CmDataJobSubscriptionPersistenceService;
37 import org.onap.cps.ncmp.impl.inventory.InventoryPersistence;
38 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
39 import org.onap.cps.ncmp.impl.utils.AlternateIdMatcher;
40 import org.onap.cps.ncmp.impl.utils.JexParser;
41 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
42 import org.springframework.stereotype.Service;
45 @RequiredArgsConstructor
47 @ConditionalOnProperty(name = "notification.enabled", havingValue = "true", matchIfMissing = true)
48 public class CmSubscriptionHandlerImpl implements CmSubscriptionHandler {
50 private final CmDataJobSubscriptionPersistenceService cmDataJobSubscriptionPersistenceService;
51 private final DmiInEventMapper dmiInEventMapper;
52 private final EventProducer eventProducer;
53 private final InventoryPersistence inventoryPersistence;
54 private final AlternateIdMatcher alternateIdMatcher;
57 public void processSubscriptionCreate(final DataSelector dataSelector,
58 final String subscriptionId, final List<String> dataNodeSelectors) {
59 for (final String dataNodeSelector : dataNodeSelectors) {
60 cmDataJobSubscriptionPersistenceService.add(subscriptionId, dataNodeSelector);
62 sendCreateEventToDmis(subscriptionId, dataSelector);
66 public void updateCmSubscriptionStatus(final String subscriptionId,
67 final String dmiServiceName,
68 final CmSubscriptionStatus cmSubscriptionStatus) {
69 final List<String> dataNodeSelectors =
70 cmDataJobSubscriptionPersistenceService.getInactiveDataNodeSelectors(subscriptionId);
71 for (final String dataNodeSelector : dataNodeSelectors) {
72 final String cmHandleId = getCmHandleId(dataNodeSelector);
73 if (cmHandleId == null) {
74 log.info("Failed to resolve cm handle ID for dataNodeSelector {}", dataNodeSelector);
76 final String resolvedDmiServiceName = getDmiServiceName(cmHandleId);
77 if (resolvedDmiServiceName.equals(dmiServiceName)) {
78 cmDataJobSubscriptionPersistenceService.updateCmSubscriptionStatus(dataNodeSelector,
79 cmSubscriptionStatus);
85 private void sendCreateEventToDmis(final String subscriptionId, final DataSelector dataSelector) {
86 final List<String> dataNodeSelectors =
87 cmDataJobSubscriptionPersistenceService.getInactiveDataNodeSelectors(subscriptionId);
88 final Map<String, CmHandleIdsAndDataNodeSelectors> cmHandleIdsAndDataNodeSelectorsPerDmi =
89 createDmiInEventTargetsPerDmi(dataNodeSelectors);
91 for (final Map.Entry<String, CmHandleIdsAndDataNodeSelectors> cmHandleIdsAndDataNodeSelectorsEntry :
92 cmHandleIdsAndDataNodeSelectorsPerDmi.entrySet()) {
93 final String dmiServiceName = cmHandleIdsAndDataNodeSelectorsEntry.getKey();
94 final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors =
95 cmHandleIdsAndDataNodeSelectorsEntry.getValue();
96 final DataJobSubscriptionDmiInEvent dmiInEvent =
97 buildDmiInEvent(cmHandleIdsAndDataNodeSelectors, dataSelector);
98 eventProducer.send(subscriptionId, dmiServiceName, "subscriptionCreateRequest", dmiInEvent);
103 private DataJobSubscriptionDmiInEvent buildDmiInEvent(
104 final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors,
105 final DataSelector dataSelector) {
106 final List<String> cmHandleIds = new ArrayList<>(cmHandleIdsAndDataNodeSelectors.cmHandleIds);
107 final List<String> dataNodeSelectors = new ArrayList<>(cmHandleIdsAndDataNodeSelectors.dataNodeSelectors);
108 final List<String> notificationTypes = dataSelector.getNotificationTypes();
109 final String notificationFilter = dataSelector.getNotificationFilter();
110 return dmiInEventMapper.toDmiInEvent(cmHandleIds, dataNodeSelectors, notificationTypes, notificationFilter);
113 private Map<String, CmHandleIdsAndDataNodeSelectors> createDmiInEventTargetsPerDmi(
114 final List<String> dataNodeSelectors) {
115 final Map<String, CmHandleIdsAndDataNodeSelectors> dmiInEventTargetsPerDmi = new HashMap<>();
116 for (final String dataNodeSelector : dataNodeSelectors) {
117 final String cmHandleId = getCmHandleId(dataNodeSelector);
118 if (cmHandleId == null) {
119 log.info("Failed to resolve cm handle ID for dataNodeSelector {}", dataNodeSelector);
121 final String dmiServiceName = getDmiServiceName(cmHandleId);
122 final CmHandleIdsAndDataNodeSelectors cmHandleIdsAndDataNodeSelectors;
123 if (dmiInEventTargetsPerDmi.get(dmiServiceName) == null) {
124 cmHandleIdsAndDataNodeSelectors =
125 new CmHandleIdsAndDataNodeSelectors(new HashSet<>(), new HashSet<>());
126 dmiInEventTargetsPerDmi.put(dmiServiceName, cmHandleIdsAndDataNodeSelectors);
128 cmHandleIdsAndDataNodeSelectors = dmiInEventTargetsPerDmi.get(dmiServiceName);
130 cmHandleIdsAndDataNodeSelectors.cmHandleIds.add(cmHandleId);
131 cmHandleIdsAndDataNodeSelectors.dataNodeSelectors.add(dataNodeSelector);
134 return dmiInEventTargetsPerDmi;
137 private String getCmHandleId(final String dataNodeSelector) {
138 final String alternateId = JexParser.extractFdnPrefix(dataNodeSelector).orElse("");
139 if (alternateId.isEmpty()) {
142 return alternateIdMatcher.getCmHandleId(alternateId);
145 private String getDmiServiceName(final String cmHandleId) {
146 final YangModelCmHandle yangModelCmHandle = inventoryPersistence.getYangModelCmHandle(cmHandleId);
147 return yangModelCmHandle.getDmiServiceName();
150 private record CmHandleIdsAndDataNodeSelectors(Set<String> cmHandleIds, Set<String> dataNodeSelectors) {}