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 java.util.ArrayList;
15 import java.util.HashSet;
18 import org.opendaylight.controller.md.sal.common.api.routing.RouteChange;
19 import org.opendaylight.controller.md.sal.common.api.routing.RouteChangeListener;
20 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
21 import org.opendaylight.controller.sal.connector.api.RpcRouter;
22 import org.opendaylight.controller.sal.core.api.RpcRoutingContext;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
27 public class RoutedRpcListener implements RouteChangeListener<RpcRoutingContext, YangInstanceIdentifier>{
28 private static final Logger LOG = LoggerFactory.getLogger(RoutedRpcListener.class);
29 private final ActorRef rpcRegistry;
31 public RoutedRpcListener(final ActorRef rpcRegistry) {
32 Preconditions.checkNotNull(rpcRegistry, "rpc registry actor should not be null");
33 this.rpcRegistry = rpcRegistry;
37 public void onRouteChange(final RouteChange<RpcRoutingContext, YangInstanceIdentifier> routeChange) {
38 final Map<RpcRoutingContext, Set<YangInstanceIdentifier>> announcements = routeChange.getAnnouncements();
39 if(announcements != null && announcements.size() > 0){
40 announce(getRouteIdentifiers(announcements));
43 final Map<RpcRoutingContext, Set<YangInstanceIdentifier>> removals = routeChange.getRemovals();
44 if(removals != null && removals.size() > 0 ) {
45 remove(getRouteIdentifiers(removals));
51 * @param announcements
53 private void announce(final Set<RpcRouter.RouteIdentifier<?, ?, ?>> announcements) {
54 if(LOG.isDebugEnabled()) {
55 LOG.debug("Announcing [{}]", announcements);
57 final RpcRegistry.Messages.AddOrUpdateRoutes addRpcMsg =
58 new RpcRegistry.Messages.AddOrUpdateRoutes(new ArrayList<>(announcements));
59 rpcRegistry.tell(addRpcMsg, ActorRef.noSender());
66 private void remove(final Set<RpcRouter.RouteIdentifier<?, ?, ?>> removals){
67 if(LOG.isDebugEnabled()) {
68 LOG.debug("Removing [{}]", removals);
70 final RpcRegistry.Messages.RemoveRoutes removeRpcMsg =
71 new RpcRegistry.Messages.RemoveRoutes(new ArrayList<>(removals));
72 rpcRegistry.tell(removeRpcMsg, ActorRef.noSender());
80 private Set<RpcRouter.RouteIdentifier<?, ?, ?>> getRouteIdentifiers(
81 final Map<RpcRoutingContext, Set<YangInstanceIdentifier>> changes) {
83 final Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIdSet = new HashSet<>();
84 for (final RpcRoutingContext context : changes.keySet()){
85 for (final YangInstanceIdentifier instanceId : changes.get(context)){
86 final RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, context.getRpc(), instanceId);
87 routeIdSet.add(routeId);