Bump upstreams
[netconf.git] / apps / netconf-topology-singleton / src / main / java / org / opendaylight / netconf / topology / singleton / impl / tx / FailedProxyTransactionFacade.java
1 /*
2  * Copyright (c) 2018 Inocybe Technologies 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.tx;
9
10 import com.google.common.util.concurrent.FluentFuture;
11 import java.util.Objects;
12 import java.util.Optional;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.mdsal.common.api.CommitInfo;
15 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
16 import org.opendaylight.mdsal.common.api.ReadFailedException;
17 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
18 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
19 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
20 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
21 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Implementation of ProxyTransactionFacade that fails each request.
27  *
28  * @author Thomas Pantelis
29  */
30 class FailedProxyTransactionFacade implements ProxyTransactionFacade {
31     private static final Logger LOG = LoggerFactory.getLogger(FailedProxyTransactionFacade.class);
32
33     private final @NonNull FluentFuture<CommitInfo> completionFuture;
34     private final RemoteDeviceId id;
35     private final Throwable failure;
36
37     FailedProxyTransactionFacade(final RemoteDeviceId id, final Throwable failure) {
38         this.id = Objects.requireNonNull(id);
39         this.failure = Objects.requireNonNull(failure);
40         completionFuture = FluentFutures.immediateFailedFluentFuture(
41             failure instanceof TransactionCommitFailedException commitFailed ? commitFailed
42                 : new TransactionCommitFailedException("commit", failure));
43     }
44
45     @Override
46     public Object getIdentifier() {
47         return id;
48     }
49
50     @Override
51     public FluentFuture<?> completionFuture() {
52         return completionFuture;
53     }
54
55     @Override
56     public boolean cancel() {
57         return true;
58     }
59
60     @Override
61     public FluentFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
62             final YangInstanceIdentifier path) {
63         LOG.debug("{}: Read {} {} - failure", id, store, path, failure);
64         return FluentFutures.immediateFailedFluentFuture(ReadFailedException.MAPPER.apply(
65                 failure instanceof Exception ? (Exception)failure : new ReadFailedException("read", failure)));
66     }
67
68     @Override
69     public FluentFuture<Boolean> exists(final LogicalDatastoreType store,
70             final YangInstanceIdentifier path) {
71         LOG.debug("{}: Exists {} {} - failure", id, store, path, failure);
72         return FluentFutures.immediateFailedFluentFuture(ReadFailedException.MAPPER.apply(
73                 failure instanceof Exception ? (Exception)failure : new ReadFailedException("read", failure)));
74     }
75
76     @Override
77     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
78         LOG.debug("{}: Delete {} {} - failure", id, store, path, failure);
79     }
80
81     @Override
82     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) {
83         LOG.debug("{}: Put {} {} - failure", id, store, path, failure);
84     }
85
86     @Override
87     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path, final NormalizedNode data) {
88         LOG.debug("{}: Merge {} {} - failure", id, store, path, failure);
89     }
90
91     @Override
92     public FluentFuture<CommitInfo> commit() {
93         LOG.debug("{}: Commit - failure", id, failure);
94         return completionFuture;
95     }
96 }