bd0ee02147172518b9e6d778dcbfc0afcd11d2aa
[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.Collection;
13 import javax.annotation.Nonnull;
14 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementation;
17 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
18 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.AddOrUpdateRoutes;
19 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.RemoveRoutes;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * A {@link DOMRpcAvailabilityListener} reacting to RPC implementations different than {@link RemoteRpcImplementation}.
25  * The knowledge of such implementations is forwarded to {@link RpcRegistry}, which is responsible for advertising
26  * their presence to other nodes.
27  */
28 final class RpcListener implements DOMRpcAvailabilityListener {
29     private static final Logger LOG = LoggerFactory.getLogger(RpcListener.class);
30
31     private final ActorRef rpcRegistry;
32
33     RpcListener(final ActorRef rpcRegistry) {
34         this.rpcRegistry = Preconditions.checkNotNull(rpcRegistry);
35     }
36
37     @Override
38     public void onRpcAvailable(@Nonnull final Collection<DOMRpcIdentifier> rpcs) {
39         Preconditions.checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
40         LOG.debug("Adding registration for [{}]", rpcs);
41
42         rpcRegistry.tell(new AddOrUpdateRoutes(rpcs), ActorRef.noSender());
43     }
44
45     @Override
46     public void onRpcUnavailable(@Nonnull final Collection<DOMRpcIdentifier> rpcs) {
47         Preconditions.checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
48
49         LOG.debug("Removing registration for [{}]", rpcs);
50         rpcRegistry.tell(new RemoveRoutes(rpcs), ActorRef.noSender());
51     }
52
53     @Override
54     public boolean acceptsImplementation(final DOMRpcImplementation impl) {
55         return !(impl instanceof RemoteRpcImplementation);
56     }
57 }