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.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;
24 class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> //
27 RpcRoutingTable<C, S>, //
28 RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
30 private static final Logger LOGGER = LoggerFactory.getLogger(RpcRoutingTableImpl.class);
31 private final String routerName;
32 private final Class<S> serviceType;
34 private final Class<C> contextType;
35 private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
36 private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
38 private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
39 private S defaultRoute;
41 public RpcRoutingTableImpl(String routerName,Class<C> contextType, Class<S> serviceType) {
43 this.routerName = routerName;
44 this.serviceType = serviceType;
45 this.contextType = contextType;
46 this.routes = new ConcurrentHashMap<>();
47 this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
51 public void setDefaultRoute(S target) {
52 defaultRoute = target;
56 public S getDefaultRoute() {
61 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
63 return (ListenerRegistration<L>) new SingletonListenerRegistration<L>(listener);
67 public Class<C> getIdentifier() {
72 @SuppressWarnings("unchecked")
73 public void updateRoute(InstanceIdentifier<?> path, S service) {
74 S previous = this.routes.put(path, service);
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));
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));
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));
107 public S getRoute(InstanceIdentifier<?> nodeInstance) {
108 S route = routes.get(nodeInstance);
112 return getDefaultRoute();
116 public Map<InstanceIdentifier<?>, S> getRoutes() {
117 return unmodifiableRoutes;
120 protected void removeAllReferences(S service) {
127 public String toString() {
128 return "RpcRoutingTableImpl [router=" + routerName + ", service=" + serviceType.getSimpleName() + ", context="
129 + contextType.getSimpleName() + "]";
134 private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
135 AbstractObjectRegistration<L>
136 implements ListenerRegistration<L> {
138 public SingletonListenerRegistration(L instance) {
144 protected void removeRegistration() {