BUG-3128: rework sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RpcListener.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 package org.opendaylight.controller.remote.rpc;
9
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;
25
26 /**
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.
30  */
31 final class RpcListener implements DOMRpcAvailabilityListener {
32     private static final Logger LOG = LoggerFactory.getLogger(RpcListener.class);
33
34     private final ActorRef rpcRegistry;
35
36     RpcListener(final ActorRef rpcRegistry) {
37         this.rpcRegistry = Preconditions.checkNotNull(rpcRegistry);
38     }
39
40     @Override
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);
44
45         final List<RouteIdentifier<?,?,?>> routeIds = new ArrayList<>(rpcs.size());
46
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);
52         }
53         rpcRegistry.tell(new AddOrUpdateRoutes(routeIds), ActorRef.noSender());
54     }
55
56     @Override
57     public void onRpcUnavailable(@Nonnull final Collection<DOMRpcIdentifier> rpcs) {
58         Preconditions.checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
59
60         LOG.debug("Removing registration for [{}]", rpcs);
61
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()));
65         }
66         rpcRegistry.tell(new RemoveRoutes(routeIds), ActorRef.noSender());
67     }
68
69     @Override
70     public boolean acceptsImplementation(final DOMRpcImplementation impl) {
71         return !(impl instanceof RemoteRpcImplementation);
72     }
73 }