ee2d3ee0b1341a869d132b57ccf63295c8bc6418
[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 akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.cluster.Cluster;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.Patterns;
15 import akka.util.Timeout;
16 import com.google.common.base.Preconditions;
17 import java.util.List;
18 import java.util.stream.Collectors;
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.sal.connect.api.RemoteDeviceHandler;
26 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
27 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
28 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceDataBroker;
29 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceNotificationService;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalProvider;
31 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
32 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
33 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
34 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
35 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
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 SchemaContext currentSchemaContext = null;
53     private NetconfSessionPreferences netconfSessionPreferences = null;
54     private DOMRpcService deviceRpc = null;
55     private DOMDataBroker deviceDataBroker = null;
56     private DOMActionService deviceAction = null;
57
58     MasterSalFacade(final RemoteDeviceId id,
59                     final ActorSystem actorSystem,
60                     final ActorRef masterActorRef,
61                     final Timeout actorResponseWaitTime,
62                     final DOMMountPointService mountService,
63                     final DataBroker dataBroker) {
64         this.id = id;
65         this.salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
66         this.actorSystem = actorSystem;
67         this.masterActorRef = masterActorRef;
68         this.actorResponseWaitTime = actorResponseWaitTime;
69     }
70
71     @Override
72     public void onDeviceConnected(final MountPointContext mountContext,
73                                   final NetconfSessionPreferences sessionPreferences,
74                                   final DOMRpcService domRpcService, final DOMActionService domActionService) {
75         this.deviceAction = domActionService;
76         LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, "
77             + "DOMActionService exposed for the device", id);
78         onDeviceConnected(mountContext, sessionPreferences, domRpcService);
79     }
80
81     @Override
82     public void onDeviceConnected(final MountPointContext mountContext,
83                                   final NetconfSessionPreferences sessionPreferences,
84                                   final DOMRpcService domRpcService) {
85         this.currentSchemaContext = mountContext.getSchemaContext();
86         this.netconfSessionPreferences = sessionPreferences;
87         this.deviceRpc = domRpcService;
88
89         LOG.info("Device {} connected - registering master mount point", id);
90
91         registerMasterMountPoint();
92
93         sendInitialDataToActor().onComplete(new OnComplete<Object>() {
94             @Override
95             public void onComplete(final Throwable failure, final Object success) {
96                 if (failure == null) {
97                     updateDeviceData();
98                     return;
99                 }
100
101                 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
102             }
103         }, actorSystem.dispatcher());
104
105     }
106
107     @Override
108     public void onDeviceDisconnected() {
109         LOG.info("Device {} disconnected - unregistering master mount point", id);
110         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
111         unregisterMasterMountPoint();
112     }
113
114     @Override
115     public void onDeviceFailed(final Throwable throwable) {
116         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
117         unregisterMasterMountPoint();
118     }
119
120     @Override
121     public void onNotification(final DOMNotification domNotification) {
122         salProvider.getMountInstance().publish(domNotification);
123     }
124
125     @Override
126     public void close() {
127         unregisterMasterMountPoint();
128         closeGracefully(salProvider);
129     }
130
131     private void registerMasterMountPoint() {
132         Preconditions.checkNotNull(id);
133         Preconditions.checkNotNull(currentSchemaContext,
134                 "Device has no remote schema context yet. Probably not fully connected.");
135         Preconditions.checkNotNull(netconfSessionPreferences,
136                 "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     private void updateDeviceData() {
168         final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
169         LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
170         salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
171                 netconfSessionPreferences.getNetconfDeviceCapabilities());
172     }
173
174     private void unregisterMasterMountPoint() {
175         salProvider.getMountInstance().onTopologyDeviceDisconnected();
176     }
177
178     @SuppressWarnings("checkstyle:IllegalCatch")
179     private void closeGracefully(final AutoCloseable resource) {
180         if (resource != null) {
181             try {
182                 resource.close();
183             } catch (final Exception e) {
184                 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);
185             }
186         }
187     }
188 }