Bump MRI upstreams
[netconf.git] / restconf / restconf-nb-rfc8040 / src / main / java / org / opendaylight / restconf / nb / rfc8040 / rests / transactions / NetconfRestconfStrategy.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, 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 package org.opendaylight.restconf.nb.rfc8040.rests.transactions;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.FluentFuture;
13 import com.google.common.util.concurrent.FutureCallback;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import com.google.common.util.concurrent.SettableFuture;
18 import java.util.List;
19 import java.util.Optional;
20 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
21 import org.opendaylight.mdsal.common.api.ReadFailedException;
22 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Implementation of RESTCONF operations on top of a raw NETCONF backend.
30  *
31  * @see NetconfDataTreeService
32  */
33 public final class NetconfRestconfStrategy extends RestconfStrategy {
34     private static final Logger LOG = LoggerFactory.getLogger(NetconfRestconfStrategy.class);
35
36     private final NetconfDataTreeService netconfService;
37
38     public NetconfRestconfStrategy(final NetconfDataTreeService netconfService) {
39         this.netconfService = requireNonNull(netconfService);
40     }
41
42     @Override
43     public RestconfTransaction prepareWriteExecution() {
44         return new NetconfRestconfTransaction(netconfService);
45     }
46
47     @Override
48     public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
49             final YangInstanceIdentifier path) {
50         switch (store) {
51             case CONFIGURATION:
52                 return netconfService.getConfig(path);
53             case OPERATIONAL:
54                 return netconfService.get(path);
55             default:
56                 LOG.info("Unknown datastore type: {}.", store);
57                 throw new IllegalArgumentException(String.format(
58                         "%s, Cannot read data %s for %s datastore, unknown datastore type",
59                         netconfService.getDeviceId(), path, store));
60         }
61     }
62
63     @Override
64     public ListenableFuture<Optional<NormalizedNode>> read(final LogicalDatastoreType store,
65             final YangInstanceIdentifier path, final List<YangInstanceIdentifier> fields) {
66         switch (store) {
67             case CONFIGURATION:
68                 return netconfService.getConfig(path, fields);
69             case OPERATIONAL:
70                 return netconfService.get(path, fields);
71             default:
72                 LOG.info("Unknown datastore type: {}.", store);
73                 throw new IllegalArgumentException(String.format(
74                         "%s, Cannot read data %s with fields %s for %s datastore, unknown datastore type",
75                         netconfService.getDeviceId(), path, fields, store));
76         }
77     }
78
79     @Override
80     public FluentFuture<Boolean> exists(final LogicalDatastoreType store, final YangInstanceIdentifier path) {
81         return remapException(read(store, path))
82                 .transform(optionalNode -> optionalNode != null && optionalNode.isPresent(),
83                         MoreExecutors.directExecutor());
84     }
85
86     private static <T> FluentFuture<T> remapException(final ListenableFuture<T> input) {
87         final SettableFuture<T> ret = SettableFuture.create();
88         Futures.addCallback(input, new FutureCallback<T>() {
89
90             @Override
91             public void onSuccess(final T result) {
92                 ret.set(result);
93             }
94
95             @Override
96             public void onFailure(final Throwable cause) {
97                 ret.setException(cause instanceof ReadFailedException ? cause
98                     : new ReadFailedException("NETCONF operation failed", cause));
99             }
100         }, MoreExecutors.directExecutor());
101         return FluentFuture.from(ret);
102     }
103 }