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