Merge "Add consistency check of visible yang modules to netconf."
[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.spi.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.ListenerRegistration;
19 import org.opendaylight.yangtools.concepts.Mutable;
20
21 class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> //
22 implements //
23         Mutable, //
24         RpcRoutingTable<C, S>, //
25         RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
26
27     private final Class<C> identifier;
28     private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
29     private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
30
31     private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
32     private S defaultRoute;
33
34     public RpcRoutingTableImpl(Class<C> identifier) {
35         super();
36         this.identifier = identifier;
37         this.routes = new ConcurrentHashMap<>();
38         this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
39     }
40
41     @Override
42     public void setDefaultRoute(S target) {
43         defaultRoute = target;
44     }
45
46     @Override
47     public S getDefaultRoute() {
48         return defaultRoute;
49     }
50
51     @Override
52     public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
53             L listener) {
54         return (ListenerRegistration<L>) new SingletonListenerRegistration<L>(listener);
55     }
56         
57     @Override
58     public Class<C> getIdentifier() {
59         return identifier;
60     }
61
62     @Override
63     @SuppressWarnings("unchecked")
64     public void updateRoute(InstanceIdentifier<?> path, S service) {
65         S previous = this.routes.put(path, service);
66         @SuppressWarnings("rawtypes")
67         RouteChangeListener listenerCapture = listener;
68         if (previous == null && listenerCapture != null) {
69             listenerCapture.onRouteChange(RoutingUtils.announcementChange(identifier, path));
70         }
71     }
72
73     
74     @Override
75     @SuppressWarnings("unchecked")
76     public void removeRoute(InstanceIdentifier<?> path) {
77         S previous = this.routes.remove(path);
78         @SuppressWarnings("rawtypes")
79         RouteChangeListener listenerCapture = listener;
80         if (previous != null && listenerCapture != null) {
81             listenerCapture.onRouteChange(RoutingUtils.removalChange(identifier, path));
82         }
83     }
84     
85     public void removeRoute(InstanceIdentifier<?> path, S service) {
86         @SuppressWarnings("rawtypes")
87         RouteChangeListener listenerCapture = listener;
88         if (routes.remove(path, service) && listenerCapture != null) {
89             listenerCapture.onRouteChange(RoutingUtils.removalChange(identifier, path));
90         }
91     }
92
93     @Override
94     public S getRoute(InstanceIdentifier<?> nodeInstance) {
95         S route = routes.get(nodeInstance);
96         if (route != null) {
97             return route;
98         }
99         return getDefaultRoute();
100     }
101
102     @Override
103     public Map<InstanceIdentifier<?>, S> getRoutes() {
104         return unmodifiableRoutes;
105     }
106     
107     protected void removeAllReferences(S service) {
108         
109     }
110
111     private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
112             AbstractObjectRegistration<L>
113             implements ListenerRegistration<L> {
114
115         public SingletonListenerRegistration(L instance) {
116             super(instance);
117             listener = instance;
118         }
119
120         @Override
121         protected void removeRegistration() {
122             listener = null;
123         }
124     }
125 }