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.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.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.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import scala.concurrent.Future;
42 class MasterSalFacade implements RemoteDeviceHandler, AutoCloseable {
43 private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
45 private final RemoteDeviceId id;
46 private final Timeout actorResponseWaitTime;
47 private final NetconfDeviceSalProvider salProvider;
48 private final ActorRef masterActorRef;
49 private final ActorSystem actorSystem;
51 private MountPointContext currentMountContext = null;
52 private NetconfSessionPreferences netconfSessionPreferences = null;
53 private DOMRpcService deviceRpc = null;
54 private DOMDataBroker deviceDataBroker = null;
55 private NetconfDataTreeService netconfService = null;
56 private DOMActionService deviceAction = null;
58 MasterSalFacade(final RemoteDeviceId id,
59 final ActorSystem actorSystem,
60 final ActorRef masterActorRef,
61 final Timeout actorResponseWaitTime,
62 final DOMMountPointService mountService,
63 final DataBroker dataBroker) {
65 salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
66 this.actorSystem = actorSystem;
67 this.masterActorRef = masterActorRef;
68 this.actorResponseWaitTime = actorResponseWaitTime;
72 public void onDeviceConnected(final MountPointContext mountContext,
73 final NetconfSessionPreferences sessionPreferences,
74 final DOMRpcService domRpcService, final DOMActionService domActionService) {
75 deviceAction = domActionService;
76 LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, "
77 + "DOMActionService exposed for the device", id);
78 onDeviceConnected(mountContext, sessionPreferences, domRpcService);
82 public void onDeviceConnected(final MountPointContext mountContext,
83 final NetconfSessionPreferences sessionPreferences,
84 final DOMRpcService domRpcService) {
85 currentMountContext = mountContext;
86 netconfSessionPreferences = sessionPreferences;
87 deviceRpc = domRpcService;
89 LOG.info("Device {} connected - registering master mount point", id);
91 registerMasterMountPoint();
93 sendInitialDataToActor().onComplete(new OnComplete<>() {
95 public void onComplete(final Throwable failure, final Object success) {
96 if (failure == null) {
101 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
103 }, actorSystem.dispatcher());
108 public void onDeviceDisconnected() {
109 LOG.info("Device {} disconnected - unregistering master mount point", id);
110 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
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() {
133 requireNonNull(currentMountContext, "Device has no remote schema context yet. Probably not fully connected.");
134 requireNonNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
136 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
137 deviceDataBroker = newDeviceDataBroker();
138 netconfService = newNetconfDataTreeService();
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 final NetconfDataTreeService proxyNetconfService = new ProxyNetconfDataTreeService(id, masterActorRef,
145 actorSystem.dispatcher(), actorResponseWaitTime);
146 salProvider.getMountInstance().onTopologyDeviceConnected(currentMountContext.getEffectiveModelContext(),
147 proxyDataBroker, proxyNetconfService, deviceRpc, notificationService, deviceAction);
150 protected DOMDataBroker newDeviceDataBroker() {
151 return new NetconfDeviceDataBroker(id, currentMountContext, deviceRpc, netconfSessionPreferences);
154 protected NetconfDataTreeService newNetconfDataTreeService() {
155 return AbstractNetconfDataTreeService.of(id, currentMountContext, deviceRpc, netconfSessionPreferences);
158 private Future<Object> sendInitialDataToActor() {
159 final List<SourceIdentifier> sourceIdentifiers = List.copyOf(SchemaContextUtil.getConstituentModuleIdentifiers(
160 currentMountContext.getEffectiveModelContext()));
162 LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
165 // send initial data to master actor
166 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, netconfService,
167 sourceIdentifiers, deviceRpc, deviceAction), actorResponseWaitTime);
170 private void updateDeviceData() {
171 final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
172 LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
173 salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
174 netconfSessionPreferences.getNetconfDeviceCapabilities());
177 private void unregisterMasterMountPoint() {
178 salProvider.getMountInstance().onTopologyDeviceDisconnected();
181 @SuppressWarnings("checkstyle:IllegalCatch")
182 private void closeGracefully(final AutoCloseable resource) {
183 if (resource != null) {
186 } catch (final Exception e) {
187 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);