Add blueprint wiring to sal-remoterpc-connector
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / main / java / org / opendaylight / controller / remote / rpc / RemoteRpcProvider.java
index 1bb7ea451441683750dcd8e4a18051e61f829e01..80aebd1918a33e5fa588ec13348e11a4515f32e3 100644 (file)
@@ -11,52 +11,67 @@ package org.opendaylight.controller.remote.rpc;
 
 import akka.actor.ActorRef;
 import akka.actor.ActorSystem;
-import org.opendaylight.controller.remote.rpc.registry.ClusterWrapper;
-import org.opendaylight.controller.remote.rpc.registry.ClusterWrapperImpl;
-import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
+import com.google.common.base.Preconditions;
+import java.util.Collection;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
+import org.opendaylight.controller.remote.rpc.messages.UpdateSchemaContext;
 import org.opendaylight.controller.sal.core.api.Broker;
 import org.opendaylight.controller.sal.core.api.Provider;
-import org.opendaylight.controller.sal.core.api.RpcProvisionRegistry;
 import org.opendaylight.controller.sal.core.api.model.SchemaService;
-import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Collection;
-import java.util.Set;
-
 /**
  * This is the base class which initialize all the actors, listeners and
  * default RPc implementation so remote invocation of rpcs.
  */
-public class RemoteRpcProvider implements AutoCloseable, Provider{
+public class RemoteRpcProvider implements AutoCloseable, Provider, SchemaContextListener {
 
   private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcProvider.class);
 
+  private final DOMRpcProviderService rpcProvisionRegistry;
+
+  private ListenerRegistration<SchemaContextListener> schemaListenerRegistration;
   private final ActorSystem actorSystem;
-  private ActorRef rpcBroker;
-  private ActorRef rpcRegistry;
-  private final RpcProvisionRegistry rpcProvisionRegistry;
-  private Broker.ProviderSession brokerSession;
-  private RpcListener rpcListener;
-  private RoutedRpcListener routeChangeListener;
-  private RemoteRpcImplementation rpcImplementation;
-  public RemoteRpcProvider(ActorSystem actorSystem, RpcProvisionRegistry rpcProvisionRegistry) {
+  private SchemaService schemaService;
+  private DOMRpcService rpcService;
+  private SchemaContext schemaContext;
+  private ActorRef rpcManager;
+  private final RemoteRpcProviderConfig config;
+
+
+  public RemoteRpcProvider(final ActorSystem actorSystem,
+                           final DOMRpcProviderService rpcProvisionRegistry,
+                           final RemoteRpcProviderConfig config) {
     this.actorSystem = actorSystem;
     this.rpcProvisionRegistry = rpcProvisionRegistry;
+    this.config = Preconditions.checkNotNull(config);
+  }
+
+  public void setRpcService(DOMRpcService rpcService) {
+      this.rpcService = rpcService;
+  }
+
+  public void setSchemaService(SchemaService schemaService) {
+      this.schemaService = schemaService;
   }
 
   @Override
   public void close() throws Exception {
-    this.actorSystem.shutdown();
-    unregisterSupportedRpcs();
-    unregisterSupportedRoutedRpcs();
+    if (schemaListenerRegistration != null) {
+        schemaListenerRegistration.close();
+        schemaListenerRegistration = null;
+    }
   }
 
   @Override
-  public void onSessionInitiated(Broker.ProviderSession session) {
-    this.brokerSession = session;
+  public void onSessionInitiated(final Broker.ProviderSession session) {
+    schemaService = session.getService(SchemaService.class);
+    rpcService = session.getService(DOMRpcService.class);
     start();
   }
 
@@ -65,65 +80,19 @@ public class RemoteRpcProvider implements AutoCloseable, Provider{
     return null;
   }
 
-  private void start() {
-    LOG.debug("Starting all rpc listeners.");
-    // Create actor to handle and sync routing table in cluster
-    ClusterWrapper clusterWrapper = new ClusterWrapperImpl(actorSystem);
-    rpcRegistry = actorSystem.actorOf(RpcRegistry.props(clusterWrapper), "rpc-registry");
-
-    // Create actor to invoke and execute rpc
-    SchemaService schemaService = brokerSession.getService(SchemaService.class);
-    SchemaContext schemaContext = schemaService.getGlobalContext();
-    rpcBroker = actorSystem.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), "rpc-broker");
-    String rpcBrokerPath = clusterWrapper.getAddress().toString() + "/user/rpc-broker";
-    rpcListener = new RpcListener(rpcRegistry, rpcBrokerPath);
-    routeChangeListener = new RoutedRpcListener(rpcRegistry, rpcBrokerPath);
-    rpcImplementation = new RemoteRpcImplementation(rpcBroker, schemaContext);
-    brokerSession.addRpcRegistrationListener(rpcListener);
-    rpcProvisionRegistry.registerRouteChangeListener(routeChangeListener);
-    rpcProvisionRegistry.setRoutedRpcDefaultDelegate(rpcImplementation);
-    announceSupportedRpcs();
-    announceSupportedRoutedRpcs();
-
-  }
-
-  /**
-   * Add all the locally registered RPCs in the clustered routing table
-   */
-  private void announceSupportedRpcs(){
-    LOG.debug("Adding all supported rpcs to routing table");
-    Set<QName> currentlySupported = brokerSession.getSupportedRpcs();
-    for (QName rpc : currentlySupported) {
-      rpcListener.onRpcImplementationAdded(rpc);
-    }
-  }
-
-  /**
-   * Add all the locally registered Routed RPCs in the clustered routing table
-   */
-  private void announceSupportedRoutedRpcs(){
-
-    //TODO: announce all routed RPCs as well
+  public void start() {
+    LOG.info("Starting remote rpc service...");
 
+    schemaContext = schemaService.getGlobalContext();
+    rpcManager = actorSystem.actorOf(RpcManager.props(schemaContext,
+            rpcProvisionRegistry, rpcService, config), config.getRpcManagerName());
+    schemaListenerRegistration = schemaService.registerSchemaContextListener(this);
+    LOG.debug("rpc manager started");
   }
 
-  /**
-   * Un-Register all the supported RPCs from clustered routing table
-   */
-  private void unregisterSupportedRpcs(){
-    LOG.debug("removing all supported rpcs to routing table");
-    Set<QName> currentlySupported = brokerSession.getSupportedRpcs();
-    for (QName rpc : currentlySupported) {
-      rpcListener.onRpcImplementationRemoved(rpc);
-    }
-  }
-
-  /**
-   * Un-Register all the locally supported Routed RPCs from clustered routing table
-   */
-  private void unregisterSupportedRoutedRpcs(){
-
-    //TODO: remove all routed RPCs as well
-
+  @Override
+  public void onGlobalContextUpdated(final SchemaContext schemaContext) {
+    this.schemaContext = schemaContext;
+    rpcManager.tell(new UpdateSchemaContext(schemaContext), null);
   }
 }