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
8 package org.opendaylight.netconf.topology.singleton.impl.actors;
10 import akka.actor.Props;
11 import akka.actor.ReceiveTimeout;
12 import akka.actor.UntypedAbstractActor;
13 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
14 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadActorMessage;
15 import org.opendaylight.netconf.topology.singleton.messages.transactions.WriteActorMessage;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import scala.concurrent.duration.Duration;
20 public final class ReadWriteTransactionActor extends UntypedAbstractActor {
22 private static final Logger LOG = LoggerFactory.getLogger(ReadWriteTransactionActor.class);
24 private final DOMDataTreeReadWriteTransaction tx;
25 private final long idleTimeout;
26 private final ReadAdapter readAdapter;
27 private final WriteAdapter writeAdapter;
29 private ReadWriteTransactionActor(final DOMDataTreeReadWriteTransaction tx, final Duration idleTimeout) {
31 this.idleTimeout = idleTimeout.toSeconds();
32 if (this.idleTimeout > 0) {
33 context().setReceiveTimeout(idleTimeout);
35 readAdapter = new ReadAdapter(tx);
36 writeAdapter = new WriteAdapter(tx);
40 * Creates new actor Props.
42 * @param tx delegate device read write transaction
43 * @param idleTimeout idle time in seconds, after which transaction is closed automatically
46 static Props props(final DOMDataTreeReadWriteTransaction tx, final Duration idleTimeout) {
47 return Props.create(ReadWriteTransactionActor.class, () -> new ReadWriteTransactionActor(tx, idleTimeout));
51 public void onReceive(final Object message) {
52 if (message instanceof ReadActorMessage) {
53 readAdapter.handle(message, sender(), self());
54 } else 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());