Gossip based eventually consistent RPC Registry.
[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 org.opendaylight.controller.remote.rpc.registry.gossip.Copier;
14 import org.opendaylight.controller.sal.connector.api.RpcRouter;
15
16 import java.io.Serializable;
17 import java.util.Collections;
18 import java.util.HashMap;
19 import java.util.Map;
20
21 public class RoutingTable implements Copier<RoutingTable>, Serializable {
22
23     private 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.setTable(Collections.unmodifiableMap(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     public void addRoute(RpcRouter.RouteIdentifier<?,?,?> routeId){
45         table.put(routeId, System.currentTimeMillis());
46     }
47
48     public void removeRoute(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
49         table.remove(routeId);
50     }
51
52     public Boolean contains(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
53         return table.containsKey(routeId);
54     }
55
56     ///
57     /// Getter, Setters
58     ///
59     //TODO: Remove public
60     public Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> getTable() {
61         return table;
62     }
63
64     void setTable(Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table) {
65         this.table = table;
66     }
67
68     public ActorRef getRouter() {
69         return router;
70     }
71
72     public void setRouter(ActorRef router) {
73         this.router = router;
74     }
75
76     @Override
77     public String toString() {
78         return "RoutingTable{" +
79                 "table=" + table +
80                 ", router=" + router +
81                 '}';
82     }
83 }