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
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 com.google.common.base.Preconditions;
15 import java.util.Collection;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
17 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
18 import org.opendaylight.controller.remote.rpc.messages.UpdateSchemaContext;
19 import org.opendaylight.controller.sal.core.api.Broker;
20 import org.opendaylight.controller.sal.core.api.Provider;
21 import org.opendaylight.controller.sal.core.api.model.SchemaService;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
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 DOMRpcProviderService rpcProvisionRegistry;
37
38   private ListenerRegistration<SchemaContextListener> schemaListenerRegistration;
39   private final ActorSystem actorSystem;
40   private SchemaService schemaService;
41   private DOMRpcService rpcService;
42   private SchemaContext schemaContext;
43   private ActorRef rpcManager;
44   private final RemoteRpcProviderConfig config;
45
46
47   public RemoteRpcProvider(final ActorSystem actorSystem,
48                            final DOMRpcProviderService rpcProvisionRegistry,
49                            final RemoteRpcProviderConfig config) {
50     this.actorSystem = actorSystem;
51     this.rpcProvisionRegistry = rpcProvisionRegistry;
52     this.config = Preconditions.checkNotNull(config);
53   }
54
55   public void setRpcService(DOMRpcService rpcService) {
56       this.rpcService = rpcService;
57   }
58
59   public void setSchemaService(SchemaService schemaService) {
60       this.schemaService = schemaService;
61   }
62
63   @Override
64   public void close() throws Exception {
65     if (schemaListenerRegistration != null) {
66         schemaListenerRegistration.close();
67         schemaListenerRegistration = null;
68     }
69   }
70
71   @Override
72   public void onSessionInitiated(final Broker.ProviderSession session) {
73     schemaService = session.getService(SchemaService.class);
74     rpcService = session.getService(DOMRpcService.class);
75     start();
76   }
77
78   @Override
79   public Collection<ProviderFunctionality> getProviderFunctionality() {
80     return null;
81   }
82
83   public void start() {
84     LOG.info("Starting remote rpc service...");
85
86     schemaContext = schemaService.getGlobalContext();
87     rpcManager = actorSystem.actorOf(RpcManager.props(schemaContext,
88             rpcProvisionRegistry, rpcService, config), config.getRpcManagerName());
89     schemaListenerRegistration = schemaService.registerSchemaContextListener(this);
90     LOG.debug("rpc manager started");
91   }
92
93   @Override
94   public void onGlobalContextUpdated(final SchemaContext schemaContext) {
95     this.schemaContext = schemaContext;
96     rpcManager.tell(new UpdateSchemaContext(schemaContext), null);
97   }
98 }