BUG-3128: rework sal-remoterpc-connector
[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 package org.opendaylight.controller.remote.rpc.registry;
9
10 import akka.actor.ActorRef;
11 import com.google.common.base.Preconditions;
12 import java.io.Serializable;
13 import java.util.HashMap;
14 import java.util.Map;
15 import java.util.Set;
16 import org.opendaylight.controller.remote.rpc.registry.gossip.Copier;
17 import org.opendaylight.controller.sal.connector.api.RpcRouter;
18 import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
19
20 public class RoutingTable implements Copier<RoutingTable>, Serializable {
21     private static final long serialVersionUID = 5592610415175278760L;
22
23     private final Map<RouteIdentifier<?, ?, ?>, Long> table;
24     private final ActorRef router;
25
26     private RoutingTable(final ActorRef router, final Map<RouteIdentifier<?, ?, ?>, Long> table) {
27         this.router = Preconditions.checkNotNull(router);
28         this.table = Preconditions.checkNotNull(table);
29     }
30
31     RoutingTable(final ActorRef router) {
32         this(router, new HashMap<>());
33     }
34
35     @Override
36     public RoutingTable copy() {
37         return new RoutingTable(router, new HashMap<>(table));
38     }
39
40     public Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRoutes() {
41         return table.keySet();
42     }
43
44     public void addRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
45         table.put(routeId, System.currentTimeMillis());
46     }
47
48     public void removeRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
49         table.remove(routeId);
50     }
51
52     public boolean contains(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
53         return table.containsKey(routeId);
54     }
55
56     public boolean isEmpty() {
57         return table.isEmpty();
58     }
59
60     public int size() {
61         return table.size();
62     }
63
64     public ActorRef getRouter() {
65         return router;
66     }
67
68     @Override
69     public String toString() {
70         return "RoutingTable{" + "table=" + table + ", router=" + router + '}';
71     }
72 }