Uplift of PMS source from ONAP
[nonrtric.git] / enrichment-coordinator-service / src / main / java / org / oransc / enrichment / repository / InfoTypeSubscriptions.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ========================LICENSE_END===================================
19  */
20
21 package org.oransc.enrichment.repository;
22
23 import java.lang.invoke.MethodHandles;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Vector;
28
29 import lombok.Builder;
30 import lombok.Getter;
31
32 import org.oransc.enrichment.exceptions.ServiceException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Component;
36
37 /**
38  * Subscriptions of callbacks for type registrations
39  */
40 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
41 @Component
42 public class InfoTypeSubscriptions {
43     private final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
44     private final Map<String, SubscriptionInfo> allSubscriptions = new HashMap<>();
45     private final MultiMap<SubscriptionInfo> subscriptionsByOwner = new MultiMap<>();
46
47     public interface Callbacks {
48         void notifyTypeRegistered(InfoType type, SubscriptionInfo subscriptionInfo);
49
50         void notifyTypeRemoved(InfoType type, SubscriptionInfo subscriptionInfo);
51     }
52
53     @Builder
54     @Getter
55     public static class SubscriptionInfo {
56         private String id;
57
58         private String callbackUrl;
59
60         private String owner;
61
62         private Callbacks callback;
63     }
64
65     public synchronized void put(SubscriptionInfo subscription) {
66         allSubscriptions.put(subscription.getId(), subscription);
67         subscriptionsByOwner.put(subscription.owner, subscription.id, subscription);
68         logger.debug("Added type status subscription {}", subscription.id);
69     }
70
71     public synchronized Collection<SubscriptionInfo> getAllSubscriptions() {
72         return new Vector<>(allSubscriptions.values());
73     }
74
75     /**
76      * Get a subscription and throw if not fond.
77      * 
78      * @param id the ID of the subscription to get.
79      * @return SubscriptionInfo
80      * @throws ServiceException if not found
81      */
82     public synchronized SubscriptionInfo getSubscription(String id) throws ServiceException {
83         SubscriptionInfo p = allSubscriptions.get(id);
84         if (p == null) {
85             throw new ServiceException("Could not find Information subscription: " + id);
86         }
87         return p;
88     }
89
90     /**
91      * Get a subscription or return null if not found. Equivalent to get in all java
92      * collections.
93      * 
94      * @param id the ID of the subscription to get.
95      * @return SubscriptionInfo
96      */
97     public synchronized SubscriptionInfo get(String id) {
98         return allSubscriptions.get(id);
99     }
100
101     public synchronized int size() {
102         return allSubscriptions.size();
103     }
104
105     public synchronized void clear() {
106         allSubscriptions.clear();
107         subscriptionsByOwner.clear();
108     }
109
110     public void remove(SubscriptionInfo subscription) {
111         allSubscriptions.remove(subscription.getId());
112         subscriptionsByOwner.remove(subscription.owner, subscription.id);
113         logger.debug("Removed type status subscription {}", subscription.id);
114     }
115
116     /**
117      * returns all subscriptions for an owner. The colllection can contain 0..n
118      * subscriptions.
119      * 
120      * @param owner
121      * @return
122      */
123     public synchronized Collection<SubscriptionInfo> getSubscriptionsForOwner(String owner) {
124         return subscriptionsByOwner.get(owner);
125     }
126
127     public synchronized void notifyTypeRegistered(InfoType type) {
128         allSubscriptions.forEach((id, subscription) -> subscription.callback.notifyTypeRegistered(type, subscription));
129     }
130
131     public synchronized void notifyTypeRemoved(InfoType type) {
132         allSubscriptions.forEach((id, subscription) -> subscription.callback.notifyTypeRemoved(type, subscription));
133     }
134
135 }