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