Convert ProxyDOMDataBroker tx creation to async
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / SlaveSalFacade.java
1 /*
2  * Copyright (c) 2016 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.netconf.topology.singleton.impl;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.util.Timeout;
14 import java.util.concurrent.atomic.AtomicBoolean;
15 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
16 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
17 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceNotificationService;
18 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalProvider;
19 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
20 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public class SlaveSalFacade {
25
26     private static final Logger LOG = LoggerFactory.getLogger(SlaveSalFacade.class);
27
28     private final RemoteDeviceId id;
29     private final NetconfDeviceSalProvider salProvider;
30     private final ActorSystem actorSystem;
31     private final Timeout actorResponseWaitTime;
32     private final AtomicBoolean registered = new AtomicBoolean(false);
33
34     public SlaveSalFacade(final RemoteDeviceId id,
35                           final ActorSystem actorSystem,
36                           final Timeout actorResponseWaitTime,
37                           final DOMMountPointService mountPointService) {
38         this.id = id;
39         this.salProvider = new NetconfDeviceSalProvider(id, mountPointService);
40         this.actorSystem = actorSystem;
41         this.actorResponseWaitTime = actorResponseWaitTime;
42     }
43
44     public void registerSlaveMountPoint(final SchemaContext remoteSchemaContext, final DOMRpcService deviceRpc,
45                                         final ActorRef masterActorRef) {
46         if (!registered.compareAndSet(false, true)) {
47             return;
48         }
49
50         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
51
52         final ProxyDOMDataBroker netconfDeviceDataBroker =
53                 new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(), actorResponseWaitTime);
54
55         salProvider.getMountInstance().onTopologyDeviceConnected(remoteSchemaContext, netconfDeviceDataBroker,
56                 deviceRpc, notificationService);
57
58         LOG.info("{}: Slave mount point registered.", id);
59     }
60
61     public void close() {
62         if (!registered.compareAndSet(true, false)) {
63             return;
64         }
65
66         salProvider.getMountInstance().onTopologyDeviceDisconnected();
67
68         LOG.info("{}: Slave mount point unregistered.", id);
69     }
70 }