Implement graceful ShardManager shutdown
[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 Broker.ProviderSession brokerSession;
41   private SchemaContext schemaContext;
42   private ActorRef rpcManager;
43   private final RemoteRpcProviderConfig config;
44
45
46   public RemoteRpcProvider(final ActorSystem actorSystem,
47                            final DOMRpcProviderService rpcProvisionRegistry,
48                            final RemoteRpcProviderConfig config) {
49     this.actorSystem = actorSystem;
50     this.rpcProvisionRegistry = rpcProvisionRegistry;
51     this.config = Preconditions.checkNotNull(config);
52   }
53
54   @Override
55   public void close() throws Exception {
56     if (schemaListenerRegistration != null) {
57         schemaListenerRegistration.close();
58         schemaListenerRegistration = null;
59     }
60   }
61
62   @Override
63   public void onSessionInitiated(final Broker.ProviderSession session) {
64     brokerSession = session;
65     start();
66   }
67
68   @Override
69   public Collection<ProviderFunctionality> getProviderFunctionality() {
70     return null;
71   }
72
73   private void start() {
74     LOG.info("Starting remote rpc service...");
75
76     final SchemaService schemaService = brokerSession.getService(SchemaService.class);
77     final DOMRpcService rpcService = brokerSession.getService(DOMRpcService.class);
78     schemaContext = schemaService.getGlobalContext();
79     rpcManager = actorSystem.actorOf(RpcManager.props(schemaContext,
80             rpcProvisionRegistry, rpcService, config), config.getRpcManagerName());
81     schemaListenerRegistration = schemaService.registerSchemaContextListener(this);
82     LOG.debug("rpc manager started");
83   }
84
85   @Override
86   public void onGlobalContextUpdated(final SchemaContext schemaContext) {
87     this.schemaContext = schemaContext;
88     rpcManager.tell(new UpdateSchemaContext(schemaContext), null);
89   }
90 }