2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.netconf.topology.singleton.impl.actors;
11 import akka.actor.Props;
12 import akka.actor.ReceiveTimeout;
13 import akka.actor.UntypedAbstractActor;
14 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
15 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
16 import org.opendaylight.netconf.topology.singleton.messages.transactions.WriteActorMessage;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import scala.concurrent.duration.Duration;
22 * WriteTransactionActor is an interface to device's {@link DOMDataReadOnlyTransaction} for cluster nodes.
24 public final class WriteTransactionActor extends UntypedAbstractActor {
26 private static final Logger LOG = LoggerFactory.getLogger(WriteTransactionActor.class);
28 private final DOMDataWriteTransaction tx;
29 private final long idleTimeout;
30 private final WriteAdapter writeAdapter;
32 private WriteTransactionActor(final DOMDataWriteTransaction tx, final Duration idleTimeout) {
34 this.idleTimeout = idleTimeout.toSeconds();
35 if (this.idleTimeout > 0) {
36 context().setReceiveTimeout(idleTimeout);
38 writeAdapter = new WriteAdapter(tx);
42 * Creates new actor Props.
44 * @param tx delegate device write transaction
45 * @param idleTimeout idle time in seconds, after which transaction is closed automatically
48 static Props props(final DOMDataWriteTransaction tx, final Duration idleTimeout) {
49 return Props.create(WriteTransactionActor.class, () -> new WriteTransactionActor(tx, idleTimeout));
53 public void onReceive(final Object message) throws Throwable {
54 if (message instanceof WriteActorMessage) {
55 writeAdapter.handle(message, sender(), context(), self());
56 } else if (message instanceof ReceiveTimeout) {
57 LOG.warn("Haven't received any message for {} seconds, cancelling transaction and stopping actor",
60 context().stop(self());