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.avcsubscription;
23 import com.hazelcast.map.IMap;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.HashSet;
27 import java.util.List;
29 import java.util.Objects;
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.event.avc.ResponseTimeoutTask;
38 import org.onap.cps.ncmp.api.impl.events.EventsPublisher;
39 import org.onap.cps.ncmp.api.impl.utils.DmiServiceNameOrganizer;
40 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
41 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
42 import org.onap.cps.ncmp.event.model.SubscriptionEvent;
43 import org.onap.cps.spi.exceptions.OperationNotYetSupportedException;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.stereotype.Component;
50 @RequiredArgsConstructor
51 public class SubscriptionEventForwarder {
53 private final InventoryPersistence inventoryPersistence;
54 private final EventsPublisher<SubscriptionEvent> eventsPublisher;
55 private final IMap<String, Set<String>> forwardedSubscriptionEventCache;
57 private final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
59 @Value("${app.ncmp.avc.subscription-forward-topic-prefix}")
60 private String dmiAvcSubscriptionTopicPrefix;
62 @Value("${ncmp.timers.subscription-forwarding.dmi-response-timeout-ms:30000}")
63 private int dmiResponseTimeoutInMs;
66 * Forward subscription event.
68 * @param subscriptionEvent the event to be forwarded
70 public void forwardCreateSubscriptionEvent(final SubscriptionEvent subscriptionEvent) {
71 final List<Object> cmHandleTargets = subscriptionEvent.getEvent().getPredicates().getTargets();
72 if (cmHandleTargets == null || cmHandleTargets.isEmpty()
73 || cmHandleTargets.stream().anyMatch(id -> ((String) id).contains("*"))) {
74 throw new OperationNotYetSupportedException(
75 "CMHandle targets are required. \"Wildcard\" operations are not yet supported");
77 final List<String> cmHandleTargetsAsStrings = cmHandleTargets.stream().map(
78 Objects::toString).collect(Collectors.toList());
79 final Collection<YangModelCmHandle> yangModelCmHandles =
80 inventoryPersistence.getYangModelCmHandles(cmHandleTargetsAsStrings);
82 final Map<String, Map<String, Map<String, String>>> dmiPropertiesPerCmHandleIdPerServiceName
83 = DmiServiceNameOrganizer.getDmiPropertiesPerCmHandleIdPerServiceName(yangModelCmHandles);
85 final Set<String> dmisToRespond = new HashSet<>(dmiPropertiesPerCmHandleIdPerServiceName.keySet());
86 if (dmisToRespond.isEmpty()) {
87 log.info("placeholder to create full outcome response for subscriptionEventId: {}.",
88 subscriptionEvent.getEvent().getSubscription().getClientID()
89 + subscriptionEvent.getEvent().getSubscription().getName());
90 //TODO outcome response with no cmhandles
92 startResponseTimeout(subscriptionEvent, dmisToRespond);
93 forwardEventToDmis(dmiPropertiesPerCmHandleIdPerServiceName, subscriptionEvent);
97 private void forwardEventToDmis(final Map<String, Map<String, Map<String, String>>> dmiNameCmHandleMap,
98 final SubscriptionEvent subscriptionEvent) {
99 dmiNameCmHandleMap.forEach((dmiName, cmHandlePropertiesMap) -> {
100 subscriptionEvent.getEvent().getPredicates().setTargets(Collections.singletonList(cmHandlePropertiesMap));
101 final String eventKey = createEventKey(subscriptionEvent, dmiName);
102 final String dmiAvcSubscriptionTopic = dmiAvcSubscriptionTopicPrefix + dmiName;
103 eventsPublisher.publishEvent(dmiAvcSubscriptionTopic, eventKey, subscriptionEvent);
107 private void startResponseTimeout(final SubscriptionEvent subscriptionEvent, final Set<String> dmisToRespond) {
108 final String subscriptionEventId = subscriptionEvent.getEvent().getSubscription().getClientID()
109 + subscriptionEvent.getEvent().getSubscription().getName();
111 forwardedSubscriptionEventCache.put(subscriptionEventId, dmisToRespond);
112 final ResponseTimeoutTask responseTimeoutTask =
113 new ResponseTimeoutTask(forwardedSubscriptionEventCache, subscriptionEventId);
114 executorService.schedule(responseTimeoutTask, dmiResponseTimeoutInMs, TimeUnit.MILLISECONDS);
117 private String createEventKey(final SubscriptionEvent subscriptionEvent, final String dmiName) {
118 return subscriptionEvent.getEvent().getSubscription().getClientID()
120 + subscriptionEvent.getEvent().getSubscription().getName()