Merge "BUG-2599 Netconf notification manager initial API and Impl"
[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 org.opendaylight.controller.remote.rpc.registry.gossip.Copier;
17 import org.opendaylight.controller.sal.connector.api.RpcRouter;
18
19 public class RoutingTable implements Copier<RoutingTable>, Serializable {
20     private static final long serialVersionUID = 5592610415175278760L;
21
22     private final 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.table.putAll(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
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     public boolean isEmpty(){
57         return table.isEmpty();
58     }
59
60     public int size() {
61         return table.size();
62     }
63
64     public ActorRef getRouter() {
65         return router;
66     }
67
68     public void setRouter(ActorRef router) {
69         this.router = router;
70     }
71
72     @Override
73     public String toString() {
74         return "RoutingTable{" +
75                 "table=" + table +
76                 ", router=" + router +
77                 '}';
78     }
79 }