1 package org.opendaylight.controller.sal.binding.codegen.impl;
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;
8 import java.util.Collections;
10 import java.util.concurrent.ConcurrentHashMap;
11 import java.util.concurrent.ConcurrentMap;
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;
21 class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> //
24 RpcRoutingTable<C, S>, //
25 RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
27 private final Class<C> identifier;
28 private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
29 private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
31 private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
32 private S defaultRoute;
34 public RpcRoutingTableImpl(Class<C> identifier) {
36 this.identifier = identifier;
37 this.routes = new ConcurrentHashMap<>();
38 this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
42 public void setDefaultRoute(S target) {
43 defaultRoute = target;
47 public S getDefaultRoute() {
52 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
54 return (ListenerRegistration<L>) new SingletonListenerRegistration<L>(listener);
58 public Class<C> getIdentifier() {
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));
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));
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));
94 public S getRoute(InstanceIdentifier<?> nodeInstance) {
95 S route = routes.get(nodeInstance);
99 return getDefaultRoute();
103 public Map<InstanceIdentifier<?>, S> getRoutes() {
104 return unmodifiableRoutes;
107 protected void removeAllReferences(S service) {
111 private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
112 AbstractObjectRegistration<L>
113 implements ListenerRegistration<L> {
115 public SingletonListenerRegistration(L instance) {
121 protected void removeRegistration() {