Fix typo in match types yang model
[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 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;
14
15 import java.util.Collections;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18 import java.util.concurrent.ConcurrentMap;
19
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;
30
31 class RpcRoutingTableImpl<C extends BaseIdentity, S extends RpcService> //
32 implements //
33         Mutable, //
34         RpcRoutingTable<C, S>, //
35         RouteChangePublisher<Class<? extends BaseIdentity>, InstanceIdentifier<?>> {
36
37     private static final Logger LOGGER = LoggerFactory.getLogger(RpcRoutingTableImpl.class);
38     private final String routerName;
39     private final Class<S> serviceType;
40
41     private final Class<C> contextType;
42     private final ConcurrentMap<InstanceIdentifier<?>, S> routes;
43     private final Map<InstanceIdentifier<?>, S> unmodifiableRoutes;
44
45     private RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>> listener;
46     private S defaultRoute;
47     
48     public RpcRoutingTableImpl(String routerName,Class<C> contextType, Class<S> serviceType) {
49         super();
50         this.routerName = routerName;
51         this.serviceType = serviceType;
52         this.contextType = contextType;
53         this.routes = new ConcurrentHashMap<>();
54         this.unmodifiableRoutes = Collections.unmodifiableMap(routes);
55     }
56
57     @Override
58     public void setDefaultRoute(S target) {
59         defaultRoute = target;
60     }
61
62     @Override
63     public S getDefaultRoute() {
64         return defaultRoute;
65     }
66
67     @Override
68     public <L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> ListenerRegistration<L> registerRouteChangeListener(
69             L listener) {
70         return (ListenerRegistration<L>) new SingletonListenerRegistration<L>(listener);
71     }
72         
73     @Override
74     public Class<C> getIdentifier() {
75         return contextType;
76     }
77
78     @Override
79     @SuppressWarnings("unchecked")
80     public void updateRoute(InstanceIdentifier<?> path, S service) {
81         S previous = this.routes.put(path, service);
82         
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));
88         }
89     }
90
91     
92     @Override
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));
101         }
102     }
103     
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));
110         }
111     }
112
113     @Override
114     public S getRoute(InstanceIdentifier<?> nodeInstance) {
115         S route = routes.get(nodeInstance);
116         if (route != null) {
117             return route;
118         }
119         return getDefaultRoute();
120     }
121
122     @Override
123     public Map<InstanceIdentifier<?>, S> getRoutes() {
124         return unmodifiableRoutes;
125     }
126     
127     protected void removeAllReferences(S service) {
128         
129     }
130     
131     
132
133     @Override
134     public String toString() {
135         return "RpcRoutingTableImpl [router=" + routerName + ", service=" + serviceType.getSimpleName() + ", context="
136                 + contextType.getSimpleName() + "]";
137     }
138
139
140
141     private class SingletonListenerRegistration<L extends RouteChangeListener<Class<? extends BaseIdentity>, InstanceIdentifier<?>>> extends
142             AbstractObjectRegistration<L>
143             implements ListenerRegistration<L> {
144
145         public SingletonListenerRegistration(L instance) {
146             super(instance);
147             listener = instance;
148         }
149
150         @Override
151         protected void removeRegistration() {
152             listener = null;
153         }
154     }
155 }