Bug 4940 - correctly implement default-request-timeout-millis
[netconf.git] / opendaylight / netconf / netconf-topology / src / main / java / org / opendaylight / netconf / topology / pipeline / TopologyMountPointFacade.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.netconf.topology.pipeline;
10
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;
35
36 public class TopologyMountPointFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
37
38     private static final Logger LOG = LoggerFactory.getLogger(TopologyMountPointFacade.class);
39
40     private static final String MOUNT_POINT = "mountpoint";
41
42     private final String topologyId;
43     private final RemoteDeviceId id;
44     private final Broker domBroker;
45     private final BindingAwareBroker bindingBroker;
46
47     private SchemaContext remoteSchemaContext = null;
48     private NetconfSessionPreferences netconfSessionPreferences = null;
49     private DOMRpcService deviceRpc = null;
50     private final ClusteredNetconfDeviceMountInstanceProxy salProvider;
51
52     private ActorSystem actorSystem;
53     private DOMDataBroker deviceDataBroker = null;
54
55     private final ArrayList<RemoteDeviceHandler<NetconfSessionPreferences>> connectionStatusListeners = new ArrayList<>();
56
57     public TopologyMountPointFacade(final String topologyId,
58                                     final RemoteDeviceId id,
59                                     final Broker domBroker,
60                                     final BindingAwareBroker bindingBroker) {
61         this.topologyId = topologyId;
62         this.id = id;
63         this.domBroker = domBroker;
64         this.bindingBroker = bindingBroker;
65         this.salProvider = new ClusteredNetconfDeviceMountInstanceProxy(id);
66         registerToSal(domBroker);
67     }
68
69     public void registerToSal(final Broker domRegistryDependency) {
70         domRegistryDependency.registerProvider(salProvider);
71     }
72
73     @Override
74     public void onDeviceConnected(final SchemaContext remoteSchemaContext,
75                                   final NetconfSessionPreferences netconfSessionPreferences,
76                                   final DOMRpcService deviceRpc) {
77         // prepare our prerequisites for mountpoint
78         LOG.debug("Mount point facade onConnected capabilities {}", netconfSessionPreferences);
79         this.remoteSchemaContext = remoteSchemaContext;
80         this.netconfSessionPreferences = netconfSessionPreferences;
81         this.deviceRpc = deviceRpc;
82         for (RemoteDeviceHandler<NetconfSessionPreferences> listener : connectionStatusListeners) {
83             listener.onDeviceConnected(remoteSchemaContext, netconfSessionPreferences, deviceRpc);
84         }
85     }
86
87     @Override
88     public void onDeviceDisconnected() {
89         // do not unregister mount point here, this gets handle by the underlying call from role change callback
90         for (RemoteDeviceHandler<NetconfSessionPreferences> listener : connectionStatusListeners) {
91             listener.onDeviceDisconnected();
92         }
93     }
94
95     @Override
96     public void onDeviceFailed(Throwable throwable) {
97         // do not unregister mount point here, this gets handle by the underlying call from role change callback
98         for (RemoteDeviceHandler<NetconfSessionPreferences> listener : connectionStatusListeners) {
99             listener.onDeviceFailed(throwable);
100         }
101     }
102
103     @Override
104     public void onNotification(DOMNotification domNotification) {
105         salProvider.getMountInstance().publish(domNotification);
106     }
107
108     public void registerMountPoint(final ActorSystem actorSystem, final ActorContext context) {
109         if (remoteSchemaContext == null || netconfSessionPreferences == null) {
110             LOG.debug("Master mount point does not have schemas ready yet, delaying registration");
111             return;
112         }
113
114         Preconditions.checkNotNull(id);
115         Preconditions.checkNotNull(remoteSchemaContext, "Device has no remote schema context yet. Probably not fully connected.");
116         Preconditions.checkNotNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
117         this.actorSystem = actorSystem;
118         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
119
120         LOG.warn("Creating master data broker for device {}", id);
121         deviceDataBroker = TypedActor.get(context).typedActorOf(new TypedProps<>(ProxyNetconfDeviceDataBroker.class, new Creator<NetconfDeviceMasterDataBroker>() {
122             @Override
123             public NetconfDeviceMasterDataBroker create() throws Exception {
124                 return new NetconfDeviceMasterDataBroker(actorSystem, id, remoteSchemaContext, deviceRpc, netconfSessionPreferences);
125             }
126         }), MOUNT_POINT);
127         LOG.debug("Master data broker registered on path {}", TypedActor.get(actorSystem).getActorRefFor(deviceDataBroker).path());
128         salProvider.getMountInstance().onTopologyDeviceConnected(remoteSchemaContext, deviceDataBroker, deviceRpc, notificationService);
129         final Cluster cluster = Cluster.get(actorSystem);
130         final Iterable<Member> members = cluster.state().getMembers();
131         final ActorRef deviceDataBrokerRef = TypedActor.get(actorSystem).getActorRefFor(deviceDataBroker);
132         for (final Member member : members) {
133             if (!member.address().equals(cluster.selfAddress())) {
134                 final String path = member.address() + "/user/" + topologyId + "/" + id.getName();
135                 actorSystem.actorSelection(path).tell(new AnnounceMasterMountPoint(), deviceDataBrokerRef);
136             }
137         }
138     }
139
140     public void registerMountPoint(final ActorSystem actorSystem, final ActorContext context, final ActorRef masterRef) {
141         if (remoteSchemaContext == null || netconfSessionPreferences == null) {
142             LOG.debug("Slave mount point does not have schemas ready yet, delaying registration");
143             return;
144         }
145
146         Preconditions.checkNotNull(id);
147         Preconditions.checkNotNull(remoteSchemaContext, "Device has no remote schema context yet. Probably not fully connected.");
148         Preconditions.checkNotNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
149         this.actorSystem = actorSystem;
150         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
151
152         final ProxyNetconfDeviceDataBroker masterDataBroker = TypedActor.get(actorSystem).typedActorOf(new TypedProps<>(ProxyNetconfDeviceDataBroker.class, NetconfDeviceMasterDataBroker.class), masterRef);
153         LOG.warn("Creating slave data broker for device {}", id);
154         final DOMDataBroker deviceDataBroker = new NetconfDeviceSlaveDataBroker(actorSystem, id, masterDataBroker);
155         salProvider.getMountInstance().onTopologyDeviceConnected(remoteSchemaContext, deviceDataBroker, deviceRpc, notificationService);
156     }
157
158     public void unregisterMountPoint() {
159         salProvider.getMountInstance().onTopologyDeviceDisconnected();
160         if (deviceDataBroker != null) {
161             LOG.debug("Stopping master data broker for device {}", id.getName());
162             for (final Member member : Cluster.get(actorSystem).state().getMembers()) {
163                 if (member.address().equals(Cluster.get(actorSystem).selfAddress())) {
164                     continue;
165                 }
166                 actorSystem.actorSelection(member.address() + "/user/" + topologyId + "/" + id.getName()).tell(new AnnounceMasterMountPointDown(), null);
167             }
168             TypedActor.get(actorSystem).stop(deviceDataBroker);
169             deviceDataBroker = null;
170         }
171     }
172
173     public ConnectionStatusListenerRegistration registerConnectionStatusListener(final RemoteDeviceHandler<NetconfSessionPreferences> listener) {
174         connectionStatusListeners.add(listener);
175         return new ConnectionStatusListenerRegistration(listener);
176     }
177
178     @Override
179     public void close() {
180         closeGracefully(salProvider);
181     }
182
183     private void closeGracefully(final AutoCloseable resource) {
184         if (resource != null) {
185             try {
186                 resource.close();
187             } catch (final Exception e) {
188                 LOG.warn("{}: Ignoring exception while closing {}", id, resource, e);
189             }
190         }
191     }
192
193     public class ConnectionStatusListenerRegistration{
194
195         private final RemoteDeviceHandler<NetconfSessionPreferences> listener;
196
197         public ConnectionStatusListenerRegistration(final RemoteDeviceHandler<NetconfSessionPreferences> listener) {
198             this.listener = listener;
199         }
200
201         public void close() {
202             connectionStatusListeners.remove(listener);
203         }
204     }
205 }