Bug 8289 - 409 in cluster restperfclient test
[netconf.git] / netconf / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / ProxyDOMDataBroker.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
9 package org.opendaylight.netconf.topology.singleton.impl;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.pattern.Patterns;
14 import akka.util.Timeout;
15 import com.google.common.base.Preconditions;
16 import java.util.Collections;
17 import java.util.Map;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.controller.md.sal.common.api.data.TransactionChainListener;
21 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
22 import org.opendaylight.controller.md.sal.dom.api.DOMDataBrokerExtension;
23 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
24 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
25 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
27 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
28 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
29 import org.opendaylight.netconf.topology.singleton.impl.tx.ProxyReadTransaction;
30 import org.opendaylight.netconf.topology.singleton.impl.tx.ProxyReadWriteTransaction;
31 import org.opendaylight.netconf.topology.singleton.impl.tx.ProxyWriteTransaction;
32 import org.opendaylight.netconf.topology.singleton.messages.transactions.NewReadTransactionReply;
33 import org.opendaylight.netconf.topology.singleton.messages.transactions.NewReadTransactionRequest;
34 import org.opendaylight.netconf.topology.singleton.messages.transactions.NewReadWriteTransactionReply;
35 import org.opendaylight.netconf.topology.singleton.messages.transactions.NewReadWriteTransactionRequest;
36 import org.opendaylight.netconf.topology.singleton.messages.transactions.NewWriteTransactionReply;
37 import org.opendaylight.netconf.topology.singleton.messages.transactions.NewWriteTransactionRequest;
38 import org.opendaylight.yangtools.concepts.ListenerRegistration;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
40 import scala.concurrent.Await;
41 import scala.concurrent.Future;
42
43 public class ProxyDOMDataBroker implements DOMDataBroker {
44
45     private final Timeout askTimeout;
46     private final RemoteDeviceId id;
47     private final ActorRef masterNode;
48     private final ActorSystem actorSystem;
49
50     /**
51      * @param actorSystem system
52      * @param id          id
53      * @param masterNode  {@link org.opendaylight.netconf.topology.singleton.impl.actors.NetconfNodeActor} ref
54      * @param askTimeout  ask timeout
55      */
56     public ProxyDOMDataBroker(final ActorSystem actorSystem, final RemoteDeviceId id,
57                               final ActorRef masterNode, final Timeout askTimeout) {
58         this.id = id;
59         this.masterNode = masterNode;
60         this.actorSystem = actorSystem;
61         this.askTimeout = askTimeout;
62     }
63
64     @Override
65     public DOMDataReadOnlyTransaction newReadOnlyTransaction() {
66         final Future<Object> txActorFuture = Patterns.ask(masterNode, new NewReadTransactionRequest(), askTimeout);
67         try {
68             final Object msg = Await.result(txActorFuture, askTimeout.duration());
69             if (msg instanceof Throwable) {
70                 throw (Throwable) msg;
71             }
72             Preconditions.checkState(msg instanceof NewReadTransactionReply);
73             final NewReadTransactionReply reply = (NewReadTransactionReply) msg;
74             return new ProxyReadTransaction(reply.getTxActor(), id, actorSystem, askTimeout);
75         } catch (final Throwable t) {
76             throw new IllegalStateException("Can't create ProxyReadTransaction", t);
77         }
78     }
79
80     @Override
81     public DOMDataReadWriteTransaction newReadWriteTransaction() {
82         final Future<Object> txActorFuture = Patterns.ask(masterNode, new NewReadWriteTransactionRequest(), askTimeout);
83         try {
84             final Object msg = Await.result(txActorFuture, askTimeout.duration());
85             if (msg instanceof Throwable) {
86                 throw (Throwable) msg;
87             }
88             Preconditions.checkState(msg instanceof NewReadWriteTransactionReply);
89             final NewReadWriteTransactionReply reply = (NewReadWriteTransactionReply) msg;
90             return new ProxyReadWriteTransaction(reply.getTxActor(), id, actorSystem, askTimeout);
91         } catch (final Throwable t) {
92             throw new IllegalStateException("Can't create ProxyReadTransaction", t);
93         }
94     }
95
96     @Override
97     public DOMDataWriteTransaction newWriteOnlyTransaction() {
98         final Future<Object> txActorFuture = Patterns.ask(masterNode, new NewWriteTransactionRequest(), askTimeout);
99         try {
100             final Object msg = Await.result(txActorFuture, askTimeout.duration());
101             if (msg instanceof Throwable) {
102                 throw (Throwable) msg;
103             }
104             Preconditions.checkState(msg instanceof NewWriteTransactionReply);
105             final NewWriteTransactionReply reply = (NewWriteTransactionReply) msg;
106             return new ProxyWriteTransaction(reply.getTxActor(), id, actorSystem, askTimeout);
107         } catch (final Throwable t) {
108             throw new IllegalStateException("Can't create ProxyWriteTransaction", t);
109         }
110     }
111
112     @Override
113     public ListenerRegistration<DOMDataChangeListener> registerDataChangeListener(
114             final LogicalDatastoreType store, final YangInstanceIdentifier path, final DOMDataChangeListener listener,
115             final DataChangeScope triggeringScope) {
116         throw new UnsupportedOperationException(id + ": Data change listeners not supported for netconf mount point");
117     }
118
119     @Override
120     public DOMTransactionChain createTransactionChain(final TransactionChainListener listener) {
121         throw new UnsupportedOperationException(id + ": Transaction chains not supported for netconf mount point");
122     }
123
124     @Nonnull
125     @Override
126     public Map<Class<? extends DOMDataBrokerExtension>, DOMDataBrokerExtension> getSupportedExtensions() {
127         return Collections.emptyMap();
128     }
129 }