1ebbad07001604248f0d4778b30669baa973d6a5
[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.Preconditions;
18 import java.util.List;
19 import java.util.stream.Collectors;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
22 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
23 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
24 import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
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.NetconfDeviceDataBroker;
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.messages.CreateInitialMasterActorData;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
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 AutoCloseable, RemoteDeviceHandler<NetconfSessionPreferences> {
42
43     private static final Logger LOG = LoggerFactory.getLogger(MasterSalFacade.class);
44
45     private final RemoteDeviceId id;
46     private final Timeout actorResponseWaitTime;
47     private final NetconfDeviceSalProvider salProvider;
48     private final ActorRef masterActorRef;
49     private final ActorSystem actorSystem;
50
51     private SchemaContext currentSchemaContext = null;
52     private NetconfSessionPreferences netconfSessionPreferences = null;
53     private DOMRpcService deviceRpc = null;
54     private DOMDataBroker deviceDataBroker = 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         this.salProvider = new NetconfDeviceSalProvider(id, mountService, dataBroker);
64         this.actorSystem = actorSystem;
65         this.masterActorRef = masterActorRef;
66         this.actorResponseWaitTime = actorResponseWaitTime;
67     }
68
69     @Override
70     @SuppressWarnings("checkstyle:hiddenField")
71     public void onDeviceConnected(final SchemaContext remoteSchemaContext,
72                                   final NetconfSessionPreferences netconfSessionPreferences,
73                                   final DOMRpcService deviceRpc) {
74         this.currentSchemaContext = remoteSchemaContext;
75         this.netconfSessionPreferences = netconfSessionPreferences;
76         this.deviceRpc = deviceRpc;
77
78         registerMasterMountPoint();
79
80         sendInitialDataToActor().onComplete(new OnComplete<Object>() {
81             @Override
82             public void onComplete(final Throwable failure, final Object success) throws Throwable {
83                 if (failure == null) {
84                     updateDeviceData();
85                     return;
86                 }
87                 throw failure;
88             }
89         }, actorSystem.dispatcher());
90
91     }
92
93     @Override
94     public void onDeviceDisconnected() {
95         salProvider.getTopologyDatastoreAdapter().updateDeviceData(false, new NetconfDeviceCapabilities());
96         unregisterMasterMountPoint();
97     }
98
99     @Override
100     public void onDeviceFailed(final Throwable throwable) {
101         salProvider.getTopologyDatastoreAdapter().setDeviceAsFailed(throwable);
102         unregisterMasterMountPoint();
103     }
104
105     @Override
106     public void onNotification(final DOMNotification domNotification) {
107         salProvider.getMountInstance().publish(domNotification);
108     }
109
110     @Override
111     public void close() {
112         unregisterMasterMountPoint();
113         closeGracefully(salProvider);
114     }
115
116     private void registerMasterMountPoint() {
117         Preconditions.checkNotNull(id);
118         Preconditions.checkNotNull(currentSchemaContext,
119                 "Device has no remote schema context yet. Probably not fully connected.");
120         Preconditions.checkNotNull(netconfSessionPreferences,
121                 "Device has no capabilities yet. Probably not fully connected.");
122
123         final NetconfDeviceNotificationService notificationService = new NetconfDeviceNotificationService();
124
125         LOG.info("{}: Creating master data broker for device", id);
126
127         deviceDataBroker = new NetconfDeviceDataBroker(id, currentSchemaContext, deviceRpc, netconfSessionPreferences);
128         // We need to create ProxyDOMDataBroker so accessing mountpoint
129         // on leader node would be same as on follower node
130         final ProxyDOMDataBroker proxyDataBroker =
131                 new ProxyDOMDataBroker(actorSystem, id, masterActorRef, actorResponseWaitTime);
132         salProvider.getMountInstance()
133                 .onTopologyDeviceConnected(currentSchemaContext, proxyDataBroker, deviceRpc, notificationService);
134     }
135
136     private Future<Object> sendInitialDataToActor() {
137         final List<SourceIdentifier> sourceIdentifiers =
138                 SchemaContextUtil.getConstituentModuleIdentifiers(currentSchemaContext).stream()
139                 .map(mi -> RevisionSourceIdentifier.create(mi.getName(), mi.getRevision()))
140                 .collect(Collectors.toList());
141
142         // send initial data to master actor and create actor for providing it
143         return Patterns.ask(masterActorRef, new CreateInitialMasterActorData(deviceDataBroker, sourceIdentifiers,
144                 deviceRpc), actorResponseWaitTime);
145     }
146
147     private void updateDeviceData() {
148         final Cluster cluster = Cluster.get(actorSystem);
149         salProvider.getTopologyDatastoreAdapter().updateClusteredDeviceData(true, cluster.selfAddress().toString(),
150                 netconfSessionPreferences.getNetconfDeviceCapabilities());
151     }
152
153     private void unregisterMasterMountPoint() {
154         salProvider.getMountInstance().onTopologyDeviceDisconnected();
155     }
156
157     @SuppressWarnings("checkstyle:IllegalCatch")
158     private void closeGracefully(final AutoCloseable resource) {
159         if (resource != null) {
160             try {
161                 resource.close();
162             } catch (final Exception e) {
163                 LOG.error("{}: Ignoring exception while closing {}", id, resource, e);
164             }
165         }
166     }
167
168 }