2 * Copyright (c) 2016 Cisco and/or its affiliates.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package io.fd.honeycomb.v3po.translate.util.write;
19 import static com.google.common.base.Preconditions.checkState;
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;
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;
38 * Transaction based WriteContext
40 public final class TransactionWriteContext implements WriteContext {
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;
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();
60 // TODO make this asynchronous
63 public <T extends DataObject> Optional<T> readBefore(@Nonnull final InstanceIdentifier<T> currentId) {
64 return read(currentId, beforeTx);
68 public <T extends DataObject> Optional<T> readAfter(@Nonnull final InstanceIdentifier<T> currentId) {
69 return read(currentId, afterTx);
73 private <T extends DataObject> Optional<T> read(final InstanceIdentifier<T> currentId,
74 final DOMDataReadOnlyTransaction tx) {
75 final YangInstanceIdentifier path = serializer.toYangInstanceIdentifier(currentId);
77 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read =
78 tx.read(LogicalDatastoreType.CONFIGURATION, path);
81 // TODO once the APIs are asynchronous use just Futures.transform
82 final Optional<NormalizedNode<?, ?>> optional = read.checkedGet();
84 if (!optional.isPresent()) {
85 return Optional.absent();
88 final NormalizedNode<?, ?> data = optional.get();
89 final Map.Entry<InstanceIdentifier<?>, DataObject> entry = serializer.fromNormalizedNode(path, data);
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);
102 public ModificationCache getModificationCache() {
108 public MappingContext getMappingContext() {
109 return mappingContext;
113 * Does not close the transactions
116 public void close() {