Specialize RemoteDeviceHandler to NetconfSessionPreferences
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / MasterSalFacade.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 static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.cluster.Cluster;
15 import akka.dispatch.OnComplete;
16 import akka.pattern.Patterns;
17 import akka.util.Timeout;
18 import java.util.List;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.dom.api.DOMActionService;
21 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.mdsal.dom.api.DOMNotification;
24 import org.opendaylight.mdsal.dom.api.DOMRpcService;
25 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
26 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
27 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
28 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
29 import org.opendaylight.netconf.sal.connect.netconf.sal.AbstractNetconfDataTreeService;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceDataBroker;
31 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceNotificationService;
32 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalProvider;
33 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
34 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
35 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
36 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import scala.concurrent.Future;
41
42 class MasterSalFacade implements RemoteDeviceHandler, AutoCloseable {
43     private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
44
45     private final RemoteDeviceId id;
46     private final Timeout actorResponseWaitTime;
47     private final NetconfDeviceSalProvider salProvider;
48     private final ActorRef masterActorRef;
49     private final ActorSystem actorSystem;
50
51     private MountPointContext currentMountContext = null;
52     private NetconfSessionPreferences netconfSessionPreferences = null;
53     private DOMRpcService deviceRpc = null;
54     private DOMDataBroker deviceDataBroker = null;
55     private NetconfDataTreeService netconfService = null;
56     private DOMActionService deviceAction = null;
57
58     MasterSalFacade(final RemoteDeviceId id,
59                     final ActorSystem actorSystem,
60                     final ActorRef masterActorRef,
61                     final Timeout actorResponseWaitTime,
62                     final DOMMountPointService mountService,
63                     final DataBroker dataBroker) {
64         this.id = id;
65         salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
66         this.actorSystem = actorSystem;
67         this.masterActorRef = masterActorRef;
68         this.actorResponseWaitTime = actorResponseWaitTime;
69     }
70
71     @Override
72     public void onDeviceConnected(final MountPointContext mountContext,
73                                   final NetconfSessionPreferences sessionPreferences,
74                                   final DOMRpcService domRpcService, final DOMActionService domActionService) {
75         deviceAction = domActionService;
76         LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, "
77             + "DOMActionService exposed for the device", id);
78         onDeviceConnected(mountContext, sessionPreferences, domRpcService);
79     }
80
81     @Override
82     public void onDeviceConnected(final MountPointContext mountContext,
83                                   final NetconfSessionPreferences sessionPreferences,
84                                   final DOMRpcService domRpcService) {
85         currentMountContext = mountContext;
86         netconfSessionPreferences = sessionPreferences;
87         deviceRpc = domRpcService;
88
89         LOG.info("Device {} connected - registering master mount point", id);
90
91         registerMasterMountPoint();
92
93         sendInitialDataToActor().onComplete(new OnComplete<>() {
94             @Override
95             public void onComplete(final Throwable failure, final Object success) {
96                 if (failure == null) {
97                     updateDeviceData();
98                     return;
99                 }
100
101                 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
102             }
103         }, actorSystem.dispatcher());
104
105     }
106
107     @Override
108     public void onDeviceDisconnected() {
109         LOG.info("Device {} disconnected - unregistering master mount point", id);
110         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
111         unregisterMasterMountPoint();
112     }
113
114     @Override
115     public void onDeviceFailed(final Throwable throwable) {
116         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
117         unregisterMasterMountPoint();
118     }
119
120     @Override
121     public void onNotification(final DOMNotification domNotification) {
122         salProvider.getMountInstance().publish(domNotification);
123     }
124
125     @Override
126     public void close() {
127         unregisterMasterMountPoint();
128         closeGracefully(salProvider);
129     }
130
131     private void registerMasterMountPoint() {
132         requireNonNull(id);
133         requireNonNull(currentMountContext, "Device has no remote schema context yet. Probably not fully connected.");
134         requireNonNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
135
136         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
137         deviceDataBroker = newDeviceDataBroker();
138         netconfService = newNetconfDataTreeService();
139
140         // We need to create ProxyDOMDataBroker so accessing mountpoint
141         // on leader node would be same as on follower node
142         final ProxyDOMDataBroker proxyDataBroker = new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(),
143             actorResponseWaitTime);
144         final NetconfDataTreeService proxyNetconfService = new ProxyNetconfDataTreeService(id, masterActorRef,
145             actorSystem.dispatcher(), actorResponseWaitTime);
146         salProvider.getMountInstance().onTopologyDeviceConnected(currentMountContext.getEffectiveModelContext(),
147             proxyDataBroker, proxyNetconfService, deviceRpc, notificationService, deviceAction);
148     }
149
150     protected DOMDataBroker newDeviceDataBroker() {
151         return new NetconfDeviceDataBroker(id, currentMountContext, deviceRpc, netconfSessionPreferences);
152     }
153
154     protected NetconfDataTreeService newNetconfDataTreeService() {
155         return AbstractNetconfDataTreeService.of(id, currentMountContext, deviceRpc, netconfSessionPreferences);
156     }
157
158     private Future<Object> sendInitialDataToActor() {
159         final List<SourceIdentifier> sourceIdentifiers = List.copyOf(SchemaContextUtil.getConstituentModuleIdentifiers(
160             currentMountContext.getEffectiveModelContext()));
161
162         LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
163             masterActorRef);
164
165         // send initial data to master actor
166         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, netconfService,
167             sourceIdentifiers, deviceRpc, deviceAction), actorResponseWaitTime);
168     }
169
170     private void updateDeviceData() {
171         final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
172         LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
173         salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
174                 netconfSessionPreferences.getNetconfDeviceCapabilities());
175     }
176
177     private void unregisterMasterMountPoint() {
178         salProvider.getMountInstance().onTopologyDeviceDisconnected();
179     }
180
181     @SuppressWarnings("checkstyle:IllegalCatch")
182     private void closeGracefully(final AutoCloseable resource) {
183         if (resource != null) {
184             try {
185                 resource.close();
186             } catch (final Exception e) {
187                 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);
188             }
189         }
190     }
191 }