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;
11 import java.util.Iterator;
13 import java.util.concurrent.ConcurrentHashMap;
14 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 final class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> implements
30 RpcRoutingTable<C, S>, //
31 RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
33 private static final Logger LOGGER = LoggerFactory.getLogger(RpcRoutingTableImpl.class);
34 private final String routerName;
35 private final Class<S> serviceType;
37 private final Class<C> contextType;
38 private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
39 private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
41 private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
42 private S defaultRoute;
44 public RpcRoutingTableImpl(final String routerName,final Class<C> contextType, final Class<S> serviceType) {
46 this.routerName = routerName;
47 this.serviceType = serviceType;
48 this.contextType = contextType;
49 this.routes = new ConcurrentHashMap<>();
50 this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
54 public void setDefaultRoute(final S target) {
55 defaultRoute = target;
59 public S getDefaultRoute() {
64 public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
66 return new SingletonListenerRegistration<L>(listener);
70 public Class<C> getIdentifier() {
75 @SuppressWarnings("unchecked")
76 public void updateRoute(final InstanceIdentifier<?> path, final S service) {
77 S previous = this.routes.put(path, service);
79 LOGGER.debug("Route {} updated to {} in routing table {}",path,service,this);
80 @SuppressWarnings("rawtypes")
81 RouteChangeListener listenerCapture = listener;
82 if (previous == null && listenerCapture != null) {
83 listenerCapture.onRouteChange(RoutingUtils.announcementChange(contextType, path));
89 @SuppressWarnings("unchecked")
90 public void removeRoute(final InstanceIdentifier<?> path) {
91 S previous = this.routes.remove(path);
92 LOGGER.debug("Route {} to {} removed in routing table {}",path,previous,this);
93 @SuppressWarnings("rawtypes")
94 RouteChangeListener listenerCapture = listener;
95 if (previous != null && listenerCapture != null) {
96 listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
100 void removeRoute(final InstanceIdentifier<?> path, final S service) {
101 @SuppressWarnings("rawtypes")
102 RouteChangeListener listenerCapture = listener;
103 if (routes.remove(path, service) && listenerCapture != null) {
104 LOGGER.debug("Route {} to {} removed in routing table {}",path,service,this);
105 listenerCapture.onRouteChange(RoutingUtils.removalChange(contextType, path));
110 public S getRoute(final InstanceIdentifier<?> nodeInstance) {
111 S route = routes.get(nodeInstance);
115 return getDefaultRoute();
119 public Map<InstanceIdentifier<?>, S> getRoutes() {
120 return unmodifiableRoutes;
123 void removeAllReferences(final S service) {
124 // FIXME: replace this via properly-synchronized BiMap (or something)
125 final Iterator<S> it = routes.values().iterator();
126 while (it.hasNext()) {
127 final S s = it.next();
128 if (service.equals(s)) {
135 public String toString() {
136 return "RpcRoutingTableImpl [router=" + routerName + ", service=" + serviceType.getSimpleName() + ", context="
137 + contextType.getSimpleName() + "]";
140 private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
141 AbstractObjectRegistration<L>
142 implements ListenerRegistration<L> {
144 public SingletonListenerRegistration(final L instance) {
150 protected void removeRegistration() {