Bump upstream versions
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / MasterSalFacade.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.netconf.topology.singleton.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.cluster.Cluster;
15 import akka.dispatch.OnComplete;
16 import akka.pattern.Patterns;
17 import akka.util.Timeout;
18 import java.util.List;
19 import org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.dom.api.DOMActionService;
21 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
22 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
23 import org.opendaylight.mdsal.dom.api.DOMNotification;
24 import org.opendaylight.mdsal.dom.api.DOMRpcService;
25 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
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.AbstractNetconfDataTreeService;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceDataBroker;
31 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceNotificationService;
32 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalProvider;
33 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
34 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
35 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
36 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import scala.concurrent.Future;
41
42 class MasterSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
43
44     private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
45
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;
51
52     private MountPointContext currentMountContext = null;
53     private NetconfSessionPreferences netconfSessionPreferences = null;
54     private DOMRpcService deviceRpc = null;
55     private DOMDataBroker deviceDataBroker = null;
56     private NetconfDataTreeService netconfService = null;
57     private DOMActionService deviceAction = null;
58
59     MasterSalFacade(final RemoteDeviceId id,
60                     final ActorSystem actorSystem,
61                     final ActorRef masterActorRef,
62                     final Timeout actorResponseWaitTime,
63                     final DOMMountPointService mountService,
64                     final DataBroker dataBroker) {
65         this.id = id;
66         salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
67         this.actorSystem = actorSystem;
68         this.masterActorRef = masterActorRef;
69         this.actorResponseWaitTime = actorResponseWaitTime;
70     }
71
72     @Override
73     public void onDeviceConnected(final MountPointContext mountContext,
74                                   final NetconfSessionPreferences sessionPreferences,
75                                   final DOMRpcService domRpcService, final DOMActionService domActionService) {
76         deviceAction = domActionService;
77         LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, "
78             + "DOMActionService exposed for the device", id);
79         onDeviceConnected(mountContext, sessionPreferences, domRpcService);
80     }
81
82     @Override
83     public void onDeviceConnected(final MountPointContext mountContext,
84                                   final NetconfSessionPreferences sessionPreferences,
85                                   final DOMRpcService domRpcService) {
86         currentMountContext = mountContext;
87         netconfSessionPreferences = sessionPreferences;
88         deviceRpc = domRpcService;
89
90         LOG.info("Device {} connected - registering master mount point", id);
91
92         registerMasterMountPoint();
93
94         sendInitialDataToActor().onComplete(new OnComplete<>() {
95             @Override
96             public void onComplete(final Throwable failure, final Object success) {
97                 if (failure == null) {
98                     updateDeviceData();
99                     return;
100                 }
101
102                 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
103             }
104         }, actorSystem.dispatcher());
105
106     }
107
108     @Override
109     public void onDeviceDisconnected() {
110         LOG.info("Device {} disconnected - unregistering master mount point", id);
111         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
112         unregisterMasterMountPoint();
113     }
114
115     @Override
116     public void onDeviceFailed(final Throwable throwable) {
117         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
118         unregisterMasterMountPoint();
119     }
120
121     @Override
122     public void onNotification(final DOMNotification domNotification) {
123         salProvider.getMountInstance().publish(domNotification);
124     }
125
126     @Override
127     public void close() {
128         unregisterMasterMountPoint();
129         closeGracefully(salProvider);
130     }
131
132     private void registerMasterMountPoint() {
133         requireNonNull(id);
134         requireNonNull(currentMountContext, "Device has no remote schema context yet. Probably not fully connected.");
135         requireNonNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
136
137         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
138         deviceDataBroker = newDeviceDataBroker();
139         netconfService = newNetconfDataTreeService();
140
141         // We need to create ProxyDOMDataBroker so accessing mountpoint
142         // on leader node would be same as on follower node
143         final ProxyDOMDataBroker proxyDataBroker = new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(),
144             actorResponseWaitTime);
145         final NetconfDataTreeService proxyNetconfService = new ProxyNetconfDataTreeService(id, masterActorRef,
146             actorSystem.dispatcher(), actorResponseWaitTime);
147         salProvider.getMountInstance().onTopologyDeviceConnected(currentMountContext.getEffectiveModelContext(),
148             proxyDataBroker, proxyNetconfService, deviceRpc, notificationService, deviceAction);
149     }
150
151     protected DOMDataBroker newDeviceDataBroker() {
152         return new NetconfDeviceDataBroker(id, currentMountContext, deviceRpc, netconfSessionPreferences);
153     }
154
155     protected NetconfDataTreeService newNetconfDataTreeService() {
156         return AbstractNetconfDataTreeService.of(id, currentMountContext, deviceRpc, netconfSessionPreferences);
157     }
158
159     private Future<Object> sendInitialDataToActor() {
160         final List<SourceIdentifier> sourceIdentifiers = List.copyOf(SchemaContextUtil.getConstituentModuleIdentifiers(
161             currentMountContext.getEffectiveModelContext()));
162
163         LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
164             masterActorRef);
165
166         // send initial data to master actor
167         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, netconfService,
168             sourceIdentifiers, deviceRpc, deviceAction), actorResponseWaitTime);
169     }
170
171     private void updateDeviceData() {
172         final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
173         LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
174         salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
175                 netconfSessionPreferences.getNetconfDeviceCapabilities());
176     }
177
178     private void unregisterMasterMountPoint() {
179         salProvider.getMountInstance().onTopologyDeviceDisconnected();
180     }
181
182     @SuppressWarnings("checkstyle:IllegalCatch")
183     private void closeGracefully(final AutoCloseable resource) {
184         if (resource != null) {
185             try {
186                 resource.close();
187             } catch (final Exception e) {
188                 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);
189             }
190         }
191     }
192 }