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 com.google.common.base.Optional;
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.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.controller.sal.core.api.Broker;
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.NetconfDeviceNotificationService;
29 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalProvider;
30 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
31 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
32 import org.opendaylight.netconf.topology.singleton.impl.tx.NetconfMasterDOMTransaction;
33 import org.opendaylight.netconf.topology.singleton.impl.tx.NetconfProxyDOMTransaction;
34 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
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;
50 private SchemaContext remoteSchemaContext = null;
51 private NetconfSessionPreferences netconfSessionPreferences = null;
52 private DOMRpcService deviceRpc = null;
53 private final NetconfDeviceSalProvider salProvider;
55 private final ActorRef masterActorRef;
56 private final ActorSystem actorSystem;
57 private DOMDataBroker deviceDataBroker = null;
59 MasterSalFacade(final RemoteDeviceId id,
60 final Broker domBroker,
61 final BindingAwareBroker bindingBroker,
62 final ActorSystem actorSystem,
63 final ActorRef masterActorRef) {
65 this.salProvider = new NetconfDeviceSalProvider(id);
66 this.actorSystem = actorSystem;
67 this.masterActorRef = masterActorRef;
69 registerToSal(domBroker, bindingBroker);
72 private void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker) {
73 // TODO: remove use of provider, there is possible directly create mount instance and
74 // TODO: NetconfDeviceTopologyAdapter in constructor = less complexity
76 domRegistryDependency.registerProvider(salProvider);
77 bindingBroker.registerProvider(salProvider);
81 public void onDeviceConnected(final SchemaContext remoteSchemaContext,
82 final NetconfSessionPreferences netconfSessionPreferences,
83 final DOMRpcService deviceRpc) {
84 this.remoteSchemaContext = remoteSchemaContext;
85 this.netconfSessionPreferences = netconfSessionPreferences;
86 this.deviceRpc = deviceRpc;
88 registerMasterMountPoint();
90 sendInitialDataToActor().onComplete(new OnComplete<Object>() {
92 public void onComplete(final Throwable failure, final Object success) throws Throwable {
93 if (failure == null) {
99 }, actorSystem.dispatcher());
104 public void onDeviceDisconnected() {
105 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
106 unregisterMasterMountPoint();
110 public void onDeviceFailed(final Throwable throwable) {
111 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
112 unregisterMasterMountPoint();
116 public void onNotification(final DOMNotification domNotification) {
117 salProvider.getMountInstance().publish(domNotification);
121 public void close() {
122 unregisterMasterMountPoint();
123 closeGracefully(salProvider);
126 private void registerMasterMountPoint() {
127 Preconditions.checkNotNull(id);
128 Preconditions.checkNotNull(remoteSchemaContext,
129 "Device has no remote schema context yet. Probably not fully connected.");
130 Preconditions.checkNotNull(netconfSessionPreferences,
131 "Device has no capabilities yet. Probably not fully connected.");
133 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
135 LOG.info("{}: Creating master data broker for device", id);
137 final NetconfDOMTransaction masterDOMTransactions =
138 new NetconfMasterDOMTransaction(id, remoteSchemaContext, deviceRpc, netconfSessionPreferences);
140 new NetconfDOMDataBroker(actorSystem, id, masterDOMTransactions);
141 // We need to create NetconfProxyDOMTransaction so accessing mountpoint
142 // on leader node would be same as on follower node
143 final NetconfDOMTransaction proxyDOMTransation =
144 new NetconfProxyDOMTransaction(id, actorSystem, masterActorRef);
145 final NetconfDOMDataBroker proxyDataBroker = new NetconfDOMDataBroker(actorSystem, id, proxyDOMTransation);
146 salProvider.getMountInstance()
147 .onTopologyDeviceConnected(remoteSchemaContext, proxyDataBroker, deviceRpc, notificationService);
150 private Future<Object> sendInitialDataToActor() {
151 final List<SourceIdentifier> sourceIdentifiers =
152 remoteSchemaContext.getAllModuleIdentifiers().stream().map(mi ->
153 RevisionSourceIdentifier.create(mi.getName(),
154 (SimpleDateFormatUtil.DEFAULT_DATE_REV == mi.getRevision() ? Optional.<String>absent() :
155 Optional.of(SimpleDateFormatUtil.getRevisionFormat().format(mi.getRevision())))))
156 .collect(Collectors.toList());
158 // send initial data to master actor and create actor for providing it
159 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
160 deviceRpc), NetconfTopologyUtils.TIMEOUT);
163 private void updateDeviceData() {
164 Cluster cluster = Cluster.get(actorSystem);
165 salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, cluster.selfAddress().toString(),
166 netconfSessionPreferences.getNetconfDeviceCapabilities());
169 private void unregisterMasterMountPoint() {
170 salProvider.getMountInstance().onTopologyDeviceDisconnected();
173 private void closeGracefully(final AutoCloseable resource) {
174 if (resource != null) {
177 } catch (final Exception e) {
178 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);