635059bfe1736ccfc158f4e69cf18fcea3269fe8
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / event / avc / SubscriptionEventForwarder.java
1 /*
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
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.api.impl.event.avc;
22
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.stream.Collectors;
30 import lombok.RequiredArgsConstructor;
31 import lombok.extern.slf4j.Slf4j;
32 import org.onap.cps.ncmp.api.impl.event.EventsPublisher;
33 import org.onap.cps.ncmp.api.impl.operations.RequiredDmiService;
34 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
35 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
36 import org.onap.cps.ncmp.event.model.SubscriptionEvent;
37 import org.onap.cps.spi.exceptions.OperationNotYetSupportedException;
38 import org.springframework.stereotype.Component;
39
40
41 @Component
42 @Slf4j
43 @RequiredArgsConstructor
44 public class SubscriptionEventForwarder {
45
46     private final InventoryPersistence inventoryPersistence;
47     private final EventsPublisher<SubscriptionEvent> eventsPublisher;
48
49     private static final String DMI_AVC_SUBSCRIPTION_TOPIC_PREFIX = "ncmp-dmi-cm-avc-subscription-";
50
51     /**
52      * Forward subscription event.
53      *
54      * @param subscriptionEvent the event to be forwarded
55      */
56     public void forwardCreateSubscriptionEvent(final SubscriptionEvent subscriptionEvent) {
57         final List<Object> cmHandleTargets = subscriptionEvent.getEvent().getPredicates().getTargets();
58         if (cmHandleTargets == null || cmHandleTargets.isEmpty()
59             || cmHandleTargets.stream().anyMatch(id -> ((String) id).contains("*"))) {
60             throw new OperationNotYetSupportedException(
61                 "CMHandle targets are required. \"Wildcard\" operations are not yet supported");
62         }
63         final List<String> cmHandleTargetsAsStrings = cmHandleTargets.stream().map(
64             Objects::toString).collect(Collectors.toList());
65         final Collection<YangModelCmHandle> yangModelCmHandles =
66             inventoryPersistence.getYangModelCmHandles(cmHandleTargetsAsStrings);
67         final Map<String, Map<String, Map<String, String>>> dmiNameCmHandleMap =
68             organizeByDmiName(yangModelCmHandles);
69         dmiNameCmHandleMap.forEach((dmiName, cmHandlePropertiesMap) -> {
70             subscriptionEvent.getEvent().getPredicates().setTargets(Collections.singletonList(cmHandlePropertiesMap));
71             final String eventKey = createEventKey(subscriptionEvent, dmiName);
72             eventsPublisher.publishEvent(DMI_AVC_SUBSCRIPTION_TOPIC_PREFIX + dmiName, eventKey, subscriptionEvent);
73         });
74     }
75
76     private Map<String, Map<String, Map<String, String>>> organizeByDmiName(
77         final Collection<YangModelCmHandle> yangModelCmHandles) {
78         final Map<String, Map<String, Map<String, String>>> dmiNameCmHandlePropertiesMap = new HashMap<>();
79         yangModelCmHandles.forEach(cmHandle -> {
80             final String dmiName = cmHandle.resolveDmiServiceName(RequiredDmiService.DATA);
81             if (!dmiNameCmHandlePropertiesMap.containsKey(dmiName)) {
82                 final Map<String, Map<String, String>> cmHandleDmiPropertiesMap = new HashMap<>();
83                 cmHandleDmiPropertiesMap.put(cmHandle.getId(), dmiPropertiesAsMap(cmHandle));
84                 dmiNameCmHandlePropertiesMap.put(cmHandle.getDmiDataServiceName(), cmHandleDmiPropertiesMap);
85             } else {
86                 dmiNameCmHandlePropertiesMap.get(cmHandle.getDmiDataServiceName())
87                     .put(cmHandle.getId(), dmiPropertiesAsMap(cmHandle));
88             }
89         });
90         return dmiNameCmHandlePropertiesMap;
91     }
92
93     private String createEventKey(final SubscriptionEvent subscriptionEvent, final String dmiName) {
94         return subscriptionEvent.getEvent().getSubscription().getClientID()
95             + "-"
96             + subscriptionEvent.getEvent().getSubscription().getName()
97             + "-"
98             + dmiName;
99     }
100
101     public Map<String, String> dmiPropertiesAsMap(final YangModelCmHandle yangModelCmHandle) {
102         return yangModelCmHandle.getDmiProperties().stream().collect(
103             Collectors.toMap(YangModelCmHandle.Property::getName, YangModelCmHandle.Property::getValue));
104     }
105
106 }