60f4282f56be70490d2b5ebcb01649e98586f5e9
[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.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;
29
30 /**
31  * Binding Transaction backed mapping context
32  */
33 public class TransactionMappingContext implements MappingContext {
34
35     private final ReadWriteTransaction readWriteTransaction;
36
37     // TODO make async
38
39     public TransactionMappingContext(final ReadWriteTransaction readWriteTransaction) {
40         this.readWriteTransaction = readWriteTransaction;
41     }
42
43     @Override
44     public <T extends DataObject> Optional<T> read(@Nonnull final InstanceIdentifier<T> currentId) {
45         try {
46             return readWriteTransaction.read(LogicalDatastoreType.OPERATIONAL, currentId).checkedGet();
47         } catch (ReadFailedException e) {
48             throw new IllegalStateException("Unable to perform read", e);
49         }
50     }
51
52     @Override
53     public void delete(final InstanceIdentifier<?> path) {
54         readWriteTransaction.delete(LogicalDatastoreType.OPERATIONAL, path);
55     }
56
57     @Override
58     public <T extends DataObject> void merge(final InstanceIdentifier<T> path, T data) {
59         readWriteTransaction.merge(LogicalDatastoreType.OPERATIONAL, path, data, true);
60     }
61
62     @Override
63     public <T extends DataObject> void put(final InstanceIdentifier<T> path, T data) {
64         readWriteTransaction.put(LogicalDatastoreType.OPERATIONAL, path, data, true);
65     }
66
67     public CheckedFuture<Void, TransactionCommitFailedException> submit() {
68         return readWriteTransaction.submit();
69     }
70
71     @Override
72     public void close() {
73         readWriteTransaction.cancel();
74     }
75 }