2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.topology.singleton.impl;
10 import static java.util.Objects.requireNonNull;
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;
43 class MasterSalFacade implements RemoteDeviceHandler, AutoCloseable {
44 private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
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;
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;
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) {
66 salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
67 this.actorSystem = actorSystem;
68 this.masterActorRef = masterActorRef;
69 this.actorResponseWaitTime = actorResponseWaitTime;
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);
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;
90 LOG.info("Device {} connected - registering master mount point", id);
92 registerMasterMountPoint();
94 sendInitialDataToActor().onComplete(new OnComplete<>() {
96 public void onComplete(final Throwable failure, final Object success) {
97 if (failure == null) {
102 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
104 }, actorSystem.dispatcher());
108 public void onDeviceDisconnected() {
109 LOG.info("Device {} disconnected - unregistering master mount point", id);
110 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, NetconfDeviceCapabilities.empty());
111 unregisterMasterMountPoint();
115 public void onDeviceFailed(final Throwable throwable) {
116 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
117 unregisterMasterMountPoint();
121 public void onNotification(final DOMNotification domNotification) {
122 salProvider.getMountInstance().publish(domNotification);
126 public void close() {
127 unregisterMasterMountPoint();
128 closeGracefully(salProvider);
131 private void registerMasterMountPoint() {
134 final var mountContext = requireNonNull(currentSchema,
135 "Device has no remote schema context yet. Probably not fully connected.")
137 final var preferences = requireNonNull(netconfSessionPreferences,
138 "Device has no capabilities yet. Probably not fully connected.");
140 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
141 deviceDataBroker = newDeviceDataBroker(mountContext, preferences);
142 netconfService = newNetconfDataTreeService(mountContext, preferences);
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);
154 protected DOMDataBroker newDeviceDataBroker(final MountPointContext mountContext,
155 final NetconfSessionPreferences preferences) {
156 return new NetconfDeviceDataBroker(id, mountContext, deviceRpc, preferences);
159 protected NetconfDataTreeService newNetconfDataTreeService(final MountPointContext mountContext,
160 final NetconfSessionPreferences preferences) {
161 return AbstractNetconfDataTreeService.of(id, mountContext, deviceRpc, preferences);
164 private Future<Object> sendInitialDataToActor() {
165 final List<SourceIdentifier> sourceIdentifiers = List.copyOf(SchemaContextUtil.getConstituentModuleIdentifiers(
166 currentSchema.mountContext().getEffectiveModelContext()));
168 LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
171 // send initial data to master actor
172 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, netconfService,
173 sourceIdentifiers, deviceRpc, deviceAction), actorResponseWaitTime);
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());
183 private void unregisterMasterMountPoint() {
184 salProvider.getMountInstance().onTopologyDeviceDisconnected();
187 @SuppressWarnings("checkstyle:IllegalCatch")
188 private void closeGracefully(final AutoCloseable resource) {
189 if (resource != null) {
192 } catch (final Exception e) {
193 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);