dce7e20219d377a3f8d6747064c55af76cb2fcd8
[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.messages.AddRoutedRpc;
17 import org.opendaylight.controller.remote.rpc.messages.RemoveRoutedRpc;
18 import org.opendaylight.controller.sal.connector.api.RpcRouter;
19 import org.opendaylight.controller.sal.core.api.RpcRoutingContext;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
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   private final String actorPath;
32
33   public RoutedRpcListener(ActorRef rpcRegistry, String actorPath) {
34     Preconditions.checkNotNull(rpcRegistry, "rpc registry actor should not be null");
35     Preconditions.checkNotNull(actorPath, "actor path of rpc broker on current node should not be null");
36
37     this.rpcRegistry = rpcRegistry;
38     this.actorPath = actorPath;
39   }
40
41   @Override
42   public void onRouteChange(RouteChange<RpcRoutingContext, YangInstanceIdentifier> routeChange) {
43     Map<RpcRoutingContext, Set<YangInstanceIdentifier>> announcements = routeChange.getAnnouncements();
44     announce(getRouteIdentifiers(announcements));
45
46     Map<RpcRoutingContext, Set<YangInstanceIdentifier>> removals = routeChange.getRemovals();
47     remove(getRouteIdentifiers(removals));
48   }
49
50   /**
51    *
52    * @param announcements
53    */
54   private void announce(Set<RpcRouter.RouteIdentifier<?, ?, ?>> announcements) {
55     LOG.debug("Announcing [{}]", announcements);
56     AddRoutedRpc addRpcMsg = new AddRoutedRpc(announcements, actorPath);
57     try {
58       ActorUtil.executeLocalOperation(rpcRegistry, addRpcMsg, ActorUtil.LOCAL_ASK_DURATION, ActorUtil.LOCAL_AWAIT_DURATION);
59     } catch (Exception e) {
60       // Just logging it because Akka API throws this exception
61       LOG.error(e.toString());
62     }
63   }
64
65   /**
66    *
67    * @param removals
68    */
69   private void remove(Set<RpcRouter.RouteIdentifier<?, ?, ?>> removals){
70     LOG.debug("Removing [{}]", removals);
71     RemoveRoutedRpc removeRpcMsg = new RemoveRoutedRpc(removals, actorPath);
72     try {
73       ActorUtil.executeLocalOperation(rpcRegistry, removeRpcMsg, ActorUtil.LOCAL_ASK_DURATION, ActorUtil.LOCAL_AWAIT_DURATION);
74     } catch (Exception e) {
75       // Just logging it because Akka API throws this exception
76       LOG.error(e.toString());
77     }
78   }
79
80   /**
81    *
82    * @param changes
83    * @return
84    */
85   private Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRouteIdentifiers(Map<RpcRoutingContext, Set<YangInstanceIdentifier>> changes) {
86     RouteIdentifierImpl routeId = null;
87     Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdSet = new HashSet<>();
88
89     for (RpcRoutingContext context : changes.keySet()){
90       for (YangInstanceIdentifier instanceId : changes.get(context)){
91         routeId = new RouteIdentifierImpl(null, context.getRpc(), instanceId);
92         routeIdSet.add(routeId);
93       }
94     }
95     return routeIdSet;
96   }
97 }