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