Merge "Add exists method on DOMStoreReadTransaction and DOMDataReadTransaction"
[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.HashMap;
18 import java.util.Map;
19
20 public class RoutingTable implements Copier<RoutingTable>, Serializable {
21
22     private Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table = new HashMap<>();
23     private ActorRef router;
24
25     @Override
26     public RoutingTable copy() {
27         RoutingTable copy = new RoutingTable();
28         copy.setTable(new HashMap<>(table));
29         copy.setRouter(this.getRouter());
30
31         return copy;
32     }
33
34     public Option<Pair<ActorRef, Long>> getRouterFor(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
35         Long updatedTime = table.get(routeId);
36
37         if (updatedTime == null || router == null)
38             return Option.none();
39         else
40             return Option.option(new Pair<>(router, updatedTime));
41     }
42
43     public void addRoute(RpcRouter.RouteIdentifier<?,?,?> routeId){
44         table.put(routeId, System.currentTimeMillis());
45     }
46
47     public void removeRoute(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
48         table.remove(routeId);
49     }
50
51     public Boolean contains(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
52         return table.containsKey(routeId);
53     }
54
55     public Boolean isEmpty(){
56         return table.isEmpty();
57     }
58     ///
59     /// Getter, Setters
60     ///
61     //TODO: Remove public
62     public Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> getTable() {
63         return table;
64     }
65
66     void setTable(Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table) {
67         this.table = table;
68     }
69
70     public ActorRef getRouter() {
71         return router;
72     }
73
74     public void setRouter(ActorRef router) {
75         this.router = router;
76     }
77
78     @Override
79     public String toString() {
80         return "RoutingTable{" +
81                 "table=" + table +
82                 ", router=" + router +
83                 '}';
84     }
85 }