28ff1523cbb676c5bf47d6180d96c9e7f1304230
[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
9 package org.opendaylight.controller.remote.rpc;
10
11
12 import akka.actor.ActorRef;
13 import com.google.common.base.Preconditions;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.dom.api.DOMRpcAvailabilityListener;
19 import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
20 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
21 import org.opendaylight.controller.sal.connector.api.RpcRouter;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class RpcListener implements DOMRpcAvailabilityListener{
26
27   private static final Logger LOG = LoggerFactory.getLogger(RpcListener.class);
28   private final ActorRef rpcRegistry;
29
30   public RpcListener(final ActorRef rpcRegistry) {
31     this.rpcRegistry = rpcRegistry;
32   }
33
34     @Override
35     public void onRpcAvailable(@Nonnull final Collection<DOMRpcIdentifier> rpcs) {
36         Preconditions.checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
37         if (LOG.isDebugEnabled()) {
38             LOG.debug("Adding registration for [{}]", rpcs);
39         }
40         final List<RpcRouter.RouteIdentifier<?,?,?>> routeIds = new ArrayList<>();
41
42         for (final DOMRpcIdentifier rpc : rpcs) {
43             final RpcRouter.RouteIdentifier<?,?,?> routeId = new RouteIdentifierImpl(null, rpc.getType().getLastComponent(), null);
44             routeIds.add(routeId);
45         }
46         final RpcRegistry.Messages.AddOrUpdateRoutes addRpcMsg = new RpcRegistry.Messages.AddOrUpdateRoutes(routeIds);
47         rpcRegistry.tell(addRpcMsg, ActorRef.noSender());
48     }
49
50     @Override
51     public void onRpcUnavailable(@Nonnull final Collection<DOMRpcIdentifier> rpcs) {
52         Preconditions.checkArgument(rpcs != null, "Input Collection of DOMRpcIdentifier can not be null.");
53         if(LOG.isDebugEnabled()) {
54             LOG.debug("Removing registration for [{}]", rpcs);
55         }
56         final List<RpcRouter.RouteIdentifier<?,?,?>> routeIds = new ArrayList<>();
57         for (final DOMRpcIdentifier rpc : rpcs) {
58             final RpcRouter.RouteIdentifier<?,?,?> routeId = new RouteIdentifierImpl(null, rpc.getType().getLastComponent(), null);
59             routeIds.add(routeId);
60         }
61         final RpcRegistry.Messages.RemoveRoutes removeRpcMsg = new RpcRegistry.Messages.RemoveRoutes(routeIds);
62         rpcRegistry.tell(removeRpcMsg, ActorRef.noSender());
63     }
64 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.