47498f594f9d24199a9764044524a6b05f69a07a
[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 static com.google.common.base.Preconditions.checkState;
20
21 import com.google.common.base.Optional;
22 import com.google.common.util.concurrent.CheckedFuture;
23 import io.fd.honeycomb.v3po.translate.MappingContext;
24 import io.fd.honeycomb.v3po.translate.ModificationCache;
25 import io.fd.honeycomb.v3po.translate.write.WriteContext;
26 import java.util.Map;
27 import javax.annotation.Nonnull;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
31 import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
32 import org.opendaylight.yangtools.yang.binding.DataObject;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36
37 /**
38  * Transaction based WriteContext
39  */
40 public final class TransactionWriteContext implements WriteContext {
41
42     private final DOMDataReadOnlyTransaction beforeTx;
43     private final DOMDataReadOnlyTransaction afterTx;
44     private final ModificationCache ctx;
45     private final BindingNormalizedNodeSerializer serializer;
46     private final MappingContext mappingContext;
47
48     public TransactionWriteContext(final BindingNormalizedNodeSerializer serializer,
49                                    final DOMDataReadOnlyTransaction beforeTx,
50                                    final DOMDataReadOnlyTransaction afterTx,
51                                    final MappingContext mappingContext) {
52         this.serializer = serializer;
53         // TODO do we have a BA transaction adapter ? If so, use it here and don't pass serializer
54         this.beforeTx = beforeTx;
55         this.afterTx = afterTx;
56         this.mappingContext = mappingContext;
57         this.ctx = new ModificationCache();
58     }
59
60     // TODO make this asynchronous
61
62     @Override
63     public <T extends DataObject> Optional<T> readBefore(@Nonnull final InstanceIdentifier<T> currentId) {
64         return read(currentId, beforeTx);
65     }
66
67     @Override
68     public <T extends DataObject> Optional<T> readAfter(@Nonnull final InstanceIdentifier<T> currentId) {
69         return read(currentId, afterTx);
70     }
71
72
73     private <T extends DataObject> Optional<T> read(final InstanceIdentifier<T> currentId,
74                                                     final DOMDataReadOnlyTransaction tx) {
75         final YangInstanceIdentifier path = serializer.toYangInstanceIdentifier(currentId);
76
77         final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read =
78                 tx.read(LogicalDatastoreType.CONFIGURATION, path);
79
80         try {
81             // TODO once the APIs are asynchronous use just Futures.transform
82             final Optional<NormalizedNode<?, ?>> optional = read.checkedGet();
83
84             if (!optional.isPresent()) {
85                 return Optional.absent();
86             }
87
88             final NormalizedNode<?, ?> data = optional.get();
89             final Map.Entry<InstanceIdentifier<?>, DataObject> entry = serializer.fromNormalizedNode(path, data);
90
91             final Class<T> targetType = currentId.getTargetType();
92             checkState(targetType.isAssignableFrom(entry.getValue().getClass()),
93                 "Unexpected data object type, should be: %s, but was: %s", targetType, entry.getValue().getClass());
94             return Optional.of(targetType.cast(entry.getValue()));
95         } catch (ReadFailedException e) {
96             throw new IllegalStateException("Unable to perform read", e);
97         }
98     }
99
100     @Nonnull
101     @Override
102     public ModificationCache getModificationCache() {
103         return ctx;
104     }
105
106     @Nonnull
107     @Override
108     public MappingContext getMappingContext() {
109         return mappingContext;
110     }
111
112     /**
113      * Does not close the transactions
114      */
115     @Override
116     public void close() {
117         ctx.close();
118         beforeTx.close();
119         afterTx.close();
120     }
121 }