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 java.util.stream.Collectors;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
21 import org.opendaylight.mdsal.dom.api.DOMActionService;
22 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
23 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
24 import org.opendaylight.mdsal.dom.api.DOMNotification;
25 import org.opendaylight.mdsal.dom.api.DOMRpcService;
26 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
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.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.RevisionSourceIdentifier;
38 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import scala.concurrent.Future;
44 class MasterSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
46 private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
48 private final RemoteDeviceId id;
49 private final Timeout actorResponseWaitTime;
50 private final NetconfDeviceSalProvider salProvider;
51 private final ActorRef masterActorRef;
52 private final ActorSystem actorSystem;
54 private MountPointContext currentMountContext = null;
55 private NetconfSessionPreferences netconfSessionPreferences = null;
56 private DOMRpcService deviceRpc = null;
57 private DOMDataBroker deviceDataBroker = null;
58 private NetconfDataTreeService netconfService = null;
59 private DOMActionService deviceAction = null;
61 MasterSalFacade(final RemoteDeviceId id,
62 final ActorSystem actorSystem,
63 final ActorRef masterActorRef,
64 final Timeout actorResponseWaitTime,
65 final DOMMountPointService mountService,
66 final DataBroker dataBroker) {
68 this.salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
69 this.actorSystem = actorSystem;
70 this.masterActorRef = masterActorRef;
71 this.actorResponseWaitTime = actorResponseWaitTime;
75 public void onDeviceConnected(final MountPointContext mountContext,
76 final NetconfSessionPreferences sessionPreferences,
77 final DOMRpcService domRpcService, final DOMActionService domActionService) {
78 this.deviceAction = domActionService;
79 LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, "
80 + "DOMActionService exposed for the device", id);
81 onDeviceConnected(mountContext, sessionPreferences, domRpcService);
85 public void onDeviceConnected(final MountPointContext mountContext,
86 final NetconfSessionPreferences sessionPreferences,
87 final DOMRpcService domRpcService) {
88 this.currentMountContext = mountContext;
89 this.netconfSessionPreferences = sessionPreferences;
90 this.deviceRpc = domRpcService;
92 LOG.info("Device {} connected - registering master mount point", id);
94 registerMasterMountPoint();
96 sendInitialDataToActor().onComplete(new OnComplete<>() {
98 public void onComplete(final Throwable failure, final Object success) {
99 if (failure == null) {
104 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
106 }, actorSystem.dispatcher());
111 public void onDeviceDisconnected() {
112 LOG.info("Device {} disconnected - unregistering master mount point", id);
113 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
114 unregisterMasterMountPoint();
118 public void onDeviceFailed(final Throwable throwable) {
119 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
120 unregisterMasterMountPoint();
124 public void onNotification(final DOMNotification domNotification) {
125 salProvider.getMountInstance().publish(domNotification);
129 public void close() {
130 unregisterMasterMountPoint();
131 closeGracefully(salProvider);
134 private void registerMasterMountPoint() {
136 requireNonNull(currentMountContext, "Device has no remote schema context yet. Probably not fully connected.");
137 requireNonNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
139 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
140 deviceDataBroker = newDeviceDataBroker();
141 netconfService = newNetconfDataTreeService();
143 // We need to create ProxyDOMDataBroker so accessing mountpoint
144 // on leader node would be same as on follower node
145 final ProxyDOMDataBroker proxyDataBroker = new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(),
146 actorResponseWaitTime);
147 final NetconfDataTreeService proxyNetconfService = new ProxyNetconfDataTreeService(id, masterActorRef,
148 actorSystem.dispatcher(), actorResponseWaitTime);
149 salProvider.getMountInstance().onTopologyDeviceConnected(currentMountContext.getEffectiveModelContext(),
150 proxyDataBroker, proxyNetconfService, deviceRpc, notificationService, deviceAction);
153 protected DOMDataBroker newDeviceDataBroker() {
154 return new NetconfDeviceDataBroker(id, currentMountContext, deviceRpc, netconfSessionPreferences);
157 protected NetconfDataTreeService newNetconfDataTreeService() {
158 return AbstractNetconfDataTreeService.of(id, currentMountContext, deviceRpc, netconfSessionPreferences);
161 private Future<Object> sendInitialDataToActor() {
162 final List<SourceIdentifier> sourceIdentifiers = SchemaContextUtil.getConstituentModuleIdentifiers(
163 currentMountContext.getEffectiveModelContext()).stream()
164 .map(mi -> RevisionSourceIdentifier.create(mi.getName(), mi.getRevision()))
165 .collect(Collectors.toList());
167 LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
170 // send initial data to master actor
171 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, netconfService,
172 sourceIdentifiers, deviceRpc, deviceAction), actorResponseWaitTime);
175 private void updateDeviceData() {
176 final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
177 LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
178 salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
179 netconfSessionPreferences.getNetconfDeviceCapabilities());
182 private void unregisterMasterMountPoint() {
183 salProvider.getMountInstance().onTopologyDeviceDisconnected();
186 @SuppressWarnings("checkstyle:IllegalCatch")
187 private void closeGracefully(final AutoCloseable resource) {
188 if (resource != null) {
191 } catch (final Exception e) {
192 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);