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