Bump odlparent to 6.0.0
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / actors / NetconfNodeActor.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.actors;
9
10 import akka.actor.ActorRef;
11 import akka.actor.Props;
12 import akka.actor.Status.Failure;
13 import akka.actor.Status.Success;
14 import akka.pattern.AskTimeoutException;
15 import akka.util.Timeout;
16 import com.google.common.base.Throwables;
17 import com.google.common.util.concurrent.FutureCallback;
18 import com.google.common.util.concurrent.Futures;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.MoreExecutors;
21 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.stream.Collectors;
25 import org.opendaylight.controller.cluster.common.actor.AbstractUntypedActor;
26 import org.opendaylight.controller.cluster.schema.provider.RemoteYangTextSourceProvider;
27 import org.opendaylight.controller.cluster.schema.provider.impl.RemoteSchemaProvider;
28 import org.opendaylight.controller.cluster.schema.provider.impl.YangTextSchemaSourceSerializationProxy;
29 import org.opendaylight.mdsal.dom.api.DOMActionResult;
30 import org.opendaylight.mdsal.dom.api.DOMActionService;
31 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
32 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
36 import org.opendaylight.mdsal.dom.api.DOMMountPointService;
37 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
38 import org.opendaylight.mdsal.dom.api.DOMRpcService;
39 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
40 import org.opendaylight.netconf.topology.singleton.impl.ProxyDOMActionService;
41 import org.opendaylight.netconf.topology.singleton.impl.ProxyDOMRpcService;
42 import org.opendaylight.netconf.topology.singleton.impl.ProxyYangTextSourceProvider;
43 import org.opendaylight.netconf.topology.singleton.impl.SlaveSalFacade;
44 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologySetup;
45 import org.opendaylight.netconf.topology.singleton.messages.AskForMasterMountPoint;
46 import org.opendaylight.netconf.topology.singleton.messages.ContainerNodeMessage;
47 import org.opendaylight.netconf.topology.singleton.messages.CreateInitialMasterActorData;
48 import org.opendaylight.netconf.topology.singleton.messages.MasterActorDataInitialized;
49 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
50 import org.opendaylight.netconf.topology.singleton.messages.NotMasterException;
51 import org.opendaylight.netconf.topology.singleton.messages.RefreshSetupMasterActorData;
52 import org.opendaylight.netconf.topology.singleton.messages.RefreshSlaveActor;
53 import org.opendaylight.netconf.topology.singleton.messages.RegisterMountPoint;
54 import org.opendaylight.netconf.topology.singleton.messages.UnregisterSlaveMountPoint;
55 import org.opendaylight.netconf.topology.singleton.messages.YangTextSchemaSourceRequest;
56 import org.opendaylight.netconf.topology.singleton.messages.action.InvokeActionMessage;
57 import org.opendaylight.netconf.topology.singleton.messages.action.InvokeActionMessageReply;
58 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessage;
59 import org.opendaylight.netconf.topology.singleton.messages.rpc.InvokeRpcMessageReply;
60 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyResultResponse;
61 import org.opendaylight.netconf.topology.singleton.messages.transactions.NewReadTransactionRequest;
62 import org.opendaylight.netconf.topology.singleton.messages.transactions.NewReadWriteTransactionRequest;
63 import org.opendaylight.netconf.topology.singleton.messages.transactions.NewWriteTransactionRequest;
64 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
65 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
66 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
67 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
68 import org.opendaylight.yangtools.yang.model.repo.api.EffectiveModelContextFactory;
69 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
70 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
71 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
72 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
73 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
74 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
75 import scala.concurrent.duration.Duration;
76
77 public class NetconfNodeActor extends AbstractUntypedActor {
78     private final Duration writeTxIdleTimeout;
79     private final DOMMountPointService mountPointService;
80
81     private SchemaSourceRegistry schemaRegistry;
82     private SchemaRepository schemaRepository;
83     private Timeout actorResponseWaitTime;
84     private RemoteDeviceId id;
85     private NetconfTopologySetup setup;
86     private List<SourceIdentifier> sourceIdentifiers;
87     private DOMRpcService deviceRpc;
88     private DOMActionService deviceAction;
89     private SlaveSalFacade slaveSalManager;
90     private DOMDataBroker deviceDataBroker;
91     //readTxActor can be shared
92     private ActorRef readTxActor;
93     private List<SchemaSourceRegistration<YangTextSchemaSource>> registeredSchemas;
94
95     public static Props props(final NetconfTopologySetup setup, final RemoteDeviceId id,
96             final Timeout actorResponseWaitTime, final DOMMountPointService mountPointService) {
97         return Props.create(NetconfNodeActor.class, () ->
98                 new NetconfNodeActor(setup, id, actorResponseWaitTime, mountPointService));
99     }
100
101     protected NetconfNodeActor(final NetconfTopologySetup setup,
102                                final RemoteDeviceId id, final Timeout actorResponseWaitTime,
103                                final DOMMountPointService mountPointService) {
104         this.setup = setup;
105         this.id = id;
106         this.schemaRegistry = setup.getSchemaResourcesDTO().getSchemaRegistry();
107         this.schemaRepository = setup.getSchemaResourcesDTO().getSchemaRepository();
108         this.actorResponseWaitTime = actorResponseWaitTime;
109         this.writeTxIdleTimeout = setup.getIdleTimeout();
110         this.mountPointService = mountPointService;
111     }
112
113     @SuppressWarnings("checkstyle:IllegalCatch")
114     @Override
115     public void handleReceive(final Object message) {
116         LOG.debug("{}:  received message {}", id, message);
117
118         if (message instanceof CreateInitialMasterActorData) { // master
119
120             final CreateInitialMasterActorData masterActorData = (CreateInitialMasterActorData) message;
121             sourceIdentifiers = masterActorData.getSourceIndentifiers();
122             this.deviceDataBroker = masterActorData.getDeviceDataBroker();
123             final DOMDataTreeReadTransaction tx = deviceDataBroker.newReadOnlyTransaction();
124             readTxActor = context().actorOf(ReadTransactionActor.props(tx));
125             this.deviceRpc = masterActorData.getDeviceRpc();
126             this.deviceAction = masterActorData.getDeviceAction();
127
128             sender().tell(new MasterActorDataInitialized(), self());
129
130             LOG.debug("{}: Master is ready.", id);
131
132         } else if (message instanceof RefreshSetupMasterActorData) {
133             setup = ((RefreshSetupMasterActorData) message).getNetconfTopologyDeviceSetup();
134             id = ((RefreshSetupMasterActorData) message).getRemoteDeviceId();
135             sender().tell(new MasterActorDataInitialized(), self());
136         } else if (message instanceof AskForMasterMountPoint) { // master
137             AskForMasterMountPoint askForMasterMountPoint = (AskForMasterMountPoint) message;
138
139             // only master contains reference to deviceDataBroker
140             if (deviceDataBroker != null) {
141                 LOG.debug("{}: Sending RegisterMountPoint reply to {}", id, askForMasterMountPoint.getSlaveActorRef());
142                 askForMasterMountPoint.getSlaveActorRef().tell(new RegisterMountPoint(sourceIdentifiers, self()),
143                     sender());
144             } else {
145                 LOG.warn("{}: Received {} but we don't appear to be the master", id, askForMasterMountPoint);
146                 sender().tell(new Failure(new NotMasterException(self())), self());
147             }
148
149         } else if (message instanceof YangTextSchemaSourceRequest) { // master
150
151             final YangTextSchemaSourceRequest yangTextSchemaSourceRequest = (YangTextSchemaSourceRequest) message;
152             sendYangTextSchemaSourceProxy(yangTextSchemaSourceRequest.getSourceIdentifier(), sender());
153
154         } else if (message instanceof NewReadTransactionRequest) { // master
155             sender().tell(new Success(readTxActor), self());
156         } else if (message instanceof NewWriteTransactionRequest) { // master
157             try {
158                 final DOMDataTreeWriteTransaction tx = deviceDataBroker.newWriteOnlyTransaction();
159                 final ActorRef txActor = context().actorOf(WriteTransactionActor.props(tx, writeTxIdleTimeout));
160                 sender().tell(new Success(txActor), self());
161             } catch (final Exception t) {
162                 sender().tell(new Failure(t), self());
163             }
164
165         } else if (message instanceof NewReadWriteTransactionRequest) {
166             try {
167                 final DOMDataTreeReadWriteTransaction tx = deviceDataBroker.newReadWriteTransaction();
168                 final ActorRef txActor = context().actorOf(ReadWriteTransactionActor.props(tx, writeTxIdleTimeout));
169                 sender().tell(new Success(txActor), self());
170             } catch (final Exception t) {
171                 sender().tell(new Failure(t), self());
172             }
173         } else if (message instanceof InvokeRpcMessage) { // master
174             final InvokeRpcMessage invokeRpcMessage = (InvokeRpcMessage) message;
175             invokeSlaveRpc(invokeRpcMessage.getSchemaPath(), invokeRpcMessage.getNormalizedNodeMessage(), sender());
176         } else if (message instanceof InvokeActionMessage) { // master
177             final InvokeActionMessage invokeActionMessage = (InvokeActionMessage) message;
178             LOG.info("InvokeActionMessage Details : {}", invokeActionMessage.toString());
179             invokeSlaveAction(invokeActionMessage.getSchemaPath(), invokeActionMessage.getContainerNodeMessage(),
180                 invokeActionMessage.getDOMDataTreeIdentifier(), sender());
181         } else if (message instanceof RegisterMountPoint) { //slaves
182             RegisterMountPoint registerMountPoint = (RegisterMountPoint) message;
183             sourceIdentifiers = registerMountPoint.getSourceIndentifiers();
184             registerSlaveMountPoint(registerMountPoint.getMasterActorRef());
185             sender().tell(new Success(null), self());
186         } else if (message instanceof UnregisterSlaveMountPoint) { //slaves
187             unregisterSlaveMountPoint();
188         } else if (message instanceof RefreshSlaveActor) { //slave
189             actorResponseWaitTime = ((RefreshSlaveActor) message).getActorResponseWaitTime();
190             id = ((RefreshSlaveActor) message).getId();
191             schemaRegistry = ((RefreshSlaveActor) message).getSchemaRegistry();
192             setup = ((RefreshSlaveActor) message).getSetup();
193             schemaRepository = ((RefreshSlaveActor) message).getSchemaRepository();
194         }
195     }
196
197     @Override
198     public void postStop() throws Exception {
199         try {
200             super.postStop();
201         } finally {
202             unregisterSlaveMountPoint();
203         }
204     }
205
206     private void unregisterSlaveMountPoint() {
207         if (slaveSalManager != null) {
208             slaveSalManager.close();
209             slaveSalManager = null;
210         }
211
212         closeSchemaSourceRegistrations();
213     }
214
215     private void sendYangTextSchemaSourceProxy(final SourceIdentifier sourceIdentifier, final ActorRef sender) {
216         final ListenableFuture<YangTextSchemaSource> schemaSourceFuture =
217                 schemaRepository.getSchemaSource(sourceIdentifier, YangTextSchemaSource.class);
218
219         Futures.addCallback(schemaSourceFuture, new FutureCallback<YangTextSchemaSource>() {
220             @Override
221             public void onSuccess(final YangTextSchemaSource yangTextSchemaSource) {
222                 try {
223                     LOG.debug("{}: getSchemaSource for {} succeeded", id, sourceIdentifier);
224                     sender.tell(new YangTextSchemaSourceSerializationProxy(yangTextSchemaSource), getSelf());
225                 } catch (IOException e) {
226                     sender.tell(new Failure(e), getSelf());
227                 }
228             }
229
230             @Override
231             public void onFailure(final Throwable throwable) {
232                 LOG.debug("{}: getSchemaSource for {} failed", id, sourceIdentifier, throwable);
233                 sender.tell(new Failure(throwable), getSelf());
234             }
235         }, MoreExecutors.directExecutor());
236     }
237
238     private void invokeSlaveRpc(final SchemaPath schemaPath, final NormalizedNodeMessage normalizedNodeMessage,
239                                 final ActorRef recipient) {
240
241         LOG.debug("{}: invokeSlaveRpc for {}, input: {} on rpc service {}", id, schemaPath, normalizedNodeMessage,
242                 deviceRpc);
243
244         final ListenableFuture<DOMRpcResult> rpcResult = deviceRpc.invokeRpc(schemaPath,
245                 normalizedNodeMessage != null ? normalizedNodeMessage.getNode() : null);
246
247         Futures.addCallback(rpcResult, new FutureCallback<DOMRpcResult>() {
248             @Override
249             public void onSuccess(final DOMRpcResult domRpcResult) {
250                 LOG.debug("{}: invokeSlaveRpc for {}, domRpcResult: {}", id, schemaPath, domRpcResult);
251
252                 if (domRpcResult == null) {
253                     recipient.tell(new EmptyResultResponse(), getSender());
254                     return;
255                 }
256                 NormalizedNodeMessage nodeMessageReply = null;
257                 if (domRpcResult.getResult() != null) {
258                     nodeMessageReply = new NormalizedNodeMessage(YangInstanceIdentifier.empty(),
259                             domRpcResult.getResult());
260                 }
261                 recipient.tell(new InvokeRpcMessageReply(nodeMessageReply, domRpcResult.getErrors()), getSelf());
262             }
263
264             @Override
265             public void onFailure(final Throwable throwable) {
266                 recipient.tell(new Failure(throwable), getSelf());
267             }
268         }, MoreExecutors.directExecutor());
269     }
270
271     /**
272      * Invoking Action on Slave Node in Odl Cluster Environment.
273      *
274      * @param schemaPath {@link SchemaPath}
275      * @param containerNodeMessage {@link ContainerNodeMessage}
276      * @param domDataTreeIdentifier {@link DOMDataTreeIdentifier}
277      * @param recipient {@link ActorRef}
278      */
279     private void invokeSlaveAction(final SchemaPath schemaPath, final ContainerNodeMessage containerNodeMessage,
280         final DOMDataTreeIdentifier domDataTreeIdentifier, final ActorRef recipient) {
281         LOG.info("{}: invokeSlaveAction for {}, input: {}, identifier: {} on action service {}", id, schemaPath,
282             containerNodeMessage, domDataTreeIdentifier, deviceAction);
283
284         final ListenableFuture<? extends DOMActionResult> actionResult = deviceAction.invokeAction(schemaPath,
285             domDataTreeIdentifier, containerNodeMessage != null ? containerNodeMessage.getNode() : null);
286
287         Futures.addCallback(actionResult, new FutureCallback<DOMActionResult>() {
288
289             @Override
290             public void onSuccess(final DOMActionResult domActionResult) {
291                 LOG.debug("{}: invokeSlaveAction for {}, domActionResult: {}", id, schemaPath, domActionResult);
292                 if (domActionResult == null) {
293                     recipient.tell(new EmptyResultResponse(), getSender());
294                     return;
295                 }
296
297                 //Check DomActionResult containing Ok onSuccess pass empty nodeMessageReply
298                 ContainerNodeMessage nodeMessageReply = domActionResult.getOutput().map(ContainerNodeMessage::new)
299                         .orElse(null);
300                 recipient.tell(new InvokeActionMessageReply(nodeMessageReply, domActionResult.getErrors()), getSelf());
301             }
302
303             @Override
304             public void onFailure(final Throwable throwable) {
305                 recipient.tell(new Failure(throwable), getSelf());
306             }
307         }, MoreExecutors.directExecutor());
308     }
309
310     private void registerSlaveMountPoint(final ActorRef masterReference) {
311         unregisterSlaveMountPoint();
312
313         slaveSalManager = new SlaveSalFacade(id, setup.getActorSystem(), actorResponseWaitTime, mountPointService);
314
315         resolveSchemaContext(createSchemaContextFactory(masterReference), slaveSalManager, masterReference, 1);
316     }
317
318     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
319             justification = "https://github.com/spotbugs/spotbugs/issues/811")
320     private DOMRpcService getDOMRpcService(final ActorRef masterReference) {
321         return new ProxyDOMRpcService(setup.getActorSystem(), masterReference, id, actorResponseWaitTime);
322     }
323
324     @SuppressFBWarnings(value = "UPM_UNCALLED_PRIVATE_METHOD",
325             justification = "https://github.com/spotbugs/spotbugs/issues/811")
326     private DOMActionService getDOMActionService(final ActorRef masterReference) {
327         return new ProxyDOMActionService(setup.getActorSystem(), masterReference, id, actorResponseWaitTime);
328     }
329
330     private EffectiveModelContextFactory createSchemaContextFactory(final ActorRef masterReference) {
331         final RemoteYangTextSourceProvider remoteYangTextSourceProvider =
332                 new ProxyYangTextSourceProvider(masterReference, getContext().dispatcher(), actorResponseWaitTime);
333         final RemoteSchemaProvider remoteProvider = new RemoteSchemaProvider(remoteYangTextSourceProvider,
334                 getContext().dispatcher());
335
336         registeredSchemas = sourceIdentifiers.stream()
337                 .map(sourceId ->
338                         schemaRegistry.registerSchemaSource(remoteProvider, PotentialSchemaSource.create(sourceId,
339                                 YangTextSchemaSource.class, PotentialSchemaSource.Costs.REMOTE_IO.getValue())))
340                 .collect(Collectors.toList());
341
342         return schemaRepository.createEffectiveModelContextFactory();
343     }
344
345     private void resolveSchemaContext(final EffectiveModelContextFactory schemaContextFactory,
346             final SlaveSalFacade localSlaveSalManager, final ActorRef masterReference, final int tries) {
347         final ListenableFuture<EffectiveModelContext> schemaContextFuture =
348                 schemaContextFactory.createEffectiveModelContext(sourceIdentifiers);
349         Futures.addCallback(schemaContextFuture, new FutureCallback<SchemaContext>() {
350             @Override
351             public void onSuccess(final SchemaContext result) {
352                 executeInSelf(() -> {
353                     // Make sure the slaveSalManager instance hasn't changed since we initiated the schema context
354                     // resolution.
355                     if (slaveSalManager == localSlaveSalManager) {
356                         LOG.info("{}: Schema context resolved: {} - registering slave mount point",
357                                 id, result.getModules());
358                         slaveSalManager.registerSlaveMountPoint(result, getDOMRpcService(masterReference),
359                             getDOMActionService(masterReference), masterReference);
360                     }
361                 });
362             }
363
364             @Override
365             public void onFailure(final Throwable throwable) {
366                 executeInSelf(() -> {
367                     if (slaveSalManager == localSlaveSalManager) {
368                         final Throwable cause = Throwables.getRootCause(throwable);
369                         if (cause instanceof AskTimeoutException) {
370                             if (tries <= 5 || tries % 10 == 0) {
371                                 LOG.warn("{}: Failed to resolve schema context - retrying...", id, throwable);
372                             }
373
374                             resolveSchemaContext(schemaContextFactory, localSlaveSalManager,
375                                     masterReference, tries + 1);
376                         } else {
377                             LOG.error("{}: Failed to resolve schema context - unable to register slave mount point",
378                                     id, throwable);
379                             closeSchemaSourceRegistrations();
380                         }
381                     }
382                 });
383             }
384         }, MoreExecutors.directExecutor());
385     }
386
387     private void closeSchemaSourceRegistrations() {
388         if (registeredSchemas != null) {
389             registeredSchemas.forEach(SchemaSourceRegistration::close);
390             registeredSchemas = null;
391         }
392     }
393 }