0bb68e3b2cf7773ed37990f8bf8a87ea1619894a
[hc2vpp.git] /
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.v3po.translate.util.write;
18
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import io.fd.honeycomb.v3po.translate.write.WriteContext;
22 import io.fd.honeycomb.v3po.translate.Context;
23 import java.util.Map;
24 import javax.annotation.Nonnull;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
28 import org.opendaylight.mdsal.binding.dom.codec.api.BindingNormalizedNodeSerializer;
29 import org.opendaylight.yangtools.yang.binding.DataObject;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33
34 /**
35  * Transaction based WriteContext
36  */
37 public final class TransactionWriteContext implements WriteContext {
38
39     private final DOMDataReadOnlyTransaction beforeTx;
40     private final DOMDataReadOnlyTransaction afterTx;
41     private final Context ctx;
42     private final BindingNormalizedNodeSerializer serializer;
43
44     public TransactionWriteContext(final BindingNormalizedNodeSerializer serializer,
45                                    final DOMDataReadOnlyTransaction beforeTx,
46                                    final DOMDataReadOnlyTransaction afterTx) {
47         this.serializer = serializer;
48         this.beforeTx = beforeTx;
49         this.afterTx = afterTx;
50         this.ctx = new Context();
51     }
52
53     // TODO make this asynchronous
54
55     @Override
56     public Optional<DataObject> readBefore(@Nonnull final InstanceIdentifier<? extends DataObject> currentId) {
57         return read(currentId, beforeTx);
58     }
59
60     private Optional<DataObject> read(final InstanceIdentifier<? extends DataObject> currentId,
61                                             final DOMDataReadOnlyTransaction tx) {
62         final YangInstanceIdentifier path = serializer.toYangInstanceIdentifier(currentId);
63
64         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read =
65                 tx.read(LogicalDatastoreType.CONFIGURATION, path);
66
67         try {
68             // TODO once the APIs are asynchronous use just Futures.transform
69             final Optional<NormalizedNode<?, ?>> optional = read.checkedGet();
70
71             if (!optional.isPresent()) {
72                 return Optional.absent();
73             }
74
75             final NormalizedNode<?, ?> data = optional.get();
76             final Map.Entry<InstanceIdentifier<?>, DataObject> entry = serializer.fromNormalizedNode(path, data);
77
78             return Optional.of(entry.getValue());
79         } catch (ReadFailedException e) {
80             throw new IllegalStateException("Unable to perform read", e);
81         }
82     }
83
84     @Override
85     public Optional<DataObject> readAfter(@Nonnull final InstanceIdentifier<? extends DataObject> currentId) {
86         return read(currentId, afterTx);
87     }
88
89     @Override
90     public Context getContext() {
91         return ctx;
92     }
93
94     /**
95      * Does not close the transactions
96      */
97     @Override
98     public void close() {
99         ctx.close();
100     }
101 }