Adding more unit tests for remote rpc connector and Integrating routing table
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RoutedRpcListener.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
9 package org.opendaylight.controller.remote.rpc;
10
11
12 import akka.actor.ActorRef;
13 import com.google.common.base.Preconditions;
14 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
15 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
16 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
17 import org.opendaylight.controller.sal.connector.api.RpcRouter;
18 import org.opendaylight.controller.sal.core.api.RpcRoutingContext;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27
28 public class RoutedRpcListener implements RouteChangeListener<RpcRoutingContext, YangInstanceIdentifier>{
29   private static final Logger LOG = LoggerFactory.getLogger(RoutedRpcListener.class);
30   private final ActorRef rpcRegistry;
31
32   public RoutedRpcListener(ActorRef rpcRegistry) {
33     Preconditions.checkNotNull(rpcRegistry, "rpc registry actor should not be null");
34
35     this.rpcRegistry = rpcRegistry;
36   }
37
38   @Override
39   public void onRouteChange(RouteChange<RpcRoutingContext, YangInstanceIdentifier> routeChange) {
40     Map<RpcRoutingContext, Set<YangInstanceIdentifier>> announcements = routeChange.getAnnouncements();
41     if(announcements != null && announcements.size() > 0){
42       announce(getRouteIdentifiers(announcements));
43     }
44
45     Map<RpcRoutingContext, Set<YangInstanceIdentifier>> removals = routeChange.getRemovals();
46     if(removals != null && removals.size() > 0 ) {
47       remove(getRouteIdentifiers(removals));
48     }
49   }
50
51   /**
52    *
53    * @param announcements
54    */
55   private void announce(Set<RpcRouter.RouteIdentifier<?, ?, ?>> announcements) {
56     LOG.debug("Announcing [{}]", announcements);
57     RpcRegistry.Messages.AddOrUpdateRoutes addRpcMsg = new RpcRegistry.Messages.AddOrUpdateRoutes(new ArrayList<>(announcements));
58     rpcRegistry.tell(addRpcMsg, ActorRef.noSender());
59   }
60
61   /**
62    *
63    * @param removals
64    */
65   private void remove(Set<RpcRouter.RouteIdentifier<?, ?, ?>> removals){
66     LOG.debug("Removing [{}]", removals);
67     RpcRegistry.Messages.RemoveRoutes removeRpcMsg = new RpcRegistry.Messages.RemoveRoutes(new ArrayList<>(removals));
68     rpcRegistry.tell(removeRpcMsg, ActorRef.noSender());
69   }
70
71   /**
72    *
73    * @param changes
74    * @return
75    */
76   private Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRouteIdentifiers(Map<RpcRoutingContext, Set<YangInstanceIdentifier>> changes) {
77     RouteIdentifierImpl routeId = null;
78     Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdSet = new HashSet<>();
79
80     for (RpcRoutingContext context : changes.keySet()){
81       for (YangInstanceIdentifier instanceId : changes.get(context)){
82         routeId = new RouteIdentifierImpl(null, context.getRpc(), instanceId);
83         routeIdSet.add(routeId);
84       }
85     }
86     return routeIdSet;
87   }
88 }