Merge "Fix precondition formatting"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RpcManager.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 akka.actor.OneForOneStrategy;
14 import akka.actor.Props;
15 import akka.actor.SupervisorStrategy;
16 import akka.japi.Creator;
17 import akka.japi.Function;
18 import org.opendaylight.controller.remote.rpc.messages.UpdateSchemaContext;
19 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
20 import org.opendaylight.controller.sal.core.api.Broker;
21 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import scala.concurrent.duration.Duration;
27 import java.util.Set;
28
29 /**
30  * This class acts as a supervisor, creates all the actors, resumes them, if an exception is thrown.
31  *
32  * It also starts the rpc listeners
33  */
34
35 public class RpcManager extends AbstractUntypedActor {
36
37   private static final Logger LOG = LoggerFactory.getLogger(RpcManager.class);
38
39   private SchemaContext schemaContext;
40   private ActorRef rpcBroker;
41   private ActorRef rpcRegistry;
42   private final Broker.ProviderSession brokerSession;
43   private RpcListener rpcListener;
44   private RoutedRpcListener routeChangeListener;
45   private RemoteRpcImplementation rpcImplementation;
46   private final RpcProvisionRegistry rpcProvisionRegistry;
47
48   private RpcManager(SchemaContext schemaContext,
49                      Broker.ProviderSession brokerSession, RpcProvisionRegistry rpcProvisionRegistry) {
50     this.schemaContext = schemaContext;
51     this.brokerSession = brokerSession;
52     this.rpcProvisionRegistry = rpcProvisionRegistry;
53
54     createRpcActors();
55     startListeners();
56   }
57
58
59   public static Props props(final SchemaContext schemaContext,
60                             final Broker.ProviderSession brokerSession, final RpcProvisionRegistry rpcProvisionRegistry) {
61     return Props.create(new Creator<RpcManager>() {
62       @Override
63       public RpcManager create() throws Exception {
64         return new RpcManager(schemaContext, brokerSession, rpcProvisionRegistry);
65       }
66     });
67   }
68
69   private void createRpcActors() {
70     LOG.debug("Create rpc registry and broker actors");
71
72
73     rpcRegistry = getContext().actorOf(Props.create(RpcRegistry.class), ActorConstants.RPC_REGISTRY);
74
75     rpcBroker = getContext().actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), ActorConstants.RPC_BROKER);
76     RpcRegistry.Messages.SetLocalRouter localRouter = new RpcRegistry.Messages.SetLocalRouter(rpcBroker);
77     rpcRegistry.tell(localRouter, self());
78   }
79
80   private void startListeners() {
81     LOG.debug("Registers rpc listeners");
82
83     rpcListener = new RpcListener(rpcRegistry);
84     routeChangeListener = new RoutedRpcListener(rpcRegistry);
85     rpcImplementation = new RemoteRpcImplementation(rpcBroker, schemaContext);
86
87     brokerSession.addRpcRegistrationListener(rpcListener);
88     rpcProvisionRegistry.registerRouteChangeListener(routeChangeListener);
89     rpcProvisionRegistry.setRoutedRpcDefaultDelegate(rpcImplementation);
90     announceSupportedRpcs();
91   }
92
93   /**
94    * Add all the locally registered RPCs in the clustered routing table
95    */
96   private void announceSupportedRpcs(){
97     LOG.debug("Adding all supported rpcs to routing table");
98     Set<QName> currentlySupported = brokerSession.getSupportedRpcs();
99     for (QName rpc : currentlySupported) {
100       rpcListener.onRpcImplementationAdded(rpc);
101     }
102   }
103
104
105   @Override
106   protected void handleReceive(Object message) throws Exception {
107     if(message instanceof UpdateSchemaContext) {
108       updateSchemaContext((UpdateSchemaContext) message);
109     }
110
111   }
112
113   private void updateSchemaContext(UpdateSchemaContext message) {
114     this.schemaContext = message.getSchemaContext();
115   }
116
117   @Override
118   public SupervisorStrategy supervisorStrategy() {
119     return new OneForOneStrategy(10, Duration.create("1 minute"),
120         new Function<Throwable, SupervisorStrategy.Directive>() {
121           @Override
122           public SupervisorStrategy.Directive apply(Throwable t) {
123             return SupervisorStrategy.resume();
124           }
125         }
126     );
127   }
128 }