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 org.opendaylight.controller.sal.binding.api.rpc.RpcRoutingTable;
11 import org.opendaylight.yangtools.yang.binding.BaseIdentity;
12 import org.opendaylight.yangtools.yang.binding.RpcService;
13 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
15 import java.util.Collections;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
20 import org.opendaylight.yangtools.yang.binding.DataObject;
21 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangePublisher;
22 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
23 import org.opendaylight.controller.md.sal.common.impl.routing.RoutingUtils;
24 import org.opendaylight.yangtools.concepts.AbstractObjectRegistration;
25 import org.opendaylight.yangtools.concepts.Identifiable;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.concepts.Mutable;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> //
34 RpcRoutingTable<C, S>, //
35 RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
37 private static final Logger LOGGER = LoggerFactory.getLogger(RpcRoutingTableImpl.class);
38 private final String routerName;
39 private final Class<S> serviceType;
41 private final Class<C> contextType;
42 private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
43 private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
45 private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
46 private S defaultRoute;
48 public RpcRoutingTableImpl(String routerName,Class<C> contextType, Class<S> serviceType) {
50 this.routerName = routerName;
51 this.serviceType = serviceType;
52 this.contextType = contextType;
53 this.routes = new ConcurrentHashMap<>();
54 this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
58 public void setDefaultRoute(S target) {
59 defaultRoute = target;
63 public S getDefaultRoute() {
68 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
70 return (ListenerRegistration<L>) new SingletonListenerRegistration<L>(listener);
74 public Class<C> getIdentifier() {
79 @SuppressWarnings("unchecked")
80 public void updateRoute(InstanceIdentifier<?> path, S service) {
81 S previous = this.routes.put(path, service);
83 LOGGER.debug("Route {} updated to {} in routing table {}",path,service,this);
84 @SuppressWarnings("rawtypes")
85 RouteChangeListener listenerCapture = listener;
86 if (previous == null && listenerCapture != null) {
87 listenerCapture.onRouteChange(RoutingUtils.announcementChange(contextType, path));
93 @SuppressWarnings("unchecked")
94 public void removeRoute(InstanceIdentifier<?> path) {
95 S previous = this.routes.remove(path);
96 LOGGER.debug("Route {} to {} removed in routing table {}",path,previous,this);
97 @SuppressWarnings("rawtypes")
98 RouteChangeListener listenerCapture = listener;
99 if (previous != null && listenerCapture != null) {
100 listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
104 public void removeRoute(InstanceIdentifier<?> path, S service) {
105 @SuppressWarnings("rawtypes")
106 RouteChangeListener listenerCapture = listener;
107 if (routes.remove(path, service) && listenerCapture != null) {
108 LOGGER.debug("Route {} to {} removed in routing table {}",path,service,this);
109 listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
114 public S getRoute(InstanceIdentifier<?> nodeInstance) {
115 S route = routes.get(nodeInstance);
119 return getDefaultRoute();
123 public Map<InstanceIdentifier<?>, S> getRoutes() {
124 return unmodifiableRoutes;
127 protected void removeAllReferences(S service) {
134 public String toString() {
135 return "RpcRoutingTableImpl [router=" + routerName + ", service=" + serviceType.getSimpleName() + ", context="
136 + contextType.getSimpleName() + "]";
141 private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
142 AbstractObjectRegistration<L>
143 implements ListenerRegistration<L> {
145 public SingletonListenerRegistration(L instance) {
151 protected void removeRegistration() {