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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.List;
20 import java.util.stream.Collectors;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.dom.api.DOMActionService;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMNotification;
26 import org.opendaylight.mdsal.dom.api.DOMRpcService;
27 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
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.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.RevisionSourceIdentifier;
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 AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
45 private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
47 private final RemoteDeviceId id;
48 private final Timeout actorResponseWaitTime;
49 private final NetconfDeviceSalProvider salProvider;
50 private final ActorRef masterActorRef;
51 private final ActorSystem actorSystem;
53 private MountPointContext currentMountContext = null;
54 private NetconfSessionPreferences netconfSessionPreferences = null;
55 private DOMRpcService deviceRpc = null;
56 private DOMDataBroker deviceDataBroker = 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 this.salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
67 this.actorSystem = actorSystem;
68 this.masterActorRef = masterActorRef;
69 this.actorResponseWaitTime = actorResponseWaitTime;
73 public void onDeviceConnected(final MountPointContext mountContext,
74 final NetconfSessionPreferences sessionPreferences,
75 final DOMRpcService domRpcService, final DOMActionService domActionService) {
76 this.deviceAction = domActionService;
77 LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, "
78 + "DOMActionService exposed for the device", id);
79 onDeviceConnected(mountContext, sessionPreferences, domRpcService);
83 public void onDeviceConnected(final MountPointContext mountContext,
84 final NetconfSessionPreferences sessionPreferences,
85 final DOMRpcService domRpcService) {
86 this.currentMountContext = mountContext;
87 this.netconfSessionPreferences = sessionPreferences;
88 this.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());
109 public void onDeviceDisconnected() {
110 LOG.info("Device {} disconnected - unregistering master mount point", id);
111 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
112 unregisterMasterMountPoint();
116 public void onDeviceFailed(final Throwable throwable) {
117 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
118 unregisterMasterMountPoint();
122 public void onNotification(final DOMNotification domNotification) {
123 salProvider.getMountInstance().publish(domNotification);
127 public void close() {
128 unregisterMasterMountPoint();
129 closeGracefully(salProvider);
132 private void registerMasterMountPoint() {
134 requireNonNull(currentMountContext, "Device has no remote schema context yet. Probably not fully connected.");
135 requireNonNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
137 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
138 deviceDataBroker = newDeviceDataBroker();
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 salProvider.getMountInstance().onTopologyDeviceConnected(currentMountContext.getSchemaContext(),
145 proxyDataBroker, deviceRpc, notificationService, deviceAction);
148 protected DOMDataBroker newDeviceDataBroker() {
149 return new NetconfDeviceDataBroker(id, currentMountContext, deviceRpc, netconfSessionPreferences);
152 private Future<Object> sendInitialDataToActor() {
153 final List<SourceIdentifier> sourceIdentifiers =
154 SchemaContextUtil.getConstituentModuleIdentifiers(currentMountContext.getSchemaContext()).stream()
155 .map(mi -> RevisionSourceIdentifier.create(mi.getName(), mi.getRevision()))
156 .collect(Collectors.toList());
158 LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
161 // send initial data to master actor
162 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
163 deviceRpc, deviceAction), actorResponseWaitTime);
166 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
167 justification = "https://github.com/spotbugs/spotbugs/issues/811")
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 netconfSessionPreferences.getNetconfDeviceCapabilities());
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);