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
8 package org.opendaylight.controller.remote.rpc;
10 import akka.actor.ActorRef;
11 import com.google.common.base.Preconditions;
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.List;
15 import javax.annotation.Nonnull;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
19 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
20 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.AddOrUpdateRoutes;
21 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.RemoveRoutes;
22 import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * A {@link DOMRpcAvailabilityListener} reacting to RPC implementations different than {@link RemoteRpcImplementation}.
28 * The knowledge of such implementations is forwarded to {@link RpcRegistry}, which is responsible for advertising
29 * their presence to other nodes.
31 final class RpcListener implements DOMRpcAvailabilityListener {
32 private static final Logger LOG = LoggerFactory.getLogger(RpcListener.class);
34 private final ActorRef rpcRegistry;
36 RpcListener(final ActorRef rpcRegistry) {
37 this.rpcRegistry = Preconditions.checkNotNull(rpcRegistry);
41 public void onRpcAvailable(@Nonnull final Collection<DOMRpcIdentifier> rpcs) {
42 Preconditions.checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
43 LOG.debug("Adding registration for [{}]", rpcs);
45 final List<RouteIdentifier<?,?,?>> routeIds = new ArrayList<>(rpcs.size());
47 for (final DOMRpcIdentifier rpc : rpcs) {
48 // FIXME: Refactor routeId and message to use DOMRpcIdentifier directly.
49 final RouteIdentifier<?,?,?> routeId =
50 new RouteIdentifierImpl(null, rpc.getType().getLastComponent(), rpc.getContextReference());
51 routeIds.add(routeId);
53 rpcRegistry.tell(new AddOrUpdateRoutes(routeIds), ActorRef.noSender());
57 public void onRpcUnavailable(@Nonnull final Collection<DOMRpcIdentifier> rpcs) {
58 Preconditions.checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
60 LOG.debug("Removing registration for [{}]", rpcs);
62 final List<RouteIdentifier<?,?,?>> routeIds = new ArrayList<>(rpcs.size());
63 for (final DOMRpcIdentifier rpc : rpcs) {
64 routeIds.add(new RouteIdentifierImpl(null, rpc.getType().getLastComponent(), rpc.getContextReference()));
66 rpcRegistry.tell(new RemoveRoutes(routeIds), ActorRef.noSender());
70 public boolean acceptsImplementation(final DOMRpcImplementation impl) {
71 return !(impl instanceof RemoteRpcImplementation);