36ab07bbea40f111e952b4a3bca7ac23eab352e8
[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 com.google.common.base.Optional;
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.dom.api.DOMDataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
22 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
23 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
24 import org.opendaylight.controller.sal.core.api.Broker;
25 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceHandler;
26 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfDeviceCapabilities;
27 import org.opendaylight.netconf.sal.connect.netconf.listener.NetconfSessionPreferences;
28 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceNotificationService;
29 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceSalProvider;
30 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
31 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
32 import org.opendaylight.netconf.topology.singleton.impl.tx.NetconfMasterDOMTransaction;
33 import org.opendaylight.netconf.topology.singleton.impl.tx.NetconfProxyDOMTransaction;
34 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
35 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
36 import org.opendaylight.yangtools.yang.common.SimpleDateFormatUtil;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
38 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
39 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import scala.concurrent.Future;
43
44 class MasterSalFacade implements AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
45
46     private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
47
48     private final RemoteDeviceId id;
49
50     private SchemaContext remoteSchemaContext = null;
51     private NetconfSessionPreferences netconfSessionPreferences = null;
52     private DOMRpcService deviceRpc = null;
53     private final NetconfDeviceSalProvider salProvider;
54
55     private final ActorRef masterActorRef;
56     private final ActorSystem actorSystem;
57     private DOMDataBroker deviceDataBroker = null;
58
59     MasterSalFacade(final RemoteDeviceId id,
60                            final Broker domBroker,
61                            final BindingAwareBroker bindingBroker,
62                            final ActorSystem actorSystem,
63                            final ActorRef masterActorRef) {
64         this.id = id;
65         this.salProvider = new NetconfDeviceSalProvider(id);
66         this.actorSystem = actorSystem;
67         this.masterActorRef = masterActorRef;
68
69         registerToSal(domBroker, bindingBroker);
70     }
71
72     private void registerToSal(final Broker domRegistryDependency, final BindingAwareBroker bindingBroker) {
73         // TODO: remove use of provider, there is possible directly create mount instance and
74         // TODO: NetconfDeviceTopologyAdapter in constructor = less complexity
75
76         domRegistryDependency.registerProvider(salProvider);
77         bindingBroker.registerProvider(salProvider);
78     }
79
80     @Override
81     public void onDeviceConnected(final SchemaContext remoteSchemaContext,
82                                   final NetconfSessionPreferences netconfSessionPreferences,
83                                   final DOMRpcService deviceRpc) {
84         this.remoteSchemaContext = remoteSchemaContext;
85         this.netconfSessionPreferences = netconfSessionPreferences;
86         this.deviceRpc = deviceRpc;
87
88         registerMasterMountPoint();
89
90         sendInitialDataToActor().onComplete(new OnComplete<Object>() {
91             @Override
92             public void onComplete(final Throwable failure, final Object success) throws Throwable {
93                 if (failure == null) {
94                     updateDeviceData();
95                     return;
96                 }
97                 throw failure;
98             }
99         }, actorSystem.dispatcher());
100
101     }
102
103     @Override
104     public void onDeviceDisconnected() {
105         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
106         unregisterMasterMountPoint();
107     }
108
109     @Override
110     public void onDeviceFailed(final Throwable throwable) {
111         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
112         unregisterMasterMountPoint();
113     }
114
115     @Override
116     public void onNotification(final DOMNotification domNotification) {
117         salProvider.getMountInstance().publish(domNotification);
118     }
119
120     @Override
121     public void close() {
122         unregisterMasterMountPoint();
123         closeGracefully(salProvider);
124     }
125
126     private void registerMasterMountPoint() {
127         Preconditions.checkNotNull(id);
128         Preconditions.checkNotNull(remoteSchemaContext,
129                 "Device has no remote schema context yet. Probably not fully connected.");
130         Preconditions.checkNotNull(netconfSessionPreferences,
131                 "Device has no capabilities yet. Probably not fully connected.");
132
133         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
134
135         LOG.info("{}: Creating master data broker for device", id);
136
137         final NetconfDOMTransaction masterDOMTransactions =
138                 new NetconfMasterDOMTransaction(id, remoteSchemaContext, deviceRpc, netconfSessionPreferences);
139         deviceDataBroker =
140                 new NetconfDOMDataBroker(actorSystem, id, masterDOMTransactions);
141         // We need to create NetconfProxyDOMTransaction so accessing mountpoint
142         // on leader node would be same as on follower node
143         final NetconfDOMTransaction proxyDOMTransation =
144                 new NetconfProxyDOMTransaction(id, actorSystem, masterActorRef);
145         final NetconfDOMDataBroker proxyDataBroker = new NetconfDOMDataBroker(actorSystem, id, proxyDOMTransation);
146         salProvider.getMountInstance()
147                 .onTopologyDeviceConnected(remoteSchemaContext, proxyDataBroker, deviceRpc, notificationService);
148     }
149
150     private Future<Object> sendInitialDataToActor() {
151         final List<SourceIdentifier> sourceIdentifiers =
152                 remoteSchemaContext.getAllModuleIdentifiers().stream().map(mi ->
153                         RevisionSourceIdentifier.create(mi.getName(),
154                             (SimpleDateFormatUtil.DEFAULT_DATE_REV == mi.getRevision() ? Optional.<String>absent() :
155                                     Optional.of(SimpleDateFormatUtil.getRevisionFormat().format(mi.getRevision())))))
156                         .collect(Collectors.toList());
157
158         // send initial data to master actor and create actor for providing it
159         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
160                         deviceRpc), NetconfTopologyUtils.TIMEOUT);
161     }
162
163     private void updateDeviceData() {
164         Cluster cluster = Cluster.get(actorSystem);
165         salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, cluster.selfAddress().toString(),
166                 netconfSessionPreferences.getNetconfDeviceCapabilities());
167     }
168
169     private void unregisterMasterMountPoint() {
170         salProvider.getMountInstance().onTopologyDeviceDisconnected();
171     }
172
173     private void closeGracefully(final AutoCloseable resource) {
174         if (resource != null) {
175             try {
176                 resource.close();
177             } catch (final Exception e) {
178                 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);
179             }
180         }
181     }
182
183 }