Merge "Fixed for bug 1197"
[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 org.opendaylight.controller.remote.rpc.messages.AddRpc;
14 import org.opendaylight.controller.remote.rpc.messages.RemoveRpc;
15 import org.opendaylight.controller.remote.rpc.utils.ActorUtil;
16 import org.opendaylight.controller.sal.core.api.RpcRegistrationListener;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public class RpcListener implements RpcRegistrationListener{
22
23   private static final Logger LOG = LoggerFactory.getLogger(RpcListener.class);
24   private final ActorRef rpcRegistry;
25   private final String actorPath;
26
27   public RpcListener(ActorRef rpcRegistry, String actorPath) {
28     this.rpcRegistry = rpcRegistry;
29     this.actorPath = actorPath;
30   }
31
32   @Override
33   public void onRpcImplementationAdded(QName rpc) {
34     LOG.debug("Adding registration for [{}]", rpc);
35     RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, null);
36     AddRpc addRpcMsg = new AddRpc(routeId, actorPath);
37     try {
38       ActorUtil.executeLocalOperation(rpcRegistry, addRpcMsg, ActorUtil.LOCAL_ASK_DURATION, ActorUtil.LOCAL_AWAIT_DURATION);
39       LOG.debug("Route added [{}-{}]", routeId, this.actorPath);
40     } catch (Exception e) {
41       // Just logging it because Akka API throws this exception
42       LOG.error(e.toString());
43     }
44
45   }
46
47   @Override
48   public void onRpcImplementationRemoved(QName rpc) {
49     LOG.debug("Removing registration for [{}]", rpc);
50     RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, null);
51     RemoveRpc removeRpcMsg = new RemoveRpc(routeId);
52     try {
53       ActorUtil.executeLocalOperation(rpcRegistry, removeRpcMsg, ActorUtil.LOCAL_ASK_DURATION, ActorUtil.LOCAL_AWAIT_DURATION);
54     } catch (Exception e) {
55       // Just logging it because Akka API throws this exception
56       LOG.error(e.toString());
57     }
58   }
59 }