2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 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 static org.onap.cps.ncmp.api.impl.events.cmsubscription.model.CmNotificationSubscriptionStatus.PENDING;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
32 import lombok.RequiredArgsConstructor;
33 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionDetails;
34 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionPredicate;
35 import org.onap.cps.ncmp.api.impl.inventory.InventoryPersistence;
36 import org.onap.cps.ncmp.api.impl.operations.DatastoreType;
37 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
38 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.Predicate;
39 import org.springframework.stereotype.Component;
42 @RequiredArgsConstructor
43 public class DmiCmNotificationSubscriptionCacheHandler {
45 private final Map<String, Map<String, DmiCmNotificationSubscriptionDetails>> cmNotificationSubscriptionCache;
46 private final InventoryPersistence inventoryPersistence;
49 * Adds new subscription to the subscription cache.
51 * @param subscriptionId subscription Id
52 * @param predicates subscription request predicates
54 public void add(final String subscriptionId, final List<Predicate> predicates) {
55 cmNotificationSubscriptionCache.put(subscriptionId, createDmiCmNotificationSubscriptionsPerDmi(predicates));
59 * Creates map of subscription details per DMI.
61 * @param predicates CM Subscription Create Request Predicates
62 * @return Map of DmiCmNotificationSubscription per DMI plugin
64 public Map<String, DmiCmNotificationSubscriptionDetails> createDmiCmNotificationSubscriptionsPerDmi(
65 final List<Predicate> predicates) {
66 final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsPerDmi =
68 for (final Predicate requestPredicate : predicates) {
69 final List<String> targetFilter = requestPredicate.getTargetFilter();
70 final DatastoreType datastoreType = DatastoreType.fromDatastoreName(
71 requestPredicate.getScopeFilter().getDatastore().toString());
72 final Set<String> xpaths = new HashSet<>(requestPredicate.getScopeFilter().getXpathFilter());
73 final Map<String, Set<String>> targetCmHandlesByDmiMap = groupTargetCmHandleIdsByDmi(targetFilter);
74 for (final Map.Entry<String, Set<String>> targetCmHandlesByDmi: targetCmHandlesByDmiMap.entrySet()) {
75 final DmiCmNotificationSubscriptionPredicate dmiCmNotificationSubscriptionPredicate =
76 new DmiCmNotificationSubscriptionPredicate(targetCmHandlesByDmi.getValue(),
77 datastoreType, xpaths);
78 updateDmiCmNotificationSubscriptionDetailsPerDmi(targetCmHandlesByDmi.getKey(),
79 dmiCmNotificationSubscriptionPredicate,
80 dmiCmNotificationSubscriptionDetailsPerDmi);
83 return dmiCmNotificationSubscriptionDetailsPerDmi;
86 private void updateDmiCmNotificationSubscriptionDetailsPerDmi(
87 final String dmiServiceName,
88 final DmiCmNotificationSubscriptionPredicate dmiCmNotificationSubscriptionPredicate,
89 final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsPerDmi) {
90 if (dmiCmNotificationSubscriptionDetailsPerDmi.containsKey(dmiServiceName)) {
91 dmiCmNotificationSubscriptionDetailsPerDmi.get(dmiServiceName)
92 .getDmiCmNotificationSubscriptionPredicates().add(dmiCmNotificationSubscriptionPredicate);
94 dmiCmNotificationSubscriptionDetailsPerDmi.put(dmiServiceName,
95 new DmiCmNotificationSubscriptionDetails(
96 new ArrayList<>(List.of(dmiCmNotificationSubscriptionPredicate)),
101 private Map<String, Set<String>> groupTargetCmHandleIdsByDmi(final List<String> targetCmHandleIds) {
102 final Map<String, Set<String>> targetCmHandlesByDmiServiceNames = new HashMap<>();
103 final Collection<YangModelCmHandle> yangModelCmHandles =
104 inventoryPersistence.getYangModelCmHandles(targetCmHandleIds);
106 for (final YangModelCmHandle yangModelCmHandle : yangModelCmHandles) {
107 final String dmiServiceName = yangModelCmHandle.getDmiServiceName();
108 targetCmHandlesByDmiServiceNames.putIfAbsent(dmiServiceName, new HashSet<>());
109 targetCmHandlesByDmiServiceNames.get(dmiServiceName).add(yangModelCmHandle.getId());
111 return targetCmHandlesByDmiServiceNames;