2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.remote.rpc;
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;
24 import java.util.HashSet;
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;
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");
37 this.rpcRegistry = rpcRegistry;
38 this.actorPath = actorPath;
42 public void onRouteChange(RouteChange<RpcRoutingContext, YangInstanceIdentifier> routeChange) {
43 Map<RpcRoutingContext, Set<YangInstanceIdentifier>> announcements = routeChange.getAnnouncements();
44 announce(getRouteIdentifiers(announcements));
46 Map<RpcRoutingContext, Set<YangInstanceIdentifier>> removals = routeChange.getRemovals();
47 remove(getRouteIdentifiers(removals));
52 * @param announcements
54 private void announce(Set<RpcRouter.RouteIdentifier<?, ?, ?>> announcements) {
55 LOG.debug("Announcing [{}]", announcements);
56 AddRoutedRpc addRpcMsg = new AddRoutedRpc(announcements, actorPath);
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());
69 private void remove(Set<RpcRouter.RouteIdentifier<?, ?, ?>> removals){
70 LOG.debug("Removing [{}]", removals);
71 RemoveRoutedRpc removeRpcMsg = new RemoveRoutedRpc(removals, actorPath);
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());
85 private Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRouteIdentifiers(Map<RpcRoutingContext, Set<YangInstanceIdentifier>> changes) {
86 RouteIdentifierImpl routeId = null;
87 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdSet = new HashSet<>();
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);