8e13f630d8fc267a34ce5af2786ff56871f8e7b0
[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
9 package org.opendaylight.netconf.topology.singleton.impl;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.cluster.Cluster;
14 import akka.dispatch.OnComplete;
15 import akka.pattern.Patterns;
16 import akka.util.Timeout;
17 import com.google.common.base.Preconditions;
18 import java.util.List;
19 import java.util.stream.Collectors;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMActionService;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
23 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
24 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
25 import org.opendaylight.controller.md.sal.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.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
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         // We do not support actions in clustered topology yet
75         if (domActionService != null) {
76             LOG.warn("{}: YANG 1.1 actions are not supported in clustered netconf topology, "
77                     + "DOMActionService will not be exposed for the device");
78         }
79
80         onDeviceConnected(remoteSchemaContext, sessionPreferences, domRpcService);
81     }
82
83     @Override
84     public void onDeviceConnected(final SchemaContext remoteSchemaContext,
85                                   final NetconfSessionPreferences sessionPreferences,
86                                   final DOMRpcService domRpcService) {
87         this.currentSchemaContext = remoteSchemaContext;
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         Preconditions.checkNotNull(id);
135         Preconditions.checkNotNull(currentSchemaContext,
136                 "Device has no remote schema context yet. Probably not fully connected.");
137         Preconditions.checkNotNull(netconfSessionPreferences,
138                 "Device has no capabilities yet. Probably not fully connected.");
139
140         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
141
142         deviceDataBroker = newDeviceDataBroker();
143
144         // We need to create ProxyDOMDataBroker so accessing mountpoint
145         // on leader node would be same as on follower node
146         final ProxyDOMDataBroker proxyDataBroker =
147                 new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(), actorResponseWaitTime);
148         salProvider.getMountInstance()
149                 .onTopologyDeviceConnected(currentSchemaContext, proxyDataBroker, deviceRpc, notificationService);
150     }
151
152     protected DOMDataBroker newDeviceDataBroker() {
153         return new NetconfDeviceDataBroker(id, currentSchemaContext, deviceRpc, netconfSessionPreferences);
154     }
155
156     private Future<Object> sendInitialDataToActor() {
157         final List<SourceIdentifier> sourceIdentifiers =
158                 SchemaContextUtil.getConstituentModuleIdentifiers(currentSchemaContext).stream()
159                 .map(mi -> RevisionSourceIdentifier.create(mi.getName(), mi.getRevision()))
160                 .collect(Collectors.toList());
161
162         LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}",
163                 id, sourceIdentifiers, masterActorRef);
164
165         // send initial data to master actor
166         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
167                 deviceRpc), actorResponseWaitTime);
168     }
169
170     private void updateDeviceData() {
171         final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
172         LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
173         salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, masterAddress,
174                 netconfSessionPreferences.getNetconfDeviceCapabilities());
175     }
176
177     private void unregisterMasterMountPoint() {
178         salProvider.getMountInstance().onTopologyDeviceDisconnected();
179     }
180
181     @SuppressWarnings("checkstyle:IllegalCatch")
182     private void closeGracefully(final AutoCloseable resource) {
183         if (resource != null) {
184             try {
185                 resource.close();
186             } catch (final Exception e) {
187                 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);
188             }
189         }
190     }
191
192 }