9e69fa66df463c25f0c9c29562c9d1f015e642a8
[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     RoutingTable copy() {
37         return new RoutingTable(router, new HashMap<>(table));
38     }
39
40     @Override
41     public Optional<ActorRef> getWatchActor() {
42         return Optional.of(router);
43     }
44
45     public Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRoutes() {
46         return table.keySet();
47     }
48
49     public void addRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
50         table.put(routeId, System.currentTimeMillis());
51     }
52
53     public void removeRoute(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
54         table.remove(routeId);
55     }
56
57     public boolean contains(final RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
58         return table.containsKey(routeId);
59     }
60
61     public boolean isEmpty() {
62         return table.isEmpty();
63     }
64
65     public int size() {
66         return table.size();
67     }
68
69     public ActorRef getRouter() {
70         return router;
71     }
72
73     @Override
74     public String toString() {
75         return "RoutingTable{" + "table=" + table + ", router=" + router + '}';
76     }
77 }