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 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;
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;
35 * Transaction based WriteContext
37 public final class TransactionWriteContext implements WriteContext {
39 private final DOMDataReadOnlyTransaction beforeTx;
40 private final DOMDataReadOnlyTransaction afterTx;
41 private final Context ctx;
42 private final BindingNormalizedNodeSerializer serializer;
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();
53 // TODO make this asynchronous
56 public Optional<DataObject> readBefore(@Nonnull final InstanceIdentifier<? extends DataObject> currentId) {
57 return read(currentId, beforeTx);
60 private Optional<DataObject> read(final InstanceIdentifier<? extends DataObject> currentId,
61 final DOMDataReadOnlyTransaction tx) {
62 final YangInstanceIdentifier path = serializer.toYangInstanceIdentifier(currentId);
64 final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read =
65 tx.read(LogicalDatastoreType.CONFIGURATION, path);
68 // TODO once the APIs are asynchronous use just Futures.transform
69 final Optional<NormalizedNode<?, ?>> optional = read.checkedGet();
71 if (!optional.isPresent()) {
72 return Optional.absent();
75 final NormalizedNode<?, ?> data = optional.get();
76 final Map.Entry<InstanceIdentifier<?>, DataObject> entry = serializer.fromNormalizedNode(path, data);
78 return Optional.of(entry.getValue());
79 } catch (ReadFailedException e) {
80 throw new IllegalStateException("Unable to perform read", e);
85 public Optional<DataObject> readAfter(@Nonnull final InstanceIdentifier<? extends DataObject> currentId) {
86 return read(currentId, afterTx);
90 public Context getContext() {
95 * Does not close the transactions