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 java.util.Collection;
24 import java.util.Collections;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.stream.Collectors;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.ncmp.api.impl.events.EventsPublisher;
32 import org.onap.cps.ncmp.api.impl.utils.DmiServiceNameOrganizer;
33 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
34 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
35 import org.onap.cps.ncmp.event.model.SubscriptionEvent;
36 import org.onap.cps.spi.exceptions.OperationNotYetSupportedException;
37 import org.springframework.stereotype.Component;
42 @RequiredArgsConstructor
43 public class SubscriptionEventForwarder {
45 private final InventoryPersistence inventoryPersistence;
46 private final EventsPublisher<SubscriptionEvent> eventsPublisher;
47 private static final String DMI_AVC_SUBSCRIPTION_TOPIC_PREFIX = "ncmp-dmi-cm-avc-subscription-";
50 * Forward subscription event.
52 * @param subscriptionEvent the event to be forwarded
54 public void forwardCreateSubscriptionEvent(final SubscriptionEvent subscriptionEvent) {
55 final List<Object> cmHandleTargets = subscriptionEvent.getEvent().getPredicates().getTargets();
56 if (cmHandleTargets == null || cmHandleTargets.isEmpty()
57 || cmHandleTargets.stream().anyMatch(id -> ((String) id).contains("*"))) {
58 throw new OperationNotYetSupportedException(
59 "CMHandle targets are required. \"Wildcard\" operations are not yet supported");
61 final List<String> cmHandleTargetsAsStrings = cmHandleTargets.stream().map(
62 Objects::toString).collect(Collectors.toList());
63 final Collection<YangModelCmHandle> yangModelCmHandles =
64 inventoryPersistence.getYangModelCmHandles(cmHandleTargetsAsStrings);
66 final Map<String, Map<String, Map<String, String>>> dmiPropertiesPerCmHandleIdPerServiceName
67 = DmiServiceNameOrganizer.getDmiPropertiesPerCmHandleIdPerServiceName(yangModelCmHandles);
68 dmiPropertiesPerCmHandleIdPerServiceName.forEach((dmiServiceName, cmHandlePropertiesMap) -> {
69 subscriptionEvent.getEvent().getPredicates().setTargets(Collections
70 .singletonList(cmHandlePropertiesMap));
71 final String eventKey = createEventKey(subscriptionEvent, dmiServiceName);
72 eventsPublisher.publishEvent(DMI_AVC_SUBSCRIPTION_TOPIC_PREFIX + dmiServiceName, eventKey,
77 private String createEventKey(final SubscriptionEvent subscriptionEvent, final String dmiName) {
78 return subscriptionEvent.getEvent().getSubscription().getClientID()
80 + subscriptionEvent.getEvent().getSubscription().getName()