8be62c482ebaeec5a52d8022201959d9dcce0d2a
[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 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 (actorSystem != null) {
57         actorSystem.shutdown();
58         actorSystem = null;
59     }
60     if (schemaListenerRegistration != null) {
61         schemaListenerRegistration.close();
62         schemaListenerRegistration = null;
63     }
64   }
65
66   @Override
67   public void onSessionInitiated(final Broker.ProviderSession session) {
68     brokerSession = session;
69     start();
70   }
71
72   @Override
73   public Collection<ProviderFunctionality> getProviderFunctionality() {
74     return null;
75   }
76
77   private void start() {
78     LOG.info("Starting remote rpc service...");
79
80     final SchemaService schemaService = brokerSession.getService(SchemaService.class);
81     final DOMRpcService rpcService = brokerSession.getService(DOMRpcService.class);
82     schemaContext = schemaService.getGlobalContext();
83     rpcManager = actorSystem.actorOf(RpcManager.props(schemaContext,
84             rpcProvisionRegistry, rpcService, config), config.getRpcManagerName());
85     schemaListenerRegistration = schemaService.registerSchemaContextListener(this);
86     LOG.debug("rpc manager started");
87   }
88
89   @Override
90   public void onGlobalContextUpdated(final SchemaContext schemaContext) {
91     this.schemaContext = schemaContext;
92     rpcManager.tell(new UpdateSchemaContext(schemaContext), null);
93   }
94 }