Rename MountInstance to NetconfDeviceMount
[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.RemoteDeviceServices;
26 import org.opendaylight.netconf.sal.connect.netconf.NetconfDeviceSchema;
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.AbstractNetconfDataTreeService;
30 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceDataBroker;
31 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceMount;
32 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
33 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
34 import org.opendaylight.netconf.topology.spi.NetconfDeviceTopologyAdapter;
35 import org.opendaylight.yangtools.rfc8528.data.api.MountPointContext;
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 RemoteDeviceHandler, AutoCloseable {
43     private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
44
45     private final RemoteDeviceId id;
46     private final Timeout actorResponseWaitTime;
47     private final ActorRef masterActorRef;
48     private final ActorSystem actorSystem;
49     private final NetconfDeviceTopologyAdapter datastoreAdapter;
50     private final NetconfDeviceMount mount;
51     private final boolean lockDatastore;
52
53     private NetconfDeviceSchema currentSchema = null;
54     private NetconfSessionPreferences netconfSessionPreferences = null;
55     private RemoteDeviceServices deviceServices = null;
56     private DOMDataBroker deviceDataBroker = null;
57     private NetconfDataTreeService netconfService = null;
58
59     MasterSalFacade(final RemoteDeviceId id,
60                     final ActorSystem actorSystem,
61                     final ActorRef masterActorRef,
62                     final Timeout actorResponseWaitTime,
63                     final DOMMountPointService mountService,
64                     final DataBroker dataBroker,
65                     final boolean lockDatastore) {
66         this.id = id;
67         mount = new NetconfDeviceMount(mountService, id);
68         this.actorSystem = actorSystem;
69         this.masterActorRef = masterActorRef;
70         this.actorResponseWaitTime = actorResponseWaitTime;
71         this.lockDatastore = lockDatastore;
72
73         datastoreAdapter = new NetconfDeviceTopologyAdapter(dataBroker, id);
74     }
75
76     @Override
77     public void onDeviceConnected(final NetconfDeviceSchema deviceSchema,
78             final NetconfSessionPreferences sessionPreferences, final RemoteDeviceServices services) {
79         currentSchema = requireNonNull(deviceSchema);
80         netconfSessionPreferences = requireNonNull(sessionPreferences);
81         deviceServices = requireNonNull(services);
82         if (services.actions() != null) {
83             LOG.debug("{}: YANG 1.1 actions are supported in clustered netconf topology, DOMActionService exposed for "
84                 + "the device", id);
85         }
86
87         LOG.info("Device {} connected - registering master mount point", id);
88
89         registerMasterMountPoint();
90
91         sendInitialDataToActor().onComplete(new OnComplete<>() {
92             @Override
93             public void onComplete(final Throwable failure, final Object success) {
94                 if (failure == null) {
95                     updateDeviceData();
96                     return;
97                 }
98
99                 LOG.error("{}: CreateInitialMasterActorData to {} failed", id, masterActorRef, failure);
100             }
101         }, actorSystem.dispatcher());
102     }
103
104     @Override
105     public void onDeviceDisconnected() {
106         LOG.info("Device {} disconnected - unregistering master mount point", id);
107         datastoreAdapter.updateDeviceData(false, NetconfDeviceCapabilities.empty());
108         mount.onDeviceDisconnected();
109     }
110
111     @Override
112     public void onDeviceFailed(final Throwable throwable) {
113         datastoreAdapter.setDeviceAsFailed(throwable);
114         mount.onDeviceDisconnected();
115     }
116
117     @Override
118     public void onNotification(final DOMNotification domNotification) {
119         mount.publish(domNotification);
120     }
121
122     @Override
123     public void close() {
124         datastoreAdapter.close();
125         mount.close();
126     }
127
128     private void registerMasterMountPoint() {
129         requireNonNull(id);
130
131         final var mountContext = requireNonNull(currentSchema,
132             "Device has no remote schema context yet. Probably not fully connected.")
133             .mountContext();
134         final var preferences = requireNonNull(netconfSessionPreferences,
135             "Device has no capabilities yet. Probably not fully connected.");
136
137         deviceDataBroker = newDeviceDataBroker(mountContext, preferences);
138         netconfService = newNetconfDataTreeService(mountContext, preferences);
139
140         // We need to create ProxyDOMDataBroker so accessing mountpoint
141         // on leader node would be same as on follower node
142         final ProxyDOMDataBroker proxyDataBroker = new ProxyDOMDataBroker(id, masterActorRef, actorSystem.dispatcher(),
143             actorResponseWaitTime);
144         final NetconfDataTreeService proxyNetconfService = new ProxyNetconfDataTreeService(id, masterActorRef,
145             actorSystem.dispatcher(), actorResponseWaitTime);
146         mount.onDeviceConnected(mountContext.getEffectiveModelContext(), deviceServices,
147             proxyDataBroker, proxyNetconfService);
148     }
149
150     protected DOMDataBroker newDeviceDataBroker(final MountPointContext mountContext,
151             final NetconfSessionPreferences preferences) {
152         return new NetconfDeviceDataBroker(id, mountContext, deviceServices.rpcs(), preferences, lockDatastore);
153     }
154
155     protected NetconfDataTreeService newNetconfDataTreeService(final MountPointContext mountContext,
156             final NetconfSessionPreferences preferences) {
157         return AbstractNetconfDataTreeService.of(id, mountContext, deviceServices.rpcs(), preferences, lockDatastore);
158     }
159
160     private Future<Object> sendInitialDataToActor() {
161         final List<SourceIdentifier> sourceIdentifiers = List.copyOf(SchemaContextUtil.getConstituentModuleIdentifiers(
162             currentSchema.mountContext().getEffectiveModelContext()));
163
164         LOG.debug("{}: Sending CreateInitialMasterActorData with sourceIdentifiers {} to {}", id, sourceIdentifiers,
165             masterActorRef);
166
167         // send initial data to master actor
168         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, netconfService,
169             sourceIdentifiers, deviceServices), actorResponseWaitTime);
170     }
171
172     private void updateDeviceData() {
173         final String masterAddress = Cluster.get(actorSystem).selfAddress().toString();
174         LOG.debug("{}: updateDeviceData with master address {}", id, masterAddress);
175         datastoreAdapter.updateClusteredDeviceData(true, masterAddress, currentSchema.capabilities());
176     }
177 }