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
9 package org.opendaylight.netconf.topology.singleton.impl;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.cluster.Cluster;
14 import akka.dispatch.OnComplete;
15 import akka.pattern.Patterns;
16 import akka.util.Timeout;
17 import com.google.common.base.Preconditions;
18 import java.util.List;
19 import java.util.stream.Collectors;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMActionService;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
24 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
25 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
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.NetconfDeviceDataBroker;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceNotificationService;
31 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalProvider;
32 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
33 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
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 AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
44 private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
46 private final RemoteDeviceId id;
47 private final Timeout actorResponseWaitTime;
48 private final NetconfDeviceSalProvider salProvider;
49 private final ActorRef masterActorRef;
50 private final ActorSystem actorSystem;
52 private SchemaContext currentSchemaContext = null;
53 private NetconfSessionPreferences netconfSessionPreferences = null;
54 private DOMRpcService deviceRpc = null;
55 private DOMDataBroker deviceDataBroker = null;
57 MasterSalFacade(final RemoteDeviceId id,
58 final ActorSystem actorSystem,
59 final ActorRef masterActorRef,
60 final Timeout actorResponseWaitTime,
61 final DOMMountPointService mountService,
62 final DataBroker dataBroker) {
64 this.salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
65 this.actorSystem = actorSystem;
66 this.masterActorRef = masterActorRef;
67 this.actorResponseWaitTime = actorResponseWaitTime;
71 public void onDeviceConnected(final SchemaContext remoteSchemaContext,
72 final NetconfSessionPreferences sessionPreferences,
73 final DOMRpcService domRpcService, final DOMActionService domActionService) {
74 // We do not support actions in clustered topology yet
75 if (domActionService != null) {
76 LOG.warn("{}: YANG 1.1 actions are not supported in clustered netconf topology, "
77 + "DOMActionService will not be exposed for the device");
80 onDeviceConnected(remoteSchemaContext, sessionPreferences, domRpcService);
84 public void onDeviceConnected(final SchemaContext remoteSchemaContext,
85 final NetconfSessionPreferences sessionPreferences,
86 final DOMRpcService domRpcService) {
87 this.currentSchemaContext = remoteSchemaContext;
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() {
134 Preconditions.checkNotNull(id);
135 Preconditions.checkNotNull(currentSchemaContext,
136 "Device has no remote schema context yet. Probably not fully connected.");
137 Preconditions.checkNotNull(netconfSessionPreferences,
138 "Device has no capabilities yet. Probably not fully connected.");
140 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
142 deviceDataBroker = newDeviceDataBroker();
144 // We need to create ProxyDOMDataBroker so accessing mountpoint
145 // on leader node would be same as on follower node
146 final ProxyDOMDataBroker proxyDataBroker =
147 new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(), actorResponseWaitTime);
148 salProvider.getMountInstance()
149 .onTopologyDeviceConnected(currentSchemaContext, proxyDataBroker, deviceRpc, notificationService);
152 protected DOMDataBroker newDeviceDataBroker() {
153 return new NetconfDeviceDataBroker(id, currentSchemaContext, deviceRpc, netconfSessionPreferences);
156 private Future<Object> sendInitialDataToActor() {
157 final List<SourceIdentifier> sourceIdentifiers =
158 SchemaContextUtil.getConstituentModuleIdentifiers(currentSchemaContext).stream()
159 .map(mi -> RevisionSourceIdentifier.create(mi.getName(), mi.getRevision()))
160 .collect(Collectors.toList());
162 LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}",
163 id, sourceIdentifiers, masterActorRef);
165 // send initial data to master actor
166 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
167 deviceRpc), 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);