2 * Copyright (c) 2013 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
9 package org.opendaylight.controller.sal.connector.remoterpc.impl;
11 import com.google.common.base.Preconditions;
12 import org.apache.felix.dm.Component;
13 import org.opendaylight.controller.clustering.services.CacheConfigException;
14 import org.opendaylight.controller.clustering.services.CacheExistException;
15 import org.opendaylight.controller.clustering.services.CacheListenerAddException;
16 import org.opendaylight.controller.clustering.services.ICacheUpdateAware;
17 import org.opendaylight.controller.clustering.services.IClusterGlobalServices;
18 import org.opendaylight.controller.clustering.services.IClusterServices;
19 import org.opendaylight.controller.sal.connector.remoterpc.api.RouteChangeListener;
20 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTable;
21 import org.opendaylight.controller.sal.connector.remoterpc.api.RoutingTableException;
22 import org.opendaylight.controller.sal.connector.remoterpc.api.SystemException;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
26 import javax.transaction.HeuristicMixedException;
27 import javax.transaction.HeuristicRollbackException;
28 import javax.transaction.NotSupportedException;
29 import javax.transaction.RollbackException;
30 import java.util.Collections;
31 import java.util.EnumSet;
32 import java.util.HashSet;
34 import java.util.concurrent.ConcurrentMap;
39 public class RoutingTableImpl<I, R> implements RoutingTable<I, R>, ICacheUpdateAware<I, R> {
40 public static final String ROUTING_TABLE_GLOBAL_CACHE = "routing_table_global_cache";
42 private Logger log = LoggerFactory.getLogger(RoutingTableImpl.class);
44 private IClusterGlobalServices clusterGlobalServices = null;
45 private RoutingTableImpl routingTableInstance = null;
46 private ConcurrentMap routingTableCache = null;
47 private Set<RouteChangeListener> routeChangeListeners = Collections
48 .synchronizedSet(new HashSet<RouteChangeListener>());
50 public RoutingTableImpl() {
54 public void addRoute(I routeId, R route) throws RoutingTableException {
55 throw new UnsupportedOperationException(" Not implemented yet!");
59 public void addGlobalRoute(I routeId, R route) throws RoutingTableException, SystemException {
60 Preconditions.checkNotNull(routeId, "addGlobalRoute: routeId cannot be null!");
61 Preconditions.checkNotNull(route, "addGlobalRoute: route cannot be null!");
64 Set<R> existingRoute = null;
65 // ok does the global route is already registered ?
66 if ((existingRoute = getRoutes(routeId)) == null) {
68 if (log.isDebugEnabled()) {
69 log.debug("addGlobalRoute: adding a new route with id" + routeId + " and value = "
72 // lets start a transaction
73 clusterGlobalServices.tbegin();
74 Set<R> routes = new HashSet<R>();
76 routingTableCache.put(routeId, routes);
77 clusterGlobalServices.tcommit();
79 throw new DuplicateRouteException(" There is already existing route " + existingRoute);
82 } catch (NotSupportedException e) {
83 throw new RoutingTableException("Transaction error - while trying to create route id="
84 + routeId + "with route" + route, e);
85 } catch (HeuristicRollbackException e) {
86 throw new RoutingTableException("Transaction error - while trying to create route id="
87 + routeId + "with route" + route, e);
88 } catch (RollbackException e) {
89 throw new RoutingTableException("Transaction error - while trying to create route id="
90 + routeId + "with route" + route, e);
91 } catch (HeuristicMixedException e) {
92 throw new RoutingTableException("Transaction error - while trying to create route id="
93 + routeId + "with route" + route, e);
94 } catch (javax.transaction.SystemException e) {
95 throw new SystemException("System error occurred - while trying to create with value", e);
101 public void removeRoute(I routeId, R route) {
102 throw new UnsupportedOperationException("Not implemented yet!");
106 public void removeGlobalRoute(I routeId) throws RoutingTableException, SystemException {
107 Preconditions.checkNotNull(routeId, "removeGlobalRoute: routeId cannot be null!");
109 if (log.isDebugEnabled()) {
110 log.debug("removeGlobalRoute: removing a new route with id" + routeId);
112 // lets start a transaction
113 clusterGlobalServices.tbegin();
115 routingTableCache.remove(routeId);
116 clusterGlobalServices.tcommit();
118 } catch (NotSupportedException e) {
119 throw new RoutingTableException("Transaction error - while trying to remove route id="
121 } catch (HeuristicRollbackException e) {
122 throw new RoutingTableException("Transaction error - while trying to remove route id="
124 } catch (RollbackException e) {
125 throw new RoutingTableException("Transaction error - while trying to remove route id="
127 } catch (HeuristicMixedException e) {
128 throw new RoutingTableException("Transaction error - while trying to remove route id="
130 } catch (javax.transaction.SystemException e) {
131 throw new SystemException("System error occurred - while trying to remove with value", e);
136 public Set<R> getRoutes(I routeId) {
138 // Note: currently works for global routes only wherein there is just single
140 Preconditions.checkNotNull(routeId, "getARoute: routeId cannot be null!");
141 return (Set<R>) routingTableCache.get(routeId);
145 public R getARoute(I routeId) {
146 throw new UnsupportedOperationException("Not implemented yet!");
150 * @deprecated doesn't do anything will be removed once listeners used
151 * whiteboard pattern Registers listener for sending any change
156 public void registerRouteChangeListener(RouteChangeListener listener) {
160 public void setRouteChangeListener(RouteChangeListener rcl) {
162 routeChangeListeners.add(rcl);
164 log.warn("setRouteChangeListener called with null listener");
168 public void unSetRouteChangeListener(RouteChangeListener rcl) {
170 routeChangeListeners.remove(rcl);
172 log.warn("unSetRouteChangeListener called with null listener");
177 * Returning the set of route change listeners for Unit testing Note: the
178 * package scope is default
180 * @return List of registered RouteChangeListener<I,R> listeners
182 Set<RouteChangeListener> getRegisteredRouteChangeListeners() {
183 return routeChangeListeners;
186 public void setClusterGlobalServices(IClusterGlobalServices clusterGlobalServices) {
187 this.clusterGlobalServices = clusterGlobalServices;
190 public void unsetClusterGlobalServices(IClusterGlobalServices clusterGlobalServices) {
191 if((clusterGlobalServices != null ) && (this.clusterGlobalServices.equals(clusterGlobalServices))){
192 this.clusterGlobalServices = null;
197 * Creates the Routing Table clustered global services cache
199 * @throws CacheExistException
200 * -- cluster global services exception when cache exist
201 * @throws CacheConfigException
202 * -- cluster global services exception during cache config
203 * @throws CacheListenerAddException
204 * -- cluster global services exception during adding of listener
207 void createRoutingTableCache() throws CacheExistException, CacheConfigException,
208 CacheListenerAddException {
209 // TBD: HOW DO WE DECIDE ON PROPERTIES OF THE CACHE i.e. what duration it
210 // should be caching?
212 // let us check here if the cache already exists -- if so don't create
213 if (!clusterGlobalServices.existCache(ROUTING_TABLE_GLOBAL_CACHE)) {
215 if (log.isDebugEnabled()) {
216 log.debug("createRoutingTableCache: creating a new routing table cache "
217 + ROUTING_TABLE_GLOBAL_CACHE);
219 routingTableCache = clusterGlobalServices.createCache(ROUTING_TABLE_GLOBAL_CACHE,
220 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
222 if (log.isDebugEnabled()) {
223 log.debug("createRoutingTableCache: found existing routing table cache "
224 + ROUTING_TABLE_GLOBAL_CACHE);
226 routingTableCache = clusterGlobalServices.getCache(ROUTING_TABLE_GLOBAL_CACHE);
232 * Function called by the dependency manager when all the required
233 * dependencies are satisfied
236 void init(Component c) {
239 createRoutingTableCache();
240 } catch (CacheExistException e) {
241 throw new IllegalStateException("could not construct routing table cache");
242 } catch (CacheConfigException e) {
243 throw new IllegalStateException("could not construct routing table cache");
244 } catch (CacheListenerAddException e) {
245 throw new IllegalStateException("could not construct routing table cache");
250 * Get routing table method is useful for unit testing <note>It has package
253 ConcurrentMap getRoutingTableCache() {
254 return this.routingTableCache;
258 * Invoked when a new entry is available in the cache, the key is only
259 * provided, the value will come as an entryUpdate invocation
262 * Key for the entry just created
264 * name of the cache for which update has been received
266 * true if the event is generated from this node
269 public void entryCreated(I key, String cacheName, boolean originLocal) {
270 // TBD: do we require this.
271 if (log.isDebugEnabled()) {
272 log.debug("RoutingTableUpdates: entryCreated routeId = " + key + " cacheName=" + cacheName);
277 * Called anytime a given entry is updated
280 * Key for the entry modified
282 * the new value the key will have
284 * name of the cache for which update has been received
286 * true if the event is generated from this node
289 public void entryUpdated(I key, R new_value, String cacheName, boolean originLocal) {
290 if (log.isDebugEnabled()) {
291 log.debug("RoutingTableUpdates: entryUpdated routeId = " + key + ",value = " + new_value
292 + " ,cacheName=" + cacheName + " originLocal="+originLocal);
295 for (RouteChangeListener rcl : routeChangeListeners) {
296 rcl.onRouteUpdated(key, new_value);
302 * Called anytime a given key is removed from the ConcurrentHashMap we are
306 * Key of the entry removed
308 * name of the cache for which update has been received
310 * true if the event is generated from this node
313 public void entryDeleted(I key, String cacheName, boolean originLocal) {
314 if (log.isDebugEnabled()) {
315 log.debug("RoutingTableUpdates: entryUpdated routeId = " + key + " local = " + originLocal
316 + " cacheName=" + cacheName + " originLocal="+originLocal);
319 for (RouteChangeListener rcl : routeChangeListeners) {
320 rcl.onRouteDeleted(key);