Merge "BUG-628 Allow configuration to override module based capabilities from remote...
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteRpcProvider.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.ActorSystem;
14 import org.opendaylight.controller.remote.rpc.messages.UpdateSchemaContext;
15 import org.opendaylight.controller.remote.rpc.registry.ClusterWrapper;
16 import org.opendaylight.controller.remote.rpc.registry.ClusterWrapperImpl;
17 import org.opendaylight.controller.sal.core.api.Broker;
18 import org.opendaylight.controller.sal.core.api.Provider;
19 import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
20 import org.opendaylight.controller.sal.core.api.model.SchemaService;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.util.Collection;
27
28 /**
29  * This is the base class which initialize all the actors, listeners and
30  * default RPc implementation so remote invocation of rpcs.
31  */
32 public class RemoteRpcProvider implements AutoCloseable, Provider, SchemaContextListener {
33
34   private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcProvider.class);
35
36   private final ActorSystem actorSystem;
37   private final RpcProvisionRegistry rpcProvisionRegistry;
38   private Broker.ProviderSession brokerSession;
39   private SchemaContext schemaContext;
40   private ActorRef rpcManager;
41
42
43   public RemoteRpcProvider(ActorSystem actorSystem, RpcProvisionRegistry rpcProvisionRegistry) {
44     this.actorSystem = actorSystem;
45     this.rpcProvisionRegistry = rpcProvisionRegistry;
46   }
47
48   @Override
49   public void close() throws Exception {
50     this.actorSystem.shutdown();
51   }
52
53   @Override
54   public void onSessionInitiated(Broker.ProviderSession session) {
55     this.brokerSession = session;
56     start();
57   }
58
59   @Override
60   public Collection<ProviderFunctionality> getProviderFunctionality() {
61     return null;
62   }
63
64   private void start() {
65     LOG.info("Starting all rpc listeners and actors.");
66     // Create actor to handle and sync routing table in cluster
67     ClusterWrapper clusterWrapper = new ClusterWrapperImpl(actorSystem);
68     SchemaService schemaService = brokerSession.getService(SchemaService.class);
69     schemaContext = schemaService.getGlobalContext();
70
71     rpcManager = actorSystem.actorOf(RpcManager.props(clusterWrapper, schemaContext, brokerSession, rpcProvisionRegistry), "rpc");
72
73     LOG.debug("Rpc actors are created.");
74   }
75
76
77   @Override
78   public void onGlobalContextUpdated(SchemaContext schemaContext) {
79     this.schemaContext = schemaContext;
80     rpcManager.tell(new UpdateSchemaContext(schemaContext), null);
81
82   }
83 }