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.Optional;
18 import com.google.common.base.Preconditions;
19 import java.util.List;
20 import java.util.stream.Collectors;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
22 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
23 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
24 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
25 import org.opendaylight.controller.sal.core.api.Broker;
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.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.api.NetconfDOMTransaction;
33 import org.opendaylight.netconf.topology.singleton.impl.tx.NetconfMasterDOMTransaction;
34 import org.opendaylight.netconf.topology.singleton.impl.tx.NetconfProxyDOMTransaction;
35 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
36 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
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;
51 private SchemaContext remoteSchemaContext = null;
52 private NetconfSessionPreferences netconfSessionPreferences = null;
53 private DOMRpcService deviceRpc = null;
54 private final NetconfDeviceSalProvider salProvider;
56 private final ActorRef masterActorRef;
57 private final ActorSystem actorSystem;
58 private DOMDataBroker deviceDataBroker = null;
60 MasterSalFacade(final RemoteDeviceId id,
61 final Broker domBroker,
62 final BindingAwareBroker bindingBroker,
63 final ActorSystem actorSystem,
64 final ActorRef masterActorRef,
65 final Timeout actorResponseWaitTime) {
67 this.salProvider = new NetconfDeviceSalProvider(id);
68 this.actorSystem = actorSystem;
69 this.masterActorRef = masterActorRef;
70 this.actorResponseWaitTime = actorResponseWaitTime;
72 registerToSal(domBroker, bindingBroker);
75 private void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker) {
76 // TODO: remove use of provider, there is possible directly create mount instance and
77 // TODO: NetconfDeviceTopologyAdapter in constructor = less complexity
79 domRegistryDependency.registerProvider(salProvider);
80 bindingBroker.registerProvider(salProvider);
84 public void onDeviceConnected(final SchemaContext remoteSchemaContext,
85 final NetconfSessionPreferences netconfSessionPreferences,
86 final DOMRpcService deviceRpc) {
87 this.remoteSchemaContext = remoteSchemaContext;
88 this.netconfSessionPreferences = netconfSessionPreferences;
89 this.deviceRpc = deviceRpc;
91 registerMasterMountPoint();
93 sendInitialDataToActor().onComplete(new OnComplete<Object>() {
95 public void onComplete(final Throwable failure, final Object success) throws Throwable {
96 if (failure == null) {
102 }, actorSystem.dispatcher());
107 public void onDeviceDisconnected() {
108 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
109 unregisterMasterMountPoint();
113 public void onDeviceFailed(final Throwable throwable) {
114 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
115 unregisterMasterMountPoint();
119 public void onNotification(final DOMNotification domNotification) {
120 salProvider.getMountInstance().publish(domNotification);
124 public void close() {
125 unregisterMasterMountPoint();
126 closeGracefully(salProvider);
129 private void registerMasterMountPoint() {
130 Preconditions.checkNotNull(id);
131 Preconditions.checkNotNull(remoteSchemaContext,
132 "Device has no remote schema context yet. Probably not fully connected.");
133 Preconditions.checkNotNull(netconfSessionPreferences,
134 "Device has no capabilities yet. Probably not fully connected.");
136 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
138 LOG.info("{}: Creating master data broker for device", id);
140 final NetconfDOMTransaction masterDOMTransactions =
141 new NetconfMasterDOMTransaction(id, remoteSchemaContext, deviceRpc, netconfSessionPreferences);
143 new NetconfDOMDataBroker(actorSystem, id, masterDOMTransactions);
144 // We need to create NetconfProxyDOMTransaction so accessing mountpoint
145 // on leader node would be same as on follower node
146 final NetconfDOMTransaction proxyDOMTransation =
147 new NetconfProxyDOMTransaction(id, actorSystem, masterActorRef, actorResponseWaitTime);
148 final NetconfDOMDataBroker proxyDataBroker = new NetconfDOMDataBroker(actorSystem, id, proxyDOMTransation);
149 salProvider.getMountInstance()
150 .onTopologyDeviceConnected(remoteSchemaContext, proxyDataBroker, deviceRpc, notificationService);
153 private Future<Object> sendInitialDataToActor() {
154 final List<SourceIdentifier> sourceIdentifiers =
155 remoteSchemaContext.getAllModuleIdentifiers().stream().map(mi ->
156 RevisionSourceIdentifier.create(mi.getName(),
157 (SimpleDateFormatUtil.DEFAULT_DATE_REV == mi.getRevision() ? Optional.<String>absent() :
158 Optional.of(mi.getQNameModule().getFormattedRevision()))))
159 .collect(Collectors.toList());
161 // send initial data to master actor and create actor for providing it
162 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
163 deviceRpc), actorResponseWaitTime);
166 private void updateDeviceData() {
167 final Cluster cluster = Cluster.get(actorSystem);
168 salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, cluster.selfAddress().toString(),
169 netconfSessionPreferences.getNetconfDeviceCapabilities());
172 private void unregisterMasterMountPoint() {
173 salProvider.getMountInstance().onTopologyDeviceDisconnected();
176 private void closeGracefully(final AutoCloseable resource) {
177 if (resource != null) {
180 } catch (final Exception e) {
181 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);