Convert to using requireNonNull()
[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.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.NetconfDeviceDataBroker;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceNotificationService;
31 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalProvider;
32 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
33 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
34 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
35 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
36 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
37 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
38 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import scala.concurrent.Future;
42
43 class MasterSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
44
45     private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
46
47     private final RemoteDeviceId id;
48     private final Timeout actorResponseWaitTime;
49     private final NetconfDeviceSalProvider salProvider;
50     private final ActorRef masterActorRef;
51     private final ActorSystem actorSystem;
52
53     private SchemaContext currentSchemaContext = null;
54     private NetconfSessionPreferences netconfSessionPreferences = null;
55     private DOMRpcService deviceRpc = null;
56     private DOMDataBroker deviceDataBroker = 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         this.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         this.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         this.currentSchemaContext = mountContext.getSchemaContext();
87         this.netconfSessionPreferences = sessionPreferences;
88         this.deviceRpc = domRpcService;
89
90         LOG.info("Device {} connected - registering master mount point", id);
91
92         registerMasterMountPoint();
93
94         sendInitialDataToActor().onComplete(new OnComplete<Object>() {
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(currentSchemaContext, "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
140         // We need to create ProxyDOMDataBroker so accessing mountpoint
141         // on leader node would be same as on follower node
142         final ProxyDOMDataBroker proxyDataBroker = new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(),
143             actorResponseWaitTime);
144         salProvider.getMountInstance().onTopologyDeviceConnected(currentSchemaContext,
145             proxyDataBroker, deviceRpc, notificationService, deviceAction);
146     }
147
148     protected DOMDataBroker newDeviceDataBroker() {
149         return new NetconfDeviceDataBroker(id, currentSchemaContext, deviceRpc, netconfSessionPreferences);
150     }
151
152     private Future<Object> sendInitialDataToActor() {
153         final List<SourceIdentifier> sourceIdentifiers =
154                 SchemaContextUtil.getConstituentModuleIdentifiers(currentSchemaContext).stream()
155                 .map(mi -> RevisionSourceIdentifier.create(mi.getName(), mi.getRevision()))
156                 .collect(Collectors.toList());
157
158         LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
159             masterActorRef);
160
161         // send initial data to master actor
162         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
163                 deviceRpc, deviceAction), actorResponseWaitTime);
164     }
165
166     private void updateDeviceData() {
167         final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
168         LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
169         salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
170                 netconfSessionPreferences.getNetconfDeviceCapabilities());
171     }
172
173     private void unregisterMasterMountPoint() {
174         salProvider.getMountInstance().onTopologyDeviceDisconnected();
175     }
176
177     @SuppressWarnings("checkstyle:IllegalCatch")
178     private void closeGracefully(final AutoCloseable resource) {
179         if (resource != null) {
180             try {
181                 resource.close();
182             } catch (final Exception e) {
183                 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);
184             }
185         }
186     }
187 }