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.binding.api.DataBroker;
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.common.SimpleDateFormatUtil;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
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 remoteSchemaContext = 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 netconfSessionPreferences,
73 final DOMRpcService deviceRpc) {
74 this.remoteSchemaContext = remoteSchemaContext;
75 this.netconfSessionPreferences = netconfSessionPreferences;
76 this.deviceRpc = deviceRpc;
78 registerMasterMountPoint();
80 sendInitialDataToActor().onComplete(new OnComplete<Object>() {
82 public void onComplete(final Throwable failure, final Object success) throws Throwable {
83 if (failure == null) {
89 }, actorSystem.dispatcher());
94 public void onDeviceDisconnected() {
95 salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
96 unregisterMasterMountPoint();
100 public void onDeviceFailed(final Throwable throwable) {
101 salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
102 unregisterMasterMountPoint();
106 public void onNotification(final DOMNotification domNotification) {
107 salProvider.getMountInstance().publish(domNotification);
111 public void close() {
112 unregisterMasterMountPoint();
113 closeGracefully(salProvider);
116 private void registerMasterMountPoint() {
117 Preconditions.checkNotNull(id);
118 Preconditions.checkNotNull(remoteSchemaContext,
119 "Device has no remote schema context yet. Probably not fully connected.");
120 Preconditions.checkNotNull(netconfSessionPreferences,
121 "Device has no capabilities yet. Probably not fully connected.");
123 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
125 LOG.info("{}: Creating master data broker for device", id);
127 deviceDataBroker = new NetconfDeviceDataBroker(id, remoteSchemaContext, deviceRpc, netconfSessionPreferences);
128 // We need to create ProxyDOMDataBroker so accessing mountpoint
129 // on leader node would be same as on follower node
130 final ProxyDOMDataBroker proxyDataBroker =
131 new ProxyDOMDataBroker(actorSystem, id, masterActorRef, actorResponseWaitTime);
132 salProvider.getMountInstance()
133 .onTopologyDeviceConnected(remoteSchemaContext, proxyDataBroker, deviceRpc, notificationService);
136 private Future<Object> sendInitialDataToActor() {
137 final List<SourceIdentifier> sourceIdentifiers =
138 remoteSchemaContext.getAllModuleIdentifiers().stream().map(mi ->
139 RevisionSourceIdentifier.create(mi.getName(),
140 (SimpleDateFormatUtil.DEFAULT_DATE_REV == mi.getRevision() ? Optional.<String>absent() :
141 Optional.of(mi.getQNameModule().getFormattedRevision()))))
142 .collect(Collectors.toList());
144 // send initial data to master actor and create actor for providing it
145 return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
146 deviceRpc), actorResponseWaitTime);
149 private void updateDeviceData() {
150 final Cluster cluster = Cluster.get(actorSystem);
151 salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, cluster.selfAddress().toString(),
152 netconfSessionPreferences.getNetconfDeviceCapabilities());
155 private void unregisterMasterMountPoint() {
156 salProvider.getMountInstance().onTopologyDeviceDisconnected();
159 @SuppressWarnings("checkstyle:IllegalCatch")
160 private void closeGracefully(final AutoCloseable resource) {
161 if (resource != null) {
164 } catch (final Exception e) {
165 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);