09a987f7e31d289c4f02656e6d187f641d23140e
[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 akka.japi.Option;
12 import akka.japi.Pair;
13 import java.io.Serializable;
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Set;
17 import org.opendaylight.controller.remote.rpc.registry.gossip.Copier;
18 import org.opendaylight.controller.sal.connector.api.RpcRouter;
19
20 public class RoutingTable implements Copier<RoutingTable>, Serializable {
21     private static final long serialVersionUID = 5592610415175278760L;
22
23     private final Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table = new HashMap<>();
24     private ActorRef router;
25
26     @Override
27     public RoutingTable copy() {
28         RoutingTable copy = new RoutingTable();
29         copy.table.putAll(table);
30         copy.setRouter(this.getRouter());
31
32         return copy;
33     }
34
35     public Option<Pair<ActorRef, Long>> getRouterFor(RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
36         Long updatedTime = table.get(routeId);
37
38         if (updatedTime == null || router == null) {
39             return Option.none();
40         } else {
41             return Option.option(new Pair<>(router, updatedTime));
42         }
43     }
44
45     public Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRoutes() {
46         return table.keySet();
47     }
48
49     public void addRoute(RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
50         table.put(routeId, System.currentTimeMillis());
51     }
52
53     public void removeRoute(RpcRouter.RouteIdentifier<?, ?, ?> routeId) {
54         table.remove(routeId);
55     }
56
57     public boolean contains(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     public void setRouter(ActorRef router) {
74         this.router = router;
75     }
76
77     @Override
78     public String toString() {
79         return "RoutingTable{" + "table=" + table + ", router=" + router + '}';
80     }
81 }