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;
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.CheckedFuture;
21 import io.fd.honeycomb.v3po.translate.MappingContext;
22 import javax.annotation.Nonnull;
23 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
24 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
25 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.yangtools.yang.binding.DataObject;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31 * Binding Transaction backed mapping context.
33 public class TransactionMappingContext implements MappingContext {
35 private final ReadWriteTransaction readWriteTransaction;
39 public TransactionMappingContext(final ReadWriteTransaction readWriteTransaction) {
40 this.readWriteTransaction = readWriteTransaction;
44 public <T extends DataObject> Optional<T> read(@Nonnull final InstanceIdentifier<T> currentId) {
46 return readWriteTransaction.read(LogicalDatastoreType.OPERATIONAL, currentId).checkedGet();
47 } catch (ReadFailedException e) {
48 throw new IllegalStateException("Unable to perform read", e);
53 public void delete(final InstanceIdentifier<?> path) {
54 readWriteTransaction.delete(LogicalDatastoreType.OPERATIONAL, path);
58 public <T extends DataObject> void merge(final InstanceIdentifier<T> path, T data) {
59 readWriteTransaction.merge(LogicalDatastoreType.OPERATIONAL, path, data, true);
63 public <T extends DataObject> void put(final InstanceIdentifier<T> path, T data) {
64 readWriteTransaction.put(LogicalDatastoreType.OPERATIONAL, path, data, true);
67 public CheckedFuture<Void, TransactionCommitFailedException> submit() {
68 return readWriteTransaction.submit();
73 readWriteTransaction.cancel();