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