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