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 akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.cluster.Cluster;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.Patterns;
15 import akka.util.Timeout;
16 import com.google.common.base.Preconditions;
17 import java.util.List;
18 import java.util.stream.Collectors;
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.sal.connect.api.RemoteDeviceHandler;
26 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
27 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
28 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceDataBroker;
29 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceNotificationService;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalProvider;
31 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
32 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
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 AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
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 SchemaContext currentSchemaContext = null;
52 private NetconfSessionPreferences netconfSessionPreferences = null;
53 private DOMRpcService deviceRpc = null;
54 private DOMDataBroker deviceDataBroker = null;
56 MasterSalFacade(final RemoteDeviceId id,
57 final ActorSystem actorSystem,
58 final ActorRef masterActorRef,
59 final Timeout actorResponseWaitTime,
60 final DOMMountPointService mountService,
61 final DataBroker dataBroker) {
63 this.salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
64 this.actorSystem = actorSystem;
65 this.masterActorRef = masterActorRef;
66 this.actorResponseWaitTime = actorResponseWaitTime;
70 public void onDeviceConnected(final SchemaContext remoteSchemaContext,
71 final NetconfSessionPreferences sessionPreferences,
72 final DOMRpcService domRpcService, final DOMActionService domActionService) {
73 // We do not support actions in clustered topology yet
74 if (domActionService != null) {
75 LOG.warn("{}: YANG 1.1 actions are not supported in clustered netconf topology, "
76 + "DOMActionService will not be exposed for the device", id);
79 onDeviceConnected(remoteSchemaContext, sessionPreferences, domRpcService);
83 public void onDeviceConnected(final SchemaContext remoteSchemaContext,
84 final NetconfSessionPreferences sessionPreferences,
85 final DOMRpcService domRpcService) {
86 this.currentSchemaContext = remoteSchemaContext;
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<Object>() {
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() {
133 Preconditions.checkNotNull(id);
134 Preconditions.checkNotNull(currentSchemaContext,
135 "Device has no remote schema context yet. Probably not fully connected.");
136 Preconditions.checkNotNull(netconfSessionPreferences,
137 "Device has no capabilities yet. Probably not fully connected.");
139 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
141 deviceDataBroker = newDeviceDataBroker();
143 // We need to create ProxyDOMDataBroker so accessing mountpoint
144 // on leader node would be same as on follower node
145 final ProxyDOMDataBroker proxyDataBroker =
146 new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(), actorResponseWaitTime);
147 salProvider.getMountInstance()
148 .onTopologyDeviceConnected(currentSchemaContext, proxyDataBroker, deviceRpc, notificationService);
151 protected DOMDataBroker newDeviceDataBroker() {
152 return new NetconfDeviceDataBroker(id, currentSchemaContext, deviceRpc, netconfSessionPreferences);
155 private Future<Object> sendInitialDataToActor() {
156 final List<SourceIdentifier> sourceIdentifiers =
157 SchemaContextUtil.getConstituentModuleIdentifiers(currentSchemaContext).stream()
158 .map(mi -> RevisionSourceIdentifier.create(mi.getName(), mi.getRevision()))
159 .collect(Collectors.toList());
161 LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}",
162 id, sourceIdentifiers, masterActorRef);
164 // send initial data to master actor
165 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
166 deviceRpc), actorResponseWaitTime);
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);