Merge "Fixed for bug 1197"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / registry / RoutingTable.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
9 package org.opendaylight.controller.remote.rpc.registry;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableSet;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import java.util.Collections;
17 import java.util.Iterator;
18 import java.util.LinkedHashSet;
19 import java.util.Set;
20 import java.util.concurrent.ConcurrentHashMap;
21 import java.util.concurrent.ConcurrentMap;
22
23 public class RoutingTable<I, R> {
24
25   private final Logger LOG = LoggerFactory.getLogger(RoutingTable.class);
26
27   private ConcurrentMap<I,R> globalRpcMap = new ConcurrentHashMap<>();
28   private ConcurrentMap<I, LinkedHashSet<R>> routedRpcMap = new ConcurrentHashMap<>();
29
30   public ConcurrentMap<I, R> getGlobalRpcMap() {
31     return globalRpcMap;
32   }
33
34   public ConcurrentMap<I, LinkedHashSet<R>> getRoutedRpcMap() {
35     return routedRpcMap;
36   }
37
38   public R getGlobalRoute(final I routeId) {
39     Preconditions.checkNotNull(routeId, "getGlobalRoute: routeId cannot be null!");
40     return globalRpcMap.get(routeId);
41   }
42
43   public void addGlobalRoute(final I routeId, final R route) {
44     Preconditions.checkNotNull(routeId, "addGlobalRoute: routeId cannot be null!");
45     Preconditions.checkNotNull(route, "addGlobalRoute: route cannot be null!");
46     LOG.debug("addGlobalRoute: adding  a new route with id[{}] and value [{}]", routeId, route);
47     if(globalRpcMap.putIfAbsent(routeId, route) != null) {
48       LOG.debug("A route already exist for route id [{}] ", routeId);
49     }
50   }
51
52   public void removeGlobalRoute(final I routeId) {
53     Preconditions.checkNotNull(routeId, "removeGlobalRoute: routeId cannot be null!");
54     LOG.debug("removeGlobalRoute: removing  a new route with id [{}]", routeId);
55     globalRpcMap.remove(routeId);
56   }
57
58   public Set<R> getRoutedRpc(final I routeId) {
59     Preconditions.checkNotNull(routeId, "getRoutes: routeId cannot be null!");
60     Set<R> routes = routedRpcMap.get(routeId);
61
62     if (routes == null) {
63       return Collections.emptySet();
64     }
65
66     return ImmutableSet.copyOf(routes);
67   }
68
69   public R getLastAddedRoutedRpc(final I routeId) {
70
71     Set<R> routes = getRoutedRpc(routeId);
72
73     if (routes.isEmpty()) {
74       return null;
75     }
76
77     R route = null;
78     Iterator<R> iter = routes.iterator();
79     while (iter.hasNext()) {
80       route = iter.next();
81     }
82
83     return route;
84   }
85
86   public void addRoutedRpc(final I routeId, final R route)   {
87     Preconditions.checkNotNull(routeId, "addRoute: routeId cannot be null");
88     Preconditions.checkNotNull(route, "addRoute: route cannot be null");
89     LOG.debug("addRoute: adding a route with k/v [{}/{}]", routeId, route);
90     threadSafeAdd(routeId, route);
91   }
92
93   public void addRoutedRpcs(final Set<I> routeIds, final R route) {
94     Preconditions.checkNotNull(routeIds, "addRoutes: routeIds must not be null");
95     for (I routeId : routeIds){
96       addRoutedRpc(routeId, route);
97     }
98   }
99
100   public void removeRoute(final I routeId, final R route) {
101     Preconditions.checkNotNull(routeId, "removeRoute: routeId cannot be null!");
102     Preconditions.checkNotNull(route, "removeRoute: route cannot be null!");
103
104     LinkedHashSet<R> routes = routedRpcMap.get(routeId);
105     if (routes == null) {
106       return;
107     }
108     LOG.debug("removeRoute: removing  a new route with k/v [{}/{}]", routeId, route);
109     threadSafeRemove(routeId, route);
110   }
111
112   public void removeRoutes(final Set<I> routeIds, final R route) {
113     Preconditions.checkNotNull(routeIds, "removeRoutes: routeIds must not be null");
114     for (I routeId : routeIds){
115       removeRoute(routeId, route);
116     }
117   }
118
119   /**
120    * This method guarantees that no 2 thread over write each other's changes.
121    * Just so that we dont end up in infinite loop, it tries for 100 times then throw
122    */
123   private void threadSafeAdd(final I routeId, final R route) {
124
125     for (int i=0;i<100;i++){
126
127       LinkedHashSet<R> updatedRoutes = new LinkedHashSet<>();
128       updatedRoutes.add(route);
129       LinkedHashSet<R> oldRoutes = routedRpcMap.putIfAbsent(routeId, updatedRoutes);
130       if (oldRoutes == null) {
131         return;
132       }
133
134       updatedRoutes = new LinkedHashSet<>(oldRoutes);
135       updatedRoutes.add(route);
136
137       if (routedRpcMap.replace(routeId, oldRoutes, updatedRoutes)) {
138         return;
139       }
140     }
141     //the method did not already return means it failed to add route in 100 attempts
142     throw new IllegalStateException("Failed to add route [" + routeId + "]");
143   }
144
145   /**
146    * This method guarantees that no 2 thread over write each other's changes.
147    * Just so that we dont end up in infinite loop, it tries for 100 times then throw
148    */
149   private void threadSafeRemove(final I routeId, final R route) {
150     LinkedHashSet<R> updatedRoutes = null;
151     for (int i=0;i<100;i++){
152       LinkedHashSet<R> oldRoutes = routedRpcMap.get(routeId);
153
154       // if route to be deleted is the only entry in the set then remove routeId from the cache
155       if ((oldRoutes.size() == 1) && oldRoutes.contains(route)){
156         routedRpcMap.remove(routeId);
157         return;
158       }
159
160       // if there are multiple routes for this routeId, remove the route to be deleted only from the set.
161       updatedRoutes = new LinkedHashSet<>(oldRoutes);
162       updatedRoutes.remove(route);
163       if (routedRpcMap.replace(routeId, oldRoutes, updatedRoutes)) {
164         return;
165       }
166
167     }
168     //the method did not already return means it failed to remove route in 100 attempts
169     throw new IllegalStateException("Failed to remove route [" + routeId + "]");
170   }
171 }