Bump mdsal to 4.0.0
[netconf.git] / netconf / 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.sal.connect.util.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 RemoteDeviceId id;
34     private final Throwable failure;
35
36     FailedProxyTransactionFacade(final RemoteDeviceId id, final Throwable failure) {
37         this.id = Objects.requireNonNull(id);
38         this.failure = Objects.requireNonNull(failure);
39     }
40
41     @Override
42     public Object getIdentifier() {
43         return id;
44     }
45
46     @Override
47     public boolean cancel() {
48         return true;
49     }
50
51     @Override
52     public FluentFuture<Optional<NormalizedNode<?, ?>>> read(final LogicalDatastoreType store,
53             final YangInstanceIdentifier path) {
54         LOG.debug("{}: Read {} {} - failure", id, store, path, failure);
55         return FluentFutures.immediateFailedFluentFuture(ReadFailedException.MAPPER.apply(
56                 failure instanceof Exception ? (Exception)failure : new ReadFailedException("read", failure)));
57     }
58
59     @Override
60     public FluentFuture<Boolean> exists(final LogicalDatastoreType store,
61             final YangInstanceIdentifier path) {
62         LOG.debug("{}: Exists {} {} - failure", id, store, path, failure);
63         return FluentFutures.immediateFailedFluentFuture(ReadFailedException.MAPPER.apply(
64                 failure instanceof Exception ? (Exception)failure : new ReadFailedException("read", failure)));
65     }
66
67     @Override
68     public void delete(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
69         LOG.debug("{}: Delete {} {} - failure", id, store, path, failure);
70     }
71
72     @Override
73     public void put(final LogicalDatastoreType store, final YangInstanceIdentifier path,
74             final NormalizedNode<?, ?> data) {
75         LOG.debug("{}: Put {} {} - failure", id, store, path, failure);
76     }
77
78     @Override
79     public void merge(final LogicalDatastoreType store, final YangInstanceIdentifier path,
80             final NormalizedNode<?, ?> data) {
81         LOG.debug("{}: Merge {} {} - failure", id, store, path, failure);
82     }
83
84     @Override
85     public @NonNull FluentFuture<? extends @NonNull CommitInfo> commit() {
86         LOG.debug("{}: Commit - failure", id, failure);
87         final TransactionCommitFailedException txCommitEx;
88         if (failure instanceof TransactionCommitFailedException) {
89             txCommitEx = (TransactionCommitFailedException) failure;
90         } else {
91             txCommitEx = new TransactionCommitFailedException("commit", failure);
92         }
93         return FluentFutures.immediateFailedFluentFuture(txCommitEx);
94     }
95 }