2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023 Nordix Foundation
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.api.impl.events.cmsubscription;
23 import com.hazelcast.map.IMap;
24 import io.cloudevents.CloudEvent;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashSet;
28 import java.util.List;
31 import java.util.concurrent.Executors;
32 import java.util.concurrent.ScheduledExecutorService;
33 import java.util.concurrent.TimeUnit;
34 import java.util.stream.Collectors;
35 import lombok.RequiredArgsConstructor;
36 import lombok.extern.slf4j.Slf4j;
37 import org.onap.cps.ncmp.api.impl.config.embeddedcache.ForwardedSubscriptionEventCacheConfig;
38 import org.onap.cps.ncmp.api.impl.events.EventsPublisher;
39 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
40 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence;
41 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus;
42 import org.onap.cps.ncmp.api.impl.utils.CmSubscriptionEventCloudMapper;
43 import org.onap.cps.ncmp.api.impl.utils.DmiServiceNameOrganizer;
44 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
45 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelSubscriptionEvent;
46 import org.onap.cps.ncmp.api.models.CmSubscriptionEvent;
47 import org.onap.cps.ncmp.events.cmsubscription1_0_0.client_to_ncmp.CmSubscriptionNcmpInEvent;
48 import org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_dmi.CmHandle;
49 import org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_dmi.CmSubscriptionDmiInEvent;
50 import org.springframework.beans.factory.annotation.Value;
51 import org.springframework.stereotype.Component;
56 @RequiredArgsConstructor
57 public class CmSubscriptionNcmpInEventForwarder {
59 private final InventoryPersistence inventoryPersistence;
60 private final EventsPublisher<CloudEvent> eventsPublisher;
61 private final IMap<String, Set<String>> forwardedSubscriptionEventCache;
62 private final CmSubscriptionNcmpOutEventPublisher cmSubscriptionNcmpOutEventPublisher;
63 private final CmSubscriptionNcmpInEventMapper cmSubscriptionNcmpInEventMapper;
64 private final CmSubscriptionEventCloudMapper cmSubscriptionEventCloudMapper;
65 private final CmSubscriptionNcmpInEventToCmSubscriptionDmiInEventMapper
66 cmSubscriptionNcmpInEventToCmSubscriptionDmiInEventMapper;
67 private final SubscriptionPersistence subscriptionPersistence;
68 private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
69 @Value("${app.ncmp.avc.subscription-forward-topic-prefix}")
70 private String dmiAvcSubscriptionTopicPrefix;
72 @Value("${ncmp.timers.subscription-forwarding.dmi-response-timeout-ms:30000}")
73 private int dmiResponseTimeoutInMs;
76 * Forward subscription event.
78 * @param cmSubscriptionNcmpInEvent the event to be forwarded
80 public void forwardCreateSubscriptionEvent(final CmSubscriptionNcmpInEvent cmSubscriptionNcmpInEvent,
81 final String eventType) {
82 final List<String> cmHandleTargets = cmSubscriptionNcmpInEvent.getData().getPredicates().getTargets();
83 if (cmHandleTargets == null || cmHandleTargets.isEmpty() || cmHandleTargets.stream()
84 .anyMatch(id -> (id).contains("*"))) {
85 throw new UnsupportedOperationException(
86 "CMHandle targets are required. \"Wildcard\" operations are not yet supported");
88 final Collection<YangModelCmHandle> yangModelCmHandles =
89 inventoryPersistence.getYangModelCmHandles(cmHandleTargets);
90 final Map<String, Map<String, Map<String, String>>> dmiPropertiesPerCmHandleIdPerServiceName =
91 DmiServiceNameOrganizer.getDmiPropertiesPerCmHandleIdPerServiceName(yangModelCmHandles);
92 findDmisAndRespond(cmSubscriptionNcmpInEvent, eventType, cmHandleTargets,
93 dmiPropertiesPerCmHandleIdPerServiceName);
96 private void findDmisAndRespond(final CmSubscriptionNcmpInEvent cmSubscriptionNcmpInEvent, final String eventType,
97 final List<String> cmHandleTargetsAsStrings,
98 final Map<String, Map<String, Map<String, String>>> dmiPropertiesPerCmHandleIdPerServiceName) {
100 final CmSubscriptionEvent cmSubscriptionEvent = new CmSubscriptionEvent();
101 cmSubscriptionEvent.setSubscriptionName(cmSubscriptionNcmpInEvent.getData().getSubscription().getName());
102 cmSubscriptionEvent.setClientId(cmSubscriptionNcmpInEvent.getData().getSubscription().getClientID());
104 final List<String> cmHandlesThatExistsInDb =
105 dmiPropertiesPerCmHandleIdPerServiceName.entrySet().stream().map(Map.Entry::getValue).map(Map::keySet)
106 .flatMap(Set::stream).collect(Collectors.toList());
108 final List<String> targetCmHandlesDoesNotExistInDb = new ArrayList<>(cmHandleTargetsAsStrings);
109 targetCmHandlesDoesNotExistInDb.removeAll(cmHandlesThatExistsInDb);
111 final Set<String> dmisToRespond = new HashSet<>(dmiPropertiesPerCmHandleIdPerServiceName.keySet());
113 if (dmisToRespond.isEmpty() || !targetCmHandlesDoesNotExistInDb.isEmpty()) {
114 updatesCmHandlesToRejectedAndPersistSubscriptionEvent(cmSubscriptionNcmpInEvent,
115 targetCmHandlesDoesNotExistInDb);
117 if (dmisToRespond.isEmpty()) {
118 cmSubscriptionNcmpOutEventPublisher.sendResponse(cmSubscriptionEvent,
119 "subscriptionCreatedStatus");
121 startResponseTimeout(cmSubscriptionEvent, dmisToRespond);
122 final CmSubscriptionDmiInEvent cmSubscriptionDmiInEvent =
123 cmSubscriptionNcmpInEventToCmSubscriptionDmiInEventMapper.toCmSubscriptionDmiInEvent(
124 cmSubscriptionNcmpInEvent);
125 forwardEventToDmis(dmiPropertiesPerCmHandleIdPerServiceName, cmSubscriptionDmiInEvent, eventType);
129 private void startResponseTimeout(final CmSubscriptionEvent cmSubscriptionEvent,
130 final Set<String> dmisToRespond) {
131 final String subscriptionClientId = cmSubscriptionEvent.getClientId();
132 final String subscriptionName = cmSubscriptionEvent.getSubscriptionName();
133 final String subscriptionEventId = subscriptionClientId + subscriptionName;
135 forwardedSubscriptionEventCache.put(subscriptionEventId, dmisToRespond,
136 ForwardedSubscriptionEventCacheConfig.SUBSCRIPTION_FORWARD_STARTED_TTL_SECS, TimeUnit.SECONDS);
137 final ResponseTimeoutTask responseTimeoutTask =
138 new ResponseTimeoutTask(forwardedSubscriptionEventCache, cmSubscriptionNcmpOutEventPublisher,
139 cmSubscriptionEvent);
141 executorService.schedule(responseTimeoutTask, dmiResponseTimeoutInMs, TimeUnit.MILLISECONDS);
144 private void forwardEventToDmis(final Map<String, Map<String, Map<String, String>>> dmiNameCmHandleMap,
145 final CmSubscriptionDmiInEvent cmSubscriptionDmiInEvent, final String eventType) {
146 dmiNameCmHandleMap.forEach((dmiName, cmHandlePropertiesMap) -> {
147 final List<CmHandle> cmHandleTargets = cmHandlePropertiesMap.entrySet().stream().map(
148 cmHandleAndProperties -> {
149 final CmHandle cmHandle = new CmHandle();
150 cmHandle.setId(cmHandleAndProperties.getKey());
151 cmHandle.setAdditionalProperties(cmHandleAndProperties.getValue());
153 }).collect(Collectors.toList());
155 cmSubscriptionDmiInEvent.getData().getPredicates().setTargets(cmHandleTargets);
156 final String eventKey = createEventKey(cmSubscriptionDmiInEvent, dmiName);
157 final String dmiAvcSubscriptionTopic = dmiAvcSubscriptionTopicPrefix + dmiName;
159 final CloudEvent cmSubscriptionDmiInCloudEvent =
160 cmSubscriptionEventCloudMapper.toCloudEvent(cmSubscriptionDmiInEvent, eventKey, eventType);
161 eventsPublisher.publishCloudEvent(dmiAvcSubscriptionTopic, eventKey, cmSubscriptionDmiInCloudEvent);
165 private String createEventKey(final CmSubscriptionDmiInEvent cmSubscriptionDmiInEvent, final String dmiName) {
166 return cmSubscriptionDmiInEvent.getData().getSubscription().getClientID() + "-"
167 + cmSubscriptionDmiInEvent.getData().getSubscription().getName() + "-" + dmiName;
170 private void updatesCmHandlesToRejectedAndPersistSubscriptionEvent(
171 final CmSubscriptionNcmpInEvent cmSubscriptionNcmpInEvent,
172 final List<String> targetCmHandlesDoesNotExistInDb) {
173 final YangModelSubscriptionEvent yangModelSubscriptionEvent =
174 cmSubscriptionNcmpInEventMapper.toYangModelSubscriptionEvent(cmSubscriptionNcmpInEvent);
175 yangModelSubscriptionEvent.getPredicates()
176 .setTargetCmHandles(findRejectedCmHandles(targetCmHandlesDoesNotExistInDb, yangModelSubscriptionEvent));
177 subscriptionPersistence.saveSubscriptionEvent(yangModelSubscriptionEvent);
180 private static List<YangModelSubscriptionEvent.TargetCmHandle> findRejectedCmHandles(
181 final List<String> targetCmHandlesDoesNotExistInDb,
182 final YangModelSubscriptionEvent yangModelSubscriptionEvent) {
183 return yangModelSubscriptionEvent.getPredicates().getTargetCmHandles().stream()
184 .filter(targetCmHandle -> targetCmHandlesDoesNotExistInDb.contains(targetCmHandle.getCmHandleId()))
185 .map(target -> new YangModelSubscriptionEvent.TargetCmHandle(target.getCmHandleId(),
186 SubscriptionStatus.REJECTED, "Targets not found"))
187 .collect(Collectors.toList());