2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.codegen.impl;
10 import java.util.Collections;
12 import java.util.concurrent.ConcurrentHashMap;
13 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;
28 class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> //
31 RpcRoutingTable<C, S>, //
32 RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
34 private static final Logger LOGGER = LoggerFactory.getLogger(RpcRoutingTableImpl.class);
35 private final String routerName;
36 private final Class<S> serviceType;
38 private final Class<C> contextType;
39 private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
40 private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
42 private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
43 private S defaultRoute;
45 public RpcRoutingTableImpl(String routerName,Class<C> contextType, Class<S> serviceType) {
47 this.routerName = routerName;
48 this.serviceType = serviceType;
49 this.contextType = contextType;
50 this.routes = new ConcurrentHashMap<>();
51 this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
55 public void setDefaultRoute(S target) {
56 defaultRoute = target;
60 public S getDefaultRoute() {
65 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
67 return new SingletonListenerRegistration<L>(listener);
71 public Class<C> getIdentifier() {
76 @SuppressWarnings("unchecked")
77 public void updateRoute(InstanceIdentifier<?> path, S service) {
78 S previous = this.routes.put(path, service);
80 LOGGER.debug("Route {} updated to {} in routing table {}",path,service,this);
81 @SuppressWarnings("rawtypes")
82 RouteChangeListener listenerCapture = listener;
83 if (previous == null && listenerCapture != null) {
84 listenerCapture.onRouteChange(RoutingUtils.announcementChange(contextType, path));
90 @SuppressWarnings("unchecked")
91 public void removeRoute(InstanceIdentifier<?> path) {
92 S previous = this.routes.remove(path);
93 LOGGER.debug("Route {} to {} removed in routing table {}",path,previous,this);
94 @SuppressWarnings("rawtypes")
95 RouteChangeListener listenerCapture = listener;
96 if (previous != null && listenerCapture != null) {
97 listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
101 public void removeRoute(InstanceIdentifier<?> path, S service) {
102 @SuppressWarnings("rawtypes")
103 RouteChangeListener listenerCapture = listener;
104 if (routes.remove(path, service) && listenerCapture != null) {
105 LOGGER.debug("Route {} to {} removed in routing table {}",path,service,this);
106 listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
111 public S getRoute(InstanceIdentifier<?> nodeInstance) {
112 S route = routes.get(nodeInstance);
116 return getDefaultRoute();
120 public Map<InstanceIdentifier<?>, S> getRoutes() {
121 return unmodifiableRoutes;
124 protected void removeAllReferences(S service) {
131 public String toString() {
132 return "RpcRoutingTableImpl [router=" + routerName + ", service=" + serviceType.getSimpleName() + ", context="
133 + contextType.getSimpleName() + "]";
138 private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
139 AbstractObjectRegistration<L>
140 implements ListenerRegistration<L> {
142 public SingletonListenerRegistration(L instance) {
148 protected void removeRegistration() {