2aaac5a78ed531fc830bfca7540d2603a2e0f41b
[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     if(LOG.isDebugEnabled()) {
57         LOG.debug("Announcing [{}]", announcements);
58     }
59     RpcRegistry.Messages.AddOrUpdateRoutes addRpcMsg = new RpcRegistry.Messages.AddOrUpdateRoutes(new ArrayList<>(announcements));
60     rpcRegistry.tell(addRpcMsg, ActorRef.noSender());
61   }
62
63   /**
64    *
65    * @param removals
66    */
67   private void remove(Set<RpcRouter.RouteIdentifier<?, ?, ?>> removals){
68     if(LOG.isDebugEnabled()) {
69         LOG.debug("Removing [{}]", removals);
70     }
71     RpcRegistry.Messages.RemoveRoutes removeRpcMsg = new RpcRegistry.Messages.RemoveRoutes(new ArrayList<>(removals));
72     rpcRegistry.tell(removeRpcMsg, ActorRef.noSender());
73   }
74
75   /**
76    *
77    * @param changes
78    * @return
79    */
80   private Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRouteIdentifiers(Map<RpcRoutingContext, Set<YangInstanceIdentifier>> changes) {
81     RouteIdentifierImpl routeId = null;
82     Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdSet = new HashSet<>();
83
84     for (RpcRoutingContext context : changes.keySet()){
85       for (YangInstanceIdentifier instanceId : changes.get(context)){
86         routeId = new RouteIdentifierImpl(null, context.getRpc(), instanceId);
87         routeIdSet.add(routeId);
88       }
89     }
90     return routeIdSet;
91   }
92 }