f40cc264ca8206c749bd3a0d28c1831127be88ab
[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 org.opendaylight.mdsal.binding.api.DataBroker;
20 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
21 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
22 import org.opendaylight.mdsal.dom.api.DOMNotification;
23 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
24 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
25 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceId;
26 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceServices;
27 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
28 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
29 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.AbstractNetconfDataTreeService;
31 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceDataBroker;
32 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceMount;
33 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
34 import org.opendaylight.netconf.topology.spi.AbstractNetconfTopology;
35 import org.opendaylight.netconf.topology.spi.NetconfDeviceTopologyAdapter;
36 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
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 RemoteDeviceHandler, AutoCloseable {
44     private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
45
46     private final RemoteDeviceId id;
47     private final Timeout actorResponseWaitTime;
48     private final ActorRef masterActorRef;
49     private final ActorSystem actorSystem;
50     private final NetconfDeviceTopologyAdapter datastoreAdapter;
51     private final NetconfDeviceMount mount;
52     private final boolean lockDatastore;
53
54     private NetconfDeviceSchema currentSchema = null;
55     private NetconfSessionPreferences netconfSessionPreferences = null;
56     private RemoteDeviceServices deviceServices = null;
57     private DOMDataBroker deviceDataBroker = null;
58     private NetconfDataTreeService netconfService = null;
59
60     MasterSalFacade(final RemoteDeviceId id,
61                     final ActorSystem actorSystem,
62                     final ActorRef masterActorRef,
63                     final Timeout actorResponseWaitTime,
64                     final DOMMountPointService mountService,
65                     final DataBroker dataBroker,
66                     final boolean lockDatastore) {
67         this.id = id;
68         mount = new NetconfDeviceMount(id, mountService, NetconfDeviceMount.defaultTopologyMountPath(id));
69         this.actorSystem = actorSystem;
70         this.masterActorRef = masterActorRef;
71         this.actorResponseWaitTime = actorResponseWaitTime;
72         this.lockDatastore = lockDatastore;
73
74         datastoreAdapter = new NetconfDeviceTopologyAdapter(dataBroker, AbstractNetconfTopology.DEFAULT_TOPOLOGY_IID,
75             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());
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         datastoreAdapter.close();
127         mount.close();
128     }
129
130     private void registerMasterMountPoint() {
131         requireNonNull(id);
132
133         final var mountContext = requireNonNull(currentSchema,
134             "Device has no remote schema context yet. Probably not fully connected.")
135             .mountContext();
136         final var preferences = requireNonNull(netconfSessionPreferences,
137             "Device has no capabilities yet. Probably not fully connected.");
138
139         deviceDataBroker = newDeviceDataBroker(mountContext, preferences);
140         netconfService = newNetconfDataTreeService(mountContext, preferences);
141
142         // We need to create ProxyDOMDataBroker so accessing mountpoint
143         // on leader node would be same as on follower node
144         final ProxyDOMDataBroker proxyDataBroker = new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(),
145             actorResponseWaitTime);
146         final NetconfDataTreeService proxyNetconfService = new ProxyNetconfDataTreeService(id, masterActorRef,
147             actorSystem.dispatcher(), actorResponseWaitTime);
148         mount.onDeviceConnected(mountContext.getEffectiveModelContext(), deviceServices,
149             proxyDataBroker, proxyNetconfService);
150     }
151
152     protected DOMDataBroker newDeviceDataBroker(final MountPointContext mountContext,
153             final NetconfSessionPreferences preferences) {
154         return new NetconfDeviceDataBroker(id, mountContext, deviceServices.rpcs(), preferences, lockDatastore);
155     }
156
157     protected NetconfDataTreeService newNetconfDataTreeService(final MountPointContext mountContext,
158             final NetconfSessionPreferences preferences) {
159         return AbstractNetconfDataTreeService.of(id, mountContext, deviceServices.rpcs(), preferences, lockDatastore);
160     }
161
162     private Future<Object> sendInitialDataToActor() {
163         final List<SourceIdentifier> sourceIdentifiers = List.copyOf(SchemaContextUtil.getConstituentModuleIdentifiers(
164             currentSchema.mountContext().getEffectiveModelContext()));
165
166         LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
167             masterActorRef);
168
169         // send initial data to master actor
170         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, netconfService,
171             sourceIdentifiers, deviceServices), actorResponseWaitTime);
172     }
173
174     private void updateDeviceData() {
175         final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
176         LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
177         datastoreAdapter.updateClusteredDeviceData(true, masterAddress, currentSchema.capabilities());
178     }
179 }