Bump upstreams
[netconf.git] / apps / 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.concurrent.ExecutionException;
20 import org.opendaylight.mdsal.binding.api.DataBroker;
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.netconf.client.mdsal.NetconfDeviceCapabilities;
25 import org.opendaylight.netconf.client.mdsal.NetconfDeviceSchema;
26 import org.opendaylight.netconf.client.mdsal.api.NetconfSessionPreferences;
27 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceHandler;
28 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
29 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices;
30 import org.opendaylight.netconf.client.mdsal.spi.AbstractNetconfDataTreeService;
31 import org.opendaylight.netconf.client.mdsal.spi.NetconfDeviceDataBroker;
32 import org.opendaylight.netconf.client.mdsal.spi.NetconfDeviceMount;
33 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
34 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
35 import org.opendaylight.netconf.topology.spi.NetconfDeviceTopologyAdapter;
36 import org.opendaylight.netconf.topology.spi.NetconfNodeUtils;
37 import org.opendaylight.yangtools.yang.data.api.schema.MountPointContext;
38 import org.opendaylight.yangtools.yang.model.api.source.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 RemoteDeviceHandler, AutoCloseable {
45     private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
46
47     private final RemoteDeviceId id;
48     private final Timeout actorResponseWaitTime;
49     private final ActorRef masterActorRef;
50     private final ActorSystem actorSystem;
51     private final NetconfDeviceTopologyAdapter datastoreAdapter;
52     private final NetconfDeviceMount mount;
53     private final boolean lockDatastore;
54
55     private NetconfDeviceSchema currentSchema = null;
56     private NetconfSessionPreferences netconfSessionPreferences = null;
57     private RemoteDeviceServices deviceServices = null;
58     private DOMDataBroker deviceDataBroker = null;
59     private NetconfDataTreeService netconfService = null;
60
61     MasterSalFacade(final RemoteDeviceId id,
62                     final ActorSystem actorSystem,
63                     final ActorRef masterActorRef,
64                     final Timeout actorResponseWaitTime,
65                     final DOMMountPointService mountService,
66                     final DataBroker dataBroker,
67                     final boolean lockDatastore) {
68         this.id = id;
69         mount = new NetconfDeviceMount(id, mountService, NetconfNodeUtils.defaultTopologyMountPath(id));
70         this.actorSystem = actorSystem;
71         this.masterActorRef = masterActorRef;
72         this.actorResponseWaitTime = actorResponseWaitTime;
73         this.lockDatastore = lockDatastore;
74
75         datastoreAdapter = new NetconfDeviceTopologyAdapter(dataBroker, NetconfNodeUtils.DEFAULT_TOPOLOGY_IID, id);
76     }
77
78     @Override
79     public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
80             final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
81         currentSchema = requireNonNull(deviceSchema);
82         netconfSessionPreferences = requireNonNull(sessionPreferences);
83         deviceServices = requireNonNull(services);
84         if (services.actions() != null) {
85             LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, DOMActionService exposed for "
86                 + "the device", id);
87         }
88
89         LOG.info("Device {} connected - registering master mount point", id);
90
91         registerMasterMountPoint();
92
93         sendInitialDataToActor().onComplete(new OnComplete<>() {
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     @Override
107     public void onDeviceDisconnected() {
108         LOG.info("Device {} disconnected - unregistering master mount point", id);
109         datastoreAdapter.updateDeviceData(false, NetconfDeviceCapabilities.empty(), null);
110         mount.onDeviceDisconnected();
111     }
112
113     @Override
114     public void onDeviceFailed(final Throwable throwable) {
115         datastoreAdapter.setDeviceAsFailed(throwable);
116         mount.onDeviceDisconnected();
117     }
118
119     @Override
120     public void onNotification(final DOMNotification domNotification) {
121         mount.publish(domNotification);
122     }
123
124     @Override
125     public void close() {
126         final var future = datastoreAdapter.shutdown();
127         mount.close();
128
129         try {
130             future.get();
131         } catch (InterruptedException e) {
132             Thread.currentThread().interrupt();
133             throw new IllegalStateException("Interrupted while waiting for datastore adapter shutdown", e);
134         } catch (ExecutionException e) {
135             throw new IllegalStateException("Datastore adapter shutdown failed", e);
136         }
137     }
138
139     private void registerMasterMountPoint() {
140         requireNonNull(id);
141
142         final var mountContext = requireNonNull(currentSchema,
143             "Device has no remote schema context yet. Probably not fully connected.")
144             .mountContext();
145         final var preferences = requireNonNull(netconfSessionPreferences,
146             "Device has no capabilities yet. Probably not fully connected.");
147
148         deviceDataBroker = newDeviceDataBroker(mountContext, preferences);
149         netconfService = newNetconfDataTreeService(mountContext, preferences);
150
151         // We need to create ProxyDOMDataBroker so accessing mountpoint
152         // on leader node would be same as on follower node
153         final ProxyDOMDataBroker proxyDataBroker = new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(),
154             actorResponseWaitTime);
155         final NetconfDataTreeService proxyNetconfService = new ProxyNetconfDataTreeService(id, masterActorRef,
156             actorSystem.dispatcher(), actorResponseWaitTime);
157         mount.onDeviceConnected(mountContext.modelContext(), deviceServices, proxyDataBroker, proxyNetconfService);
158     }
159
160     protected DOMDataBroker newDeviceDataBroker(final MountPointContext mountContext,
161             final NetconfSessionPreferences preferences) {
162         return new NetconfDeviceDataBroker(id, mountContext, deviceServices.rpcs(), preferences, lockDatastore);
163     }
164
165     protected NetconfDataTreeService newNetconfDataTreeService(final MountPointContext mountContext,
166             final NetconfSessionPreferences preferences) {
167         return AbstractNetconfDataTreeService.of(id, mountContext, deviceServices.rpcs(), preferences, lockDatastore);
168     }
169
170     private Future<Object> sendInitialDataToActor() {
171         final List<SourceIdentifier> sourceIdentifiers = List.copyOf(SchemaContextUtil.getConstituentModuleIdentifiers(
172             currentSchema.mountContext().modelContext()));
173
174         LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
175             masterActorRef);
176
177         // send initial data to master actor
178         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, netconfService,
179             sourceIdentifiers, deviceServices), actorResponseWaitTime);
180     }
181
182     private void updateDeviceData() {
183         final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
184         LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
185         datastoreAdapter.updateClusteredDeviceData(true, masterAddress, currentSchema.capabilities(),
186                 netconfSessionPreferences.sessionId());
187     }
188 }