BUG 3057 - notify added event source by topics created before
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / RpcRoutingTableImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.binding.codegen.impl;
9
10 import java.util.Collections;
11 import java.util.Iterator;
12 import java.util.Map;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangePublisher;
17 import org.opendaylight.controller.md.sal.common.impl.routing.RoutingUtils;
18 import org.opendaylight.controller.sal.binding.api.rpc.RpcRoutingTable;
19 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.concepts.Mutable;
22 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
23 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.binding.RpcService;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 final class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> implements
29         Mutable, //
30         RpcRoutingTable<C, S>, //
31         RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
32
33     private static final Logger LOGGER = LoggerFactory.getLogger(RpcRoutingTableImpl.class);
34     private final String routerName;
35     private final Class<S> serviceType;
36
37     private final Class<C> contextType;
38     private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
39     private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
40
41     private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
42     private S defaultRoute;
43
44     public RpcRoutingTableImpl(final String routerName,final Class<C> contextType, final Class<S> serviceType) {
45         super();
46         this.routerName = routerName;
47         this.serviceType = serviceType;
48         this.contextType = contextType;
49         this.routes = new ConcurrentHashMap<>();
50         this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
51     }
52
53     @Override
54     public void setDefaultRoute(final S target) {
55         defaultRoute = target;
56     }
57
58     @Override
59     public S getDefaultRoute() {
60         return defaultRoute;
61     }
62
63     @Override
64     public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
65             final L listener) {
66         return new SingletonListenerRegistration<L>(listener);
67     }
68
69     @Override
70     public Class<C> getIdentifier() {
71         return contextType;
72     }
73
74     @Override
75     @SuppressWarnings("unchecked")
76     public void updateRoute(final InstanceIdentifier<?> path, final S service) {
77         S previous = this.routes.put(path, service);
78
79         LOGGER.debug("Route {} updated to {} in routing table {}",path,service,this);
80         @SuppressWarnings("rawtypes")
81         RouteChangeListener listenerCapture = listener;
82         if (previous == null && listenerCapture != null) {
83             listenerCapture.onRouteChange(RoutingUtils.announcementChange(contextType, path));
84         }
85     }
86
87
88     @Override
89     @SuppressWarnings("unchecked")
90     public void removeRoute(final InstanceIdentifier<?> path) {
91         S previous = this.routes.remove(path);
92         LOGGER.debug("Route {} to {} removed in routing table {}",path,previous,this);
93         @SuppressWarnings("rawtypes")
94         RouteChangeListener listenerCapture = listener;
95         if (previous != null && listenerCapture != null) {
96             listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
97         }
98     }
99
100     void removeRoute(final InstanceIdentifier<?> path, final S service) {
101         @SuppressWarnings("rawtypes")
102         RouteChangeListener listenerCapture = listener;
103         if (routes.remove(path, service) && listenerCapture != null) {
104             LOGGER.debug("Route {} to {} removed in routing table {}",path,service,this);
105             listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
106         }
107     }
108
109     @Override
110     public S getRoute(final InstanceIdentifier<?> nodeInstance) {
111         S route = routes.get(nodeInstance);
112         if (route != null) {
113             return route;
114         }
115         return getDefaultRoute();
116     }
117
118     @Override
119     public Map<InstanceIdentifier<?>, S> getRoutes() {
120         return unmodifiableRoutes;
121     }
122
123     void removeAllReferences(final S service) {
124         // FIXME: replace this via properly-synchronized BiMap (or something)
125         final Iterator<S> it = routes.values().iterator();
126         while (it.hasNext()) {
127             final S s = it.next();
128             if (service.equals(s)) {
129                 it.remove();
130             }
131         }
132     }
133
134     @Override
135     public String toString() {
136         return "RpcRoutingTableImpl [router=" + routerName + ", service=" + serviceType.getSimpleName() + ", context="
137                 + contextType.getSimpleName() + "]";
138     }
139
140     private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
141             AbstractObjectRegistration<L>
142             implements ListenerRegistration<L> {
143
144         public SingletonListenerRegistration(final L instance) {
145             super(instance);
146             listener = instance;
147         }
148
149         @Override
150         protected void removeRegistration() {
151             listener = null;
152         }
153     }
154 }