Migrate netconf to MD-SAL APIs
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / actors / ReadWriteTransactionActor.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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.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;
19
20 public final class ReadWriteTransactionActor extends UntypedAbstractActor {
21
22     private static final Logger LOG = LoggerFactory.getLogger(ReadWriteTransactionActor.class);
23
24     private final DOMDataTreeReadWriteTransaction tx;
25     private final long idleTimeout;
26     private final ReadAdapter readAdapter;
27     private final WriteAdapter writeAdapter;
28
29     private ReadWriteTransactionActor(final DOMDataTreeReadWriteTransaction tx, final Duration idleTimeout) {
30         this.tx = tx;
31         this.idleTimeout = idleTimeout.toSeconds();
32         if (this.idleTimeout > 0) {
33             context().setReceiveTimeout(idleTimeout);
34         }
35         readAdapter = new ReadAdapter(tx);
36         writeAdapter = new WriteAdapter(tx);
37     }
38
39     /**
40      * Creates new actor Props.
41      *
42      * @param tx          delegate device read write transaction
43      * @param idleTimeout idle time in seconds, after which transaction is closed automatically
44      * @return props
45      */
46     static Props props(final DOMDataTreeReadWriteTransaction tx, final Duration idleTimeout) {
47         return Props.create(ReadWriteTransactionActor.class, () -> new ReadWriteTransactionActor(tx, idleTimeout));
48     }
49
50     @Override
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",
58                     idleTimeout);
59             tx.cancel();
60             context().stop(self());
61         } else {
62             unhandled(message);
63         }
64     }
65
66 }