Merge "Fix for Bug 3"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / RpcRoutingTableImpl.java
1 package org.opendaylight.controller.sal.binding.codegen.impl;
2
3 import org.opendaylight.controller.sal.binding.api.rpc.RpcRoutingTable;
4 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
5 import org.opendaylight.yangtools.yang.binding.RpcService;
6 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
7
8 import java.util.Collections;
9 import java.util.Map;
10 import java.util.concurrent.ConcurrentHashMap;
11 import java.util.concurrent.ConcurrentMap;
12
13 import org.opendaylight.yangtools.yang.binding.DataObject;
14 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangePublisher;
15 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
16 import org.opendaylight.controller.md.sal.common.impl.routing.RoutingUtils;
17 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
18 import org.opendaylight.yangtools.concepts.Identifiable;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.concepts.Mutable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> //
25 implements //
26         Mutable, //
27         RpcRoutingTable<C, S>, //
28         RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
29
30     private static final Logger LOGGER = LoggerFactory.getLogger(RpcRoutingTableImpl.class);
31     private final String routerName;
32     private final Class<S> serviceType;
33
34     private final Class<C> contextType;
35     private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
36     private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
37
38     private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
39     private S defaultRoute;
40     
41     public RpcRoutingTableImpl(String routerName,Class<C> contextType, Class<S> serviceType) {
42         super();
43         this.routerName = routerName;
44         this.serviceType = serviceType;
45         this.contextType = contextType;
46         this.routes = new ConcurrentHashMap<>();
47         this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
48     }
49
50     @Override
51     public void setDefaultRoute(S target) {
52         defaultRoute = target;
53     }
54
55     @Override
56     public S getDefaultRoute() {
57         return defaultRoute;
58     }
59
60     @Override
61     public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
62             L listener) {
63         return (ListenerRegistration<L>) new SingletonListenerRegistration<L>(listener);
64     }
65         
66     @Override
67     public Class<C> getIdentifier() {
68         return contextType;
69     }
70
71     @Override
72     @SuppressWarnings("unchecked")
73     public void updateRoute(InstanceIdentifier<?> path, S service) {
74         S previous = this.routes.put(path, service);
75         
76         LOGGER.debug("Route {} updated to {} in routing table {}",path,service,this);
77         @SuppressWarnings("rawtypes")
78         RouteChangeListener listenerCapture = listener;
79         if (previous == null && listenerCapture != null) {
80             listenerCapture.onRouteChange(RoutingUtils.announcementChange(contextType, path));
81         }
82     }
83
84     
85     @Override
86     @SuppressWarnings("unchecked")
87     public void removeRoute(InstanceIdentifier<?> path) {
88         S previous = this.routes.remove(path);
89         LOGGER.debug("Route {} to {} removed in routing table {}",path,previous,this);
90         @SuppressWarnings("rawtypes")
91         RouteChangeListener listenerCapture = listener;
92         if (previous != null && listenerCapture != null) {
93             listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
94         }
95     }
96     
97     public void removeRoute(InstanceIdentifier<?> path, S service) {
98         @SuppressWarnings("rawtypes")
99         RouteChangeListener listenerCapture = listener;
100         if (routes.remove(path, service) && listenerCapture != null) {
101             LOGGER.debug("Route {} to {} removed in routing table {}",path,service,this);
102             listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
103         }
104     }
105
106     @Override
107     public S getRoute(InstanceIdentifier<?> nodeInstance) {
108         S route = routes.get(nodeInstance);
109         if (route != null) {
110             return route;
111         }
112         return getDefaultRoute();
113     }
114
115     @Override
116     public Map<InstanceIdentifier<?>, S> getRoutes() {
117         return unmodifiableRoutes;
118     }
119     
120     protected void removeAllReferences(S service) {
121         
122     }
123     
124     
125
126     @Override
127     public String toString() {
128         return "RpcRoutingTableImpl [router=" + routerName + ", service=" + serviceType.getSimpleName() + ", context="
129                 + contextType.getSimpleName() + "]";
130     }
131
132
133
134     private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
135             AbstractObjectRegistration<L>
136             implements ListenerRegistration<L> {
137
138         public SingletonListenerRegistration(L instance) {
139             super(instance);
140             listener = instance;
141         }
142
143         @Override
144         protected void removeRegistration() {
145             listener = null;
146         }
147     }
148 }