Reuse PEM provider in netconf-testtool.
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / main / java / org / opendaylight / controller / sal / binding / codegen / impl / RpcRoutingTableImpl.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.binding.codegen.impl;
9
10 import java.util.Collections;
11 import java.util.Map;
12 import java.util.concurrent.ConcurrentHashMap;
13 import java.util.concurrent.ConcurrentMap;
14
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;
27
28 class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> //
29 implements //
30         Mutable, //
31         RpcRoutingTable<C, S>, //
32         RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
33
34     private static final Logger LOGGER = LoggerFactory.getLogger(RpcRoutingTableImpl.class);
35     private final String routerName;
36     private final Class<S> serviceType;
37
38     private final Class<C> contextType;
39     private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
40     private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
41
42     private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
43     private S defaultRoute;
44
45     public RpcRoutingTableImpl(String routerName,Class<C> contextType, Class<S> serviceType) {
46         super();
47         this.routerName = routerName;
48         this.serviceType = serviceType;
49         this.contextType = contextType;
50         this.routes = new ConcurrentHashMap<>();
51         this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
52     }
53
54     @Override
55     public void setDefaultRoute(S target) {
56         defaultRoute = target;
57     }
58
59     @Override
60     public S getDefaultRoute() {
61         return defaultRoute;
62     }
63
64     @Override
65     public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
66             L listener) {
67         return new SingletonListenerRegistration<L>(listener);
68     }
69
70     @Override
71     public Class<C> getIdentifier() {
72         return contextType;
73     }
74
75     @Override
76     @SuppressWarnings("unchecked")
77     public void updateRoute(InstanceIdentifier<?> path, S service) {
78         S previous = this.routes.put(path, service);
79
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));
85         }
86     }
87
88
89     @Override
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));
98         }
99     }
100
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));
107         }
108     }
109
110     @Override
111     public S getRoute(InstanceIdentifier<?> nodeInstance) {
112         S route = routes.get(nodeInstance);
113         if (route != null) {
114             return route;
115         }
116         return getDefaultRoute();
117     }
118
119     @Override
120     public Map<InstanceIdentifier<?>, S> getRoutes() {
121         return unmodifiableRoutes;
122     }
123
124     protected void removeAllReferences(S service) {
125
126     }
127
128
129
130     @Override
131     public String toString() {
132         return "RpcRoutingTableImpl [router=" + routerName + ", service=" + serviceType.getSimpleName() + ", context="
133                 + contextType.getSimpleName() + "]";
134     }
135
136
137
138     private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
139             AbstractObjectRegistration<L>
140             implements ListenerRegistration<L> {
141
142         public SingletonListenerRegistration(L instance) {
143             super(instance);
144             listener = instance;
145         }
146
147         @Override
148         protected void removeRegistration() {
149             listener = null;
150         }
151     }
152 }