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