2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===================================
21 package org.oransc.enrichment.repository;
23 import java.lang.invoke.MethodHandles;
24 import java.util.Collection;
25 import java.util.HashMap;
27 import java.util.Vector;
29 import lombok.Builder;
32 import org.oransc.enrichment.exceptions.ServiceException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35 import org.springframework.stereotype.Component;
38 * Subscriptions of callbacks for type registrations
40 @SuppressWarnings("squid:S2629") // Invoke method(s) only conditionally
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<>();
47 public interface Callbacks {
48 void notifyTypeRegistered(InfoType type, SubscriptionInfo subscriptionInfo);
50 void notifyTypeRemoved(InfoType type, SubscriptionInfo subscriptionInfo);
55 public static class SubscriptionInfo {
58 private String callbackUrl;
62 private Callbacks callback;
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);
71 public synchronized Collection<SubscriptionInfo> getAllSubscriptions() {
72 return new Vector<>(allSubscriptions.values());
76 * Get a subscription and throw if not fond.
78 * @param id the ID of the subscription to get.
79 * @return SubscriptionInfo
80 * @throws ServiceException if not found
82 public synchronized SubscriptionInfo getSubscription(String id) throws ServiceException {
83 SubscriptionInfo p = allSubscriptions.get(id);
85 throw new ServiceException("Could not find Information subscription: " + id);
91 * Get a subscription or return null if not found. Equivalent to get in all java
94 * @param id the ID of the subscription to get.
95 * @return SubscriptionInfo
97 public synchronized SubscriptionInfo get(String id) {
98 return allSubscriptions.get(id);
101 public synchronized int size() {
102 return allSubscriptions.size();
105 public synchronized void clear() {
106 this.allSubscriptions.clear();
107 this.subscriptionsByOwner.clear();
110 public void remove(SubscriptionInfo subscription) {
111 allSubscriptions.remove(subscription.getId());
112 this.subscriptionsByOwner.remove(subscription.owner, subscription.id);
113 logger.debug("Removed type status subscription {}", subscription.id);
117 * returns all subscriptions for an owner. The colllection can contain 0..n
123 public synchronized Collection<SubscriptionInfo> getSubscriptionsForOwner(String owner) {
124 return this.subscriptionsByOwner.get(owner);
127 public synchronized void notifyTypeRegistered(InfoType type) {
128 this.allSubscriptions
129 .forEach((id, subscription) -> subscription.callback.notifyTypeRegistered(type, subscription));
132 public synchronized void notifyTypeRemoved(InfoType type) {
133 this.allSubscriptions
134 .forEach((id, subscription) -> subscription.callback.notifyTypeRemoved(type, subscription));