Bump odlparent to 6.0.0
[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 edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19 import java.util.List;
20 import java.util.stream.Collectors;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.mdsal.dom.api.DOMActionService;
23 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
24 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
25 import org.opendaylight.mdsal.dom.api.DOMNotification;
26 import org.opendaylight.mdsal.dom.api.DOMRpcService;
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.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.api.SchemaContext;
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 SchemaContext currentSchemaContext = null;
55     private NetconfSessionPreferences netconfSessionPreferences = null;
56     private DOMRpcService deviceRpc = null;
57     private DOMDataBroker deviceDataBroker = null;
58     private DOMActionService deviceAction = null;
59
60     MasterSalFacade(final RemoteDeviceId id,
61                     final ActorSystem actorSystem,
62                     final ActorRef masterActorRef,
63                     final Timeout actorResponseWaitTime,
64                     final DOMMountPointService mountService,
65                     final DataBroker dataBroker) {
66         this.id = id;
67         this.salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
68         this.actorSystem = actorSystem;
69         this.masterActorRef = masterActorRef;
70         this.actorResponseWaitTime = actorResponseWaitTime;
71     }
72
73     @Override
74     public void onDeviceConnected(final MountPointContext mountContext,
75                                   final NetconfSessionPreferences sessionPreferences,
76                                   final DOMRpcService domRpcService, final DOMActionService domActionService) {
77         this.deviceAction = domActionService;
78         LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, "
79             + "DOMActionService exposed for the device", id);
80         onDeviceConnected(mountContext, sessionPreferences, domRpcService);
81     }
82
83     @Override
84     public void onDeviceConnected(final MountPointContext mountContext,
85                                   final NetconfSessionPreferences sessionPreferences,
86                                   final DOMRpcService domRpcService) {
87         this.currentSchemaContext = mountContext.getSchemaContext();
88         this.netconfSessionPreferences = sessionPreferences;
89         this.deviceRpc = domRpcService;
90
91         LOG.info("Device {} connected - registering master mount point", id);
92
93         registerMasterMountPoint();
94
95         sendInitialDataToActor().onComplete(new OnComplete<Object>() {
96             @Override
97             public void onComplete(final Throwable failure, final Object success) {
98                 if (failure == null) {
99                     updateDeviceData();
100                     return;
101                 }
102
103                 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
104             }
105         }, actorSystem.dispatcher());
106
107     }
108
109     @Override
110     public void onDeviceDisconnected() {
111         LOG.info("Device {} disconnected - unregistering master mount point", id);
112         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
113         unregisterMasterMountPoint();
114     }
115
116     @Override
117     public void onDeviceFailed(final Throwable throwable) {
118         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
119         unregisterMasterMountPoint();
120     }
121
122     @Override
123     public void onNotification(final DOMNotification domNotification) {
124         salProvider.getMountInstance().publish(domNotification);
125     }
126
127     @Override
128     public void close() {
129         unregisterMasterMountPoint();
130         closeGracefully(salProvider);
131     }
132
133     private void registerMasterMountPoint() {
134         requireNonNull(id);
135         requireNonNull(currentSchemaContext, "Device has no remote schema context yet. Probably not fully connected.");
136         requireNonNull(netconfSessionPreferences, "Device has no capabilities yet. Probably not fully connected.");
137
138         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
139         deviceDataBroker = newDeviceDataBroker();
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         salProvider.getMountInstance().onTopologyDeviceConnected(currentSchemaContext,
146             proxyDataBroker, deviceRpc, notificationService, deviceAction);
147     }
148
149     protected DOMDataBroker newDeviceDataBroker() {
150         return new NetconfDeviceDataBroker(id, currentSchemaContext, deviceRpc, netconfSessionPreferences);
151     }
152
153     private Future<Object> sendInitialDataToActor() {
154         final List<SourceIdentifier> sourceIdentifiers =
155                 SchemaContextUtil.getConstituentModuleIdentifiers(currentSchemaContext).stream()
156                 .map(mi -> RevisionSourceIdentifier.create(mi.getName(), mi.getRevision()))
157                 .collect(Collectors.toList());
158
159         LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
160             masterActorRef);
161
162         // send initial data to master actor
163         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
164                 deviceRpc, deviceAction), actorResponseWaitTime);
165     }
166
167     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
168             justification = "https://github.com/spotbugs/spotbugs/issues/811")
169     private void updateDeviceData() {
170         final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
171         LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
172         salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
173                 netconfSessionPreferences.getNetconfDeviceCapabilities());
174     }
175
176     private void unregisterMasterMountPoint() {
177         salProvider.getMountInstance().onTopologyDeviceDisconnected();
178     }
179
180     @SuppressWarnings("checkstyle:IllegalCatch")
181     private void closeGracefully(final AutoCloseable resource) {
182         if (resource != null) {
183             try {
184                 resource.close();
185             } catch (final Exception e) {
186                 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);
187             }
188         }
189     }
190 }