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