Bug 2853 : Adding Remote RPC MX bean
[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
18 import org.opendaylight.controller.remote.rpc.registry.gossip.Copier;
19 import org.opendaylight.controller.sal.connector.api.RpcRouter;
20
21 public class RoutingTable implements Copier<RoutingTable>, Serializable {
22     private static final long serialVersionUID = 5592610415175278760L;
23
24     private final Map<RpcRouter.RouteIdentifier<?, ?, ?>, Long> table = new HashMap<>();
25     private ActorRef router;
26
27     @Override
28     public RoutingTable copy() {
29         RoutingTable copy = new RoutingTable();
30         copy.table.putAll(table);
31         copy.setRouter(this.getRouter());
32
33         return copy;
34     }
35
36     public Option<Pair<ActorRef, Long>> getRouterFor(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
37         Long updatedTime = table.get(routeId);
38
39         if (updatedTime == null || router == null) {
40             return Option.none();
41         } else {
42             return Option.option(new Pair<>(router, updatedTime));
43         }
44     }
45
46     public Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRoutes() {
47         return table.keySet();
48     }
49
50     public void addRoute(RpcRouter.RouteIdentifier<?,?,?> routeId){
51         table.put(routeId, System.currentTimeMillis());
52     }
53
54     public void removeRoute(RpcRouter.RouteIdentifier<?, ?, ?> routeId){
55         table.remove(routeId);
56     }
57
58     public boolean contains(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     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 }