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.api.SchemaContext;
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 SchemaContext currentSchemaContext = null;
55 private NetconfSessionPreferences netconfSessionPreferences = null;
56 private DOMRpcService deviceRpc = null;
57 private DOMDataBroker deviceDataBroker = null;
58 private DOMActionService deviceAction = null;
60 MasterSalFacade(final RemoteDeviceId id,
61 final ActorSystem actorSystem,
62 final ActorRef masterActorRef,
63 final Timeout actorResponseWaitTime,
64 final DOMMountPointService mountService,
65 final DataBroker dataBroker) {
67 this.salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
68 this.actorSystem = actorSystem;
69 this.masterActorRef = masterActorRef;
70 this.actorResponseWaitTime = actorResponseWaitTime;
74 public void onDeviceConnected(final MountPointContext mountContext,
75 final NetconfSessionPreferences sessionPreferences,
76 final DOMRpcService domRpcService, final DOMActionService domActionService) {
77 this.deviceAction = domActionService;
78 LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, "
79 + "DOMActionService exposed for the device", id);
80 onDeviceConnected(mountContext, sessionPreferences, domRpcService);
84 public void onDeviceConnected(final MountPointContext mountContext,
85 final NetconfSessionPreferences sessionPreferences,
86 final DOMRpcService domRpcService) {
87 this.currentSchemaContext = mountContext.getSchemaContext();
88 this.netconfSessionPreferences = sessionPreferences;
89 this.deviceRpc = domRpcService;
91 LOG.info("Device {} connected - registering master mount point", id);
93 registerMasterMountPoint();
95 sendInitialDataToActor().onComplete(new OnComplete<Object>() {
97 public void onComplete(final Throwable failure, final Object success) {
98 if (failure == null) {
103 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
105 }, actorSystem.dispatcher());
110 public void onDeviceDisconnected() {
111 LOG.info("Device {} disconnected - unregistering master mount point", id);
112 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
113 unregisterMasterMountPoint();
117 public void onDeviceFailed(final Throwable throwable) {
118 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
119 unregisterMasterMountPoint();
123 public void onNotification(final DOMNotification domNotification) {
124 salProvider.getMountInstance().publish(domNotification);
128 public void close() {
129 unregisterMasterMountPoint();
130 closeGracefully(salProvider);
133 private void registerMasterMountPoint() {
135 requireNonNull(currentSchemaContext, "Device has no remote schema context yet. Probably not fully connected.");
136 requireNonNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
138 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
139 deviceDataBroker = newDeviceDataBroker();
141 // We need to create ProxyDOMDataBroker so accessing mountpoint
142 // on leader node would be same as on follower node
143 final ProxyDOMDataBroker proxyDataBroker = new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(),
144 actorResponseWaitTime);
145 salProvider.getMountInstance().onTopologyDeviceConnected(currentSchemaContext,
146 proxyDataBroker, deviceRpc, notificationService, deviceAction);
149 protected DOMDataBroker newDeviceDataBroker() {
150 return new NetconfDeviceDataBroker(id, currentSchemaContext, deviceRpc, netconfSessionPreferences);
153 private Future<Object> sendInitialDataToActor() {
154 final List<SourceIdentifier> sourceIdentifiers =
155 SchemaContextUtil.getConstituentModuleIdentifiers(currentSchemaContext).stream()
156 .map(mi -> RevisionSourceIdentifier.create(mi.getName(), mi.getRevision()))
157 .collect(Collectors.toList());
159 LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
162 // send initial data to master actor
163 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
164 deviceRpc, deviceAction), actorResponseWaitTime);
167 @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
168 justification = "https://github.com/spotbugs/spotbugs/issues/811")
169 private void updateDeviceData() {
170 final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
171 LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
172 salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
173 netconfSessionPreferences.getNetconfDeviceCapabilities());
176 private void unregisterMasterMountPoint() {
177 salProvider.getMountInstance().onTopologyDeviceDisconnected();
180 @SuppressWarnings("checkstyle:IllegalCatch")
181 private void closeGracefully(final AutoCloseable resource) {
182 if (resource != null) {
185 } catch (final Exception e) {
186 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);