Merge "Increase IDLE timeout for ssh netconf server"
[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 java.util.Collection;
15 import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
17 import org.opendaylight.controller.remote.rpc.messages.UpdateSchemaContext;
18 import org.opendaylight.controller.sal.core.api.Broker;
19 import org.opendaylight.controller.sal.core.api.Provider;
20 import org.opendaylight.controller.sal.core.api.model.SchemaService;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * This is the base class which initialize all the actors, listeners and
29  * default RPc implementation so remote invocation of rpcs.
30  */
31 public class RemoteRpcProvider implements AutoCloseable, Provider, SchemaContextListener {
32
33   private static final Logger LOG = LoggerFactory.getLogger(RemoteRpcProvider.class);
34
35   private final DOMRpcProviderService rpcProvisionRegistry;
36
37   private ListenerRegistration<SchemaContextListener> schemaListenerRegistration;
38   private ActorSystem actorSystem;
39   private Broker.ProviderSession brokerSession;
40   private SchemaContext schemaContext;
41   private ActorRef rpcManager;
42   private final RemoteRpcProviderConfig config;
43
44
45   public RemoteRpcProvider(final ActorSystem actorSystem, final DOMRpcProviderService rpcProvisionRegistry) {
46     this.actorSystem = actorSystem;
47     this.rpcProvisionRegistry = rpcProvisionRegistry;
48     config = new RemoteRpcProviderConfig(actorSystem.settings().config());
49   }
50
51   @Override
52   public void close() throws Exception {
53     if (actorSystem != null) {
54         actorSystem.shutdown();
55         actorSystem = null;
56     }
57     if (schemaListenerRegistration != null) {
58         schemaListenerRegistration.close();
59         schemaListenerRegistration = null;
60     }
61   }
62
63   @Override
64   public void onSessionInitiated(final Broker.ProviderSession session) {
65     brokerSession = session;
66     start();
67   }
68
69   @Override
70   public Collection<ProviderFunctionality> getProviderFunctionality() {
71     return null;
72   }
73
74   private void start() {
75     LOG.info("Starting remote rpc service...");
76
77     final SchemaService schemaService = brokerSession.getService(SchemaService.class);
78     final DOMRpcService rpcService = brokerSession.getService(DOMRpcService.class);
79     schemaContext = schemaService.getGlobalContext();
80     rpcManager = actorSystem.actorOf(RpcManager.props(schemaContext,
81             rpcProvisionRegistry, rpcService), config.getRpcManagerName());
82     schemaListenerRegistration = schemaService.registerSchemaContextListener(this);
83     LOG.debug("rpc manager started");
84   }
85
86   @Override
87   public void onGlobalContextUpdated(final SchemaContext schemaContext) {
88     this.schemaContext = schemaContext;
89     rpcManager.tell(new UpdateSchemaContext(schemaContext), null);
90   }
91 }