Populate cmSubscription cache
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / cmsubscription / DmiCmNotificationSubscriptionCacheHandler.java
1 /*
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
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.events.cmsubscription;
22
23 import static org.onap.cps.ncmp.api.impl.events.cmsubscription.model.CmNotificationSubscriptionStatus.PENDING;
24
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;
30 import java.util.Map;
31 import java.util.Set;
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;
40
41 @Component
42 @RequiredArgsConstructor
43 public class DmiCmNotificationSubscriptionCacheHandler {
44
45     private final Map<String, Map<String, DmiCmNotificationSubscriptionDetails>> cmNotificationSubscriptionCache;
46     private final InventoryPersistence inventoryPersistence;
47
48     /**
49      * Adds new subscription to the subscription cache.
50      *
51      * @param subscriptionId    subscription Id
52      * @param predicates        subscription request predicates
53      */
54     public void add(final String subscriptionId, final List<Predicate> predicates) {
55         cmNotificationSubscriptionCache.put(subscriptionId, createDmiCmNotificationSubscriptionsPerDmi(predicates));
56     }
57
58     /**
59      *  Creates map of subscription details per DMI.
60      *
61      * @param predicates    CM Subscription Create Request Predicates
62      * @return              Map of DmiCmNotificationSubscription per DMI plugin
63      */
64     public Map<String, DmiCmNotificationSubscriptionDetails> createDmiCmNotificationSubscriptionsPerDmi(
65             final List<Predicate> predicates) {
66         final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsPerDmi =
67                 new HashMap<>();
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);
81             }
82         }
83         return dmiCmNotificationSubscriptionDetailsPerDmi;
84     }
85
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);
93         } else {
94             dmiCmNotificationSubscriptionDetailsPerDmi.put(dmiServiceName,
95                     new DmiCmNotificationSubscriptionDetails(
96                             new ArrayList<>(List.of(dmiCmNotificationSubscriptionPredicate)),
97                             PENDING));
98         }
99     }
100
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);
105
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());
110         }
111         return targetCmHandlesByDmiServiceNames;
112     }
113 }