NETVIRT-1630 migrate to md-sal APIs
[netvirt.git] / aclservice / impl / src / test / java / org / opendaylight / netvirt / aclservice / tests / infra / DataBrokerPairsUtil.java
1 /*
2  * Copyright (c) 2016 Red Hat, Inc. and others. All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.netvirt.aclservice.tests.infra;
9
10 import javax.inject.Inject;
11 import org.eclipse.xtext.xbase.lib.Pair;
12 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
13 import org.opendaylight.mdsal.binding.api.DataBroker;
14 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
15 import org.opendaylight.mdsal.binding.api.WriteTransaction;
16 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
17 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
18 import org.opendaylight.yangtools.yang.binding.DataObject;
19 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
20
21 /**
22  * Utility around {@link WriteTransaction} for working with pairs and tuples of Type/Id/DataObject.
23  *
24  * <ul><li>{@link Pair}s (TODO or {@link java.util.Map.Entry} ?) of {@link InstanceIdentifier}s and
25  * {@link DataObject}s
26  * <li>Triples of
27  * {@link LogicalDatastoreType}s/InstanceIdentifiers/DataObjects
28  * <li>{@link DataTreeIdentifierDataObjectPairBuilder}
29  * </ul>
30  *
31  * @author Michael Vorburger
32  */
33 public class DataBrokerPairsUtil {
34     // TODO use, when merged, pending https://git.opendaylight.org/gerrit/#/c/46534/ (and https://git.opendaylight.org/gerrit/#/c/46479/)
35
36     private final SingleTransactionDataBroker singleTxDB;
37
38     @Inject
39     public DataBrokerPairsUtil(DataBroker db) {
40         this.singleTxDB = new SingleTransactionDataBroker(db);
41     }
42
43     public <T extends DataObject> void put(LogicalDatastoreType type, Pair<InstanceIdentifier<T>, T> pair)
44             throws TransactionCommitFailedException {
45         singleTxDB.syncWrite(type, pair.getKey(), pair.getValue());
46     }
47
48     public <T extends DataObject> void put(Pair<DataTreeIdentifier<T>, T> pair)
49             throws TransactionCommitFailedException {
50         singleTxDB.syncWrite(pair.getKey().getDatastoreType(), pair.getKey().getRootIdentifier(), pair.getValue());
51     }
52
53     public <T extends DataObject> void put(DataTreeIdentifierDataObjectPairBuilder<T> builder)
54             throws TransactionCommitFailedException {
55         put(builder.build());
56     }
57
58 }