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.DOMDataBroker;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
22 import org.opendaylight.mdsal.dom.api.DOMNotification;
23 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
24 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
25 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
26 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
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.NetconfDeviceSalProvider;
32 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
33 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
34 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
35 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
36 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import scala.concurrent.Future;
41 class MasterSalFacade implements RemoteDeviceHandler, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
44 private final RemoteDeviceId id;
45 private final Timeout actorResponseWaitTime;
46 private final NetconfDeviceSalProvider salProvider;
47 private final ActorRef masterActorRef;
48 private final ActorSystem actorSystem;
49 private final boolean lockDatastore;
51 private NetconfDeviceSchema currentSchema = null;
52 private NetconfSessionPreferences netconfSessionPreferences = null;
53 private RemoteDeviceServices deviceServices = null;
54 private DOMDataBroker deviceDataBroker = null;
55 private NetconfDataTreeService netconfService = null;
57 MasterSalFacade(final RemoteDeviceId id,
58 final ActorSystem actorSystem,
59 final ActorRef masterActorRef,
60 final Timeout actorResponseWaitTime,
61 final DOMMountPointService mountService,
62 final DataBroker dataBroker,
63 final boolean lockDatastore) {
65 salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
66 this.actorSystem = actorSystem;
67 this.masterActorRef = masterActorRef;
68 this.actorResponseWaitTime = actorResponseWaitTime;
69 this.lockDatastore = lockDatastore;
73 public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
74 final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
75 currentSchema = requireNonNull(deviceSchema);
76 netconfSessionPreferences = requireNonNull(sessionPreferences);
77 deviceServices = requireNonNull(services);
78 if (services.actions() != null) {
79 LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, DOMActionService exposed for "
83 LOG.info("Device {} connected - registering master mount point", id);
85 registerMasterMountPoint();
87 sendInitialDataToActor().onComplete(new OnComplete<>() {
89 public void onComplete(final Throwable failure, final Object success) {
90 if (failure == null) {
95 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
97 }, actorSystem.dispatcher());
101 public void onDeviceDisconnected() {
102 LOG.info("Device {} disconnected - unregistering master mount point", id);
103 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, NetconfDeviceCapabilities.empty());
104 unregisterMasterMountPoint();
108 public void onDeviceFailed(final Throwable throwable) {
109 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
110 unregisterMasterMountPoint();
114 public void onNotification(final DOMNotification domNotification) {
115 salProvider.getMountInstance().publish(domNotification);
119 public void close() {
120 unregisterMasterMountPoint();
121 closeGracefully(salProvider);
124 private void registerMasterMountPoint() {
127 final var mountContext = requireNonNull(currentSchema,
128 "Device has no remote schema context yet. Probably not fully connected.")
130 final var preferences = requireNonNull(netconfSessionPreferences,
131 "Device has no capabilities yet. Probably not fully connected.");
133 deviceDataBroker = newDeviceDataBroker(mountContext, preferences);
134 netconfService = newNetconfDataTreeService(mountContext, preferences);
136 // We need to create ProxyDOMDataBroker so accessing mountpoint
137 // on leader node would be same as on follower node
138 final ProxyDOMDataBroker proxyDataBroker = new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(),
139 actorResponseWaitTime);
140 final NetconfDataTreeService proxyNetconfService = new ProxyNetconfDataTreeService(id, masterActorRef,
141 actorSystem.dispatcher(), actorResponseWaitTime);
142 salProvider.getMountInstance().onTopologyDeviceConnected(mountContext.getEffectiveModelContext(),
143 deviceServices, proxyDataBroker, proxyNetconfService);
146 protected DOMDataBroker newDeviceDataBroker(final MountPointContext mountContext,
147 final NetconfSessionPreferences preferences) {
148 return new NetconfDeviceDataBroker(id, mountContext, deviceServices.rpcs(), preferences, lockDatastore);
151 protected NetconfDataTreeService newNetconfDataTreeService(final MountPointContext mountContext,
152 final NetconfSessionPreferences preferences) {
153 return AbstractNetconfDataTreeService.of(id, mountContext, deviceServices.rpcs(), preferences, lockDatastore);
156 private Future<Object> sendInitialDataToActor() {
157 final List<SourceIdentifier> sourceIdentifiers = List.copyOf(SchemaContextUtil.getConstituentModuleIdentifiers(
158 currentSchema.mountContext().getEffectiveModelContext()));
160 LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
163 // send initial data to master actor
164 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, netconfService,
165 sourceIdentifiers, deviceServices), actorResponseWaitTime);
168 private void updateDeviceData() {
169 final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
170 LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
171 salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
172 currentSchema.capabilities());
175 private void unregisterMasterMountPoint() {
176 salProvider.getMountInstance().onTopologyDeviceDisconnected();
179 @SuppressWarnings("checkstyle:IllegalCatch")
180 private void closeGracefully(final AutoCloseable resource) {
181 if (resource != null) {
184 } catch (final Exception e) {
185 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);