Teach NETCONF about YANG 1.1 actions in cluster topology
[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.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
35 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
36 import org.opendaylight.yangtools.yang.model.util.SchemaContextUtil;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import scala.concurrent.Future;
40
41 class MasterSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
44
45     private final RemoteDeviceId id;
46     private final Timeout actorResponseWaitTime;
47     private final NetconfDeviceSalProvider salProvider;
48     private final ActorRef masterActorRef;
49     private final ActorSystem actorSystem;
50
51     private SchemaContext currentSchemaContext = null;
52     private NetconfSessionPreferences netconfSessionPreferences = null;
53     private DOMRpcService deviceRpc = null;
54     private DOMDataBroker deviceDataBroker = null;
55     private DOMActionService deviceAction = null;
56
57     MasterSalFacade(final RemoteDeviceId id,
58                     final ActorSystem actorSystem,
59                     final ActorRef masterActorRef,
60                     final Timeout actorResponseWaitTime,
61                     final DOMMountPointService mountService,
62                     final DataBroker dataBroker) {
63         this.id = id;
64         this.salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
65         this.actorSystem = actorSystem;
66         this.masterActorRef = masterActorRef;
67         this.actorResponseWaitTime = actorResponseWaitTime;
68     }
69
70     @Override
71     public void onDeviceConnected(final SchemaContext remoteSchemaContext,
72                                   final NetconfSessionPreferences sessionPreferences,
73                                   final DOMRpcService domRpcService, final DOMActionService domActionService) {
74         this.deviceAction = domActionService;
75         LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, "
76             + "DOMActionService exposed for the device", id);
77         onDeviceConnected(remoteSchemaContext, sessionPreferences, domRpcService);
78     }
79
80     @Override
81     public void onDeviceConnected(final SchemaContext remoteSchemaContext,
82                                   final NetconfSessionPreferences sessionPreferences,
83                                   final DOMRpcService domRpcService) {
84         this.currentSchemaContext = remoteSchemaContext;
85         this.netconfSessionPreferences = sessionPreferences;
86         this.deviceRpc = domRpcService;
87
88         LOG.info("Device {} connected - registering master mount point", id);
89
90         registerMasterMountPoint();
91
92         sendInitialDataToActor().onComplete(new OnComplete<Object>() {
93             @Override
94             public void onComplete(final Throwable failure, final Object success) {
95                 if (failure == null) {
96                     updateDeviceData();
97                     return;
98                 }
99
100                 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
101             }
102         }, actorSystem.dispatcher());
103
104     }
105
106     @Override
107     public void onDeviceDisconnected() {
108         LOG.info("Device {} disconnected - unregistering master mount point", id);
109         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
110         unregisterMasterMountPoint();
111     }
112
113     @Override
114     public void onDeviceFailed(final Throwable throwable) {
115         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
116         unregisterMasterMountPoint();
117     }
118
119     @Override
120     public void onNotification(final DOMNotification domNotification) {
121         salProvider.getMountInstance().publish(domNotification);
122     }
123
124     @Override
125     public void close() {
126         unregisterMasterMountPoint();
127         closeGracefully(salProvider);
128     }
129
130     private void registerMasterMountPoint() {
131         Preconditions.checkNotNull(id);
132         Preconditions.checkNotNull(currentSchemaContext,
133                 "Device has no remote schema context yet. Probably not fully connected.");
134         Preconditions.checkNotNull(netconfSessionPreferences,
135                 "Device has no capabilities yet. Probably not fully connected.");
136
137         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
138
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 =
144                 new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(), actorResponseWaitTime);
145         salProvider.getMountInstance().onTopologyDeviceConnected(currentSchemaContext, proxyDataBroker, deviceRpc,
146             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 {}",
160                 id, sourceIdentifiers, 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
189 }