2 * Copyright (c) 2015 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.pipeline;
11 import akka.actor.ActorContext;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.actor.TypedActor;
15 import akka.actor.TypedProps;
16 import akka.cluster.Cluster;
17 import akka.cluster.Member;
18 import akka.japi.Creator;
19 import com.google.common.base.Preconditions;
20 import java.util.ArrayList;
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.NetconfSessionPreferences;
28 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceNotificationService;
29 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
30 import org.opendaylight.netconf.topology.util.messages.AnnounceMasterMountPoint;
31 import org.opendaylight.netconf.topology.util.messages.AnnounceMasterMountPointDown;
32 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
36 public class TopologyMountPointFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
38 private static final Logger LOG = LoggerFactory.getLogger(TopologyMountPointFacade.class);
40 private static final String MOUNT_POINT = "mountpoint";
42 private final String topologyId;
43 private final RemoteDeviceId id;
44 private final Broker domBroker;
45 private final BindingAwareBroker bindingBroker;
46 private final long defaultRequestTimeoutMillis;
48 private SchemaContext remoteSchemaContext = null;
49 private NetconfSessionPreferences netconfSessionPreferences = null;
50 private DOMRpcService deviceRpc = null;
51 private final ClusteredNetconfDeviceMountInstanceProxy salProvider;
53 private ActorSystem actorSystem;
54 private DOMDataBroker deviceDataBroker = null;
56 private final ArrayList<RemoteDeviceHandler<NetconfSessionPreferences>> connectionStatusListeners = new ArrayList<>();
58 public TopologyMountPointFacade(final String topologyId,
59 final RemoteDeviceId id,
60 final Broker domBroker,
61 final BindingAwareBroker bindingBroker,
62 long defaultRequestTimeoutMillis) {
63 this.topologyId = topologyId;
65 this.domBroker = domBroker;
66 this.bindingBroker = bindingBroker;
67 this.defaultRequestTimeoutMillis = defaultRequestTimeoutMillis;
68 this.salProvider = new ClusteredNetconfDeviceMountInstanceProxy(id);
69 registerToSal(domBroker);
72 public void registerToSal(final Broker domRegistryDependency) {
73 domRegistryDependency.registerProvider(salProvider);
77 public void onDeviceConnected(final SchemaContext remoteSchemaContext,
78 final NetconfSessionPreferences netconfSessionPreferences,
79 final DOMRpcService deviceRpc) {
80 // prepare our prerequisites for mountpoint
81 LOG.debug("Mount point facade onConnected capabilities {}", netconfSessionPreferences);
82 this.remoteSchemaContext = remoteSchemaContext;
83 this.netconfSessionPreferences = netconfSessionPreferences;
84 this.deviceRpc = deviceRpc;
85 for (RemoteDeviceHandler<NetconfSessionPreferences> listener : connectionStatusListeners) {
86 listener.onDeviceConnected(remoteSchemaContext, netconfSessionPreferences, deviceRpc);
91 public void onDeviceDisconnected() {
92 // do not unregister mount point here, this gets handle by the underlying call from role change callback
93 for (RemoteDeviceHandler<NetconfSessionPreferences> listener : connectionStatusListeners) {
94 listener.onDeviceDisconnected();
99 public void onDeviceFailed(Throwable throwable) {
100 // do not unregister mount point here, this gets handle by the underlying call from role change callback
101 for (RemoteDeviceHandler<NetconfSessionPreferences> listener : connectionStatusListeners) {
102 listener.onDeviceFailed(throwable);
107 public void onNotification(DOMNotification domNotification) {
108 salProvider.getMountInstance().publish(domNotification);
111 public void registerMountPoint(final ActorSystem actorSystem, final ActorContext context) {
112 if (remoteSchemaContext == null || netconfSessionPreferences == null) {
113 LOG.debug("Master mount point does not have schemas ready yet, delaying registration");
117 Preconditions.checkNotNull(id);
118 Preconditions.checkNotNull(remoteSchemaContext, "Device has no remote schema context yet. Probably not fully connected.");
119 Preconditions.checkNotNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
120 this.actorSystem = actorSystem;
121 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
123 LOG.warn("Creating master data broker for device {}", id);
124 deviceDataBroker = TypedActor.get(context).typedActorOf(new TypedProps<>(ProxyNetconfDeviceDataBroker.class, new Creator<NetconfDeviceMasterDataBroker>() {
126 public NetconfDeviceMasterDataBroker create() throws Exception {
127 return new NetconfDeviceMasterDataBroker(actorSystem, id, remoteSchemaContext, deviceRpc, netconfSessionPreferences, defaultRequestTimeoutMillis);
130 LOG.debug("Master data broker registered on path {}", TypedActor.get(actorSystem).getActorRefFor(deviceDataBroker).path());
131 salProvider.getMountInstance().onTopologyDeviceConnected(remoteSchemaContext, deviceDataBroker, deviceRpc, notificationService);
132 final Cluster cluster = Cluster.get(actorSystem);
133 final Iterable<Member> members = cluster.state().getMembers();
134 final ActorRef deviceDataBrokerRef = TypedActor.get(actorSystem).getActorRefFor(deviceDataBroker);
135 for (final Member member : members) {
136 if (!member.address().equals(cluster.selfAddress())) {
137 final String path = member.address() + "/user/" + topologyId + "/" + id.getName();
138 actorSystem.actorSelection(path).tell(new AnnounceMasterMountPoint(), deviceDataBrokerRef);
143 public void registerMountPoint(final ActorSystem actorSystem, final ActorContext context, final ActorRef masterRef) {
144 if (remoteSchemaContext == null || netconfSessionPreferences == null) {
145 LOG.debug("Slave mount point does not have schemas ready yet, delaying registration");
149 Preconditions.checkNotNull(id);
150 Preconditions.checkNotNull(remoteSchemaContext, "Device has no remote schema context yet. Probably not fully connected.");
151 Preconditions.checkNotNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
152 this.actorSystem = actorSystem;
153 final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
155 final ProxyNetconfDeviceDataBroker masterDataBroker = TypedActor.get(actorSystem).typedActorOf(new TypedProps<>(ProxyNetconfDeviceDataBroker.class, NetconfDeviceMasterDataBroker.class), masterRef);
156 LOG.warn("Creating slave data broker for device {}", id);
157 final DOMDataBroker deviceDataBroker = new NetconfDeviceSlaveDataBroker(actorSystem, id, masterDataBroker);
158 salProvider.getMountInstance().onTopologyDeviceConnected(remoteSchemaContext, deviceDataBroker, deviceRpc, notificationService);
161 public void unregisterMountPoint() {
162 salProvider.getMountInstance().onTopologyDeviceDisconnected();
163 if (deviceDataBroker != null) {
164 LOG.debug("Stopping master data broker for device {}", id.getName());
165 for (final Member member : Cluster.get(actorSystem).state().getMembers()) {
166 if (member.address().equals(Cluster.get(actorSystem).selfAddress())) {
169 actorSystem.actorSelection(member.address() + "/user/" + topologyId + "/" + id.getName()).tell(new AnnounceMasterMountPointDown(), null);
171 TypedActor.get(actorSystem).stop(deviceDataBroker);
172 deviceDataBroker = null;
176 public ConnectionStatusListenerRegistration registerConnectionStatusListener(final RemoteDeviceHandler<NetconfSessionPreferences> listener) {
177 connectionStatusListeners.add(listener);
178 return new ConnectionStatusListenerRegistration(listener);
182 public void close() {
183 closeGracefully(salProvider);
186 private void closeGracefully(final AutoCloseable resource) {
187 if (resource != null) {
190 } catch (final Exception e) {
191 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
196 public class ConnectionStatusListenerRegistration{
198 private final RemoteDeviceHandler<NetconfSessionPreferences> listener;
200 public ConnectionStatusListenerRegistration(final RemoteDeviceHandler<NetconfSessionPreferences> listener) {
201 this.listener = listener;
204 public void close() {
205 connectionStatusListeners.remove(listener);