2 * Copyright (c) 2016 Cisco Systems, Inc. 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.tx;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.dispatch.OnComplete;
14 import akka.pattern.Patterns;
15 import com.google.common.base.Optional;
16 import org.opendaylight.controller.config.util.xml.DocumentedException;
17 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorSeverity;
18 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorTag;
19 import org.opendaylight.controller.config.util.xml.DocumentedException.ErrorType;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
22 import org.opendaylight.netconf.topology.singleton.api.NetconfDOMTransaction;
23 import org.opendaylight.netconf.topology.singleton.impl.utils.NetconfTopologyUtils;
24 import org.opendaylight.netconf.topology.singleton.messages.NormalizedNodeMessage;
25 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitFailedReply;
26 import org.opendaylight.netconf.topology.singleton.messages.transactions.CancelRequest;
27 import org.opendaylight.netconf.topology.singleton.messages.transactions.DeleteRequest;
28 import org.opendaylight.netconf.topology.singleton.messages.transactions.EmptyReadResponse;
29 import org.opendaylight.netconf.topology.singleton.messages.transactions.ExistsRequest;
30 import org.opendaylight.netconf.topology.singleton.messages.transactions.MergeRequest;
31 import org.opendaylight.netconf.topology.singleton.messages.transactions.PutRequest;
32 import org.opendaylight.netconf.topology.singleton.messages.transactions.ReadRequest;
33 import org.opendaylight.netconf.topology.singleton.messages.transactions.SubmitRequest;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import scala.concurrent.Await;
38 import scala.concurrent.Future;
39 import scala.concurrent.impl.Promise.DefaultPromise;
42 public class NetconfProxyDOMTransaction implements NetconfDOMTransaction {
44 private static final Logger LOG = LoggerFactory.getLogger(NetconfProxyDOMTransaction.class);
46 private final RemoteDeviceId id;
47 private final ActorSystem actorSystem;
48 private final ActorRef masterContextRef;
50 public NetconfProxyDOMTransaction(final RemoteDeviceId id,
51 final ActorSystem actorSystem,
52 final ActorRef masterContextRef) {
54 this.actorSystem = actorSystem;
55 this.masterContextRef = masterContextRef;
59 public Future<Optional<NormalizedNodeMessage>> read(final LogicalDatastoreType store,
60 final YangInstanceIdentifier path) {
62 final Future<Object> readScalaFuture =
63 Patterns.ask(masterContextRef, new ReadRequest(store, path), NetconfTopologyUtils.TIMEOUT);
65 LOG.trace("{}: Read {} via NETCONF: {}", id, store, path);
67 final DefaultPromise<Optional<NormalizedNodeMessage>> promise = new DefaultPromise<>();
69 readScalaFuture.onComplete(new OnComplete<Object>() {
71 public void onComplete(final Throwable failure, final Object success) throws Throwable {
72 if (failure != null) { // ask timeout
73 Exception exception = new DocumentedException(id + ":Master is down. Please try again.",
74 ErrorType.application, ErrorTag.operation_failed,
75 ErrorSeverity.warning);
76 promise.failure(exception);
79 if (success instanceof Throwable) { // Error sended by master
80 promise.failure((Throwable) success);
83 if (success instanceof EmptyReadResponse) {
84 promise.success(Optional.absent());
88 promise.success(Optional.of((NormalizedNodeMessage) success));
90 }, actorSystem.dispatcher());
92 return promise.future();
96 public Future<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
97 final Future<Object> existsScalaFuture =
98 Patterns.ask(masterContextRef, new ExistsRequest(store, path), NetconfTopologyUtils.TIMEOUT);
100 LOG.trace("{}: Exists {} via NETCONF: {}", id, store, path);
102 final DefaultPromise<Boolean> promise = new DefaultPromise<>();
103 existsScalaFuture.onComplete(new OnComplete<Object>() {
105 public void onComplete(final Throwable failure, final Object success) throws Throwable {
106 if (failure != null) { // ask timeout
107 Exception exception = new DocumentedException(id + ":Master is down. Please try again.",
108 ErrorType.application, ErrorTag.operation_failed,
109 ErrorSeverity.warning);
110 promise.failure(exception);
113 if (success instanceof Throwable) {
114 promise.failure((Throwable) success);
117 promise.success((Boolean) success);
119 }, actorSystem.dispatcher());
120 return promise.future();
124 public void put(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
125 LOG.trace("{}: Write {} via NETCONF: {} with payload {}", id, store, data.getIdentifier(), data.getNode());
127 masterContextRef.tell(new PutRequest(store, data), ActorRef.noSender());
132 public void merge(final LogicalDatastoreType store, final NormalizedNodeMessage data) {
133 LOG.trace("{}: Merge {} via NETCONF: {} with payload {}", id, store, data.getIdentifier(), data.getNode());
135 masterContextRef.tell(new MergeRequest(store, data), ActorRef.noSender());
139 public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
140 LOG.trace("{}: Delete {} via NETCONF: {}", id, store, path);
142 masterContextRef.tell(new DeleteRequest(store, path), ActorRef.noSender());
146 public boolean cancel() {
147 final Future<Object> cancelScalaFuture =
148 Patterns.ask(masterContextRef, new CancelRequest(), NetconfTopologyUtils.TIMEOUT);
150 LOG.trace("{}: Cancel {} via NETCONF", id);
153 // here must be Await because AsyncWriteTransaction do not return future
154 return (boolean) Await.result(cancelScalaFuture, NetconfTopologyUtils.TIMEOUT.duration());
155 } catch (Exception e) {
161 public Future<Void> submit() {
162 final Future<Object> submitScalaFuture =
163 Patterns.ask(masterContextRef, new SubmitRequest(), NetconfTopologyUtils.TIMEOUT);
165 LOG.trace("{}: Submit {} via NETCONF", id);
167 final DefaultPromise<Void> promise = new DefaultPromise<>();
169 submitScalaFuture.onComplete(new OnComplete<Object>() {
171 public void onComplete(final Throwable failure, final Object success) throws Throwable {
172 if (failure != null) { // ask timeout
173 Exception exception = new DocumentedException(id + ":Master is down. Please try again.",
174 ErrorType.application, ErrorTag.operation_failed,
175 ErrorSeverity.warning);
176 promise.failure(exception);
179 if (success instanceof Throwable) {
180 promise.failure((Throwable) success);
182 if (success instanceof SubmitFailedReply) {
183 LOG.error("{}: Transaction was not submitted because already closed.", id);
185 promise.success(null);
188 }, actorSystem.dispatcher());
190 return promise.future();