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