285686a1193f09510279ce52d8e1c06d040bdec0
[netvirt.git] / vpnservice / 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.controller.md.sal.binding.api.DataTreeIdentifier;
13 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
16 import org.opendaylight.yangtools.yang.binding.DataObject;
17 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
18
19 /**
20  * Utility around {@link WriteTransaction} for working with pairs and tuples of Type/Id/DataObject.
21  *
22  * <ul><li>{@link Pair}s (TODO or {@link java.util.Map.Entry} ?) of {@link InstanceIdentifier}s and
23  * {@link DataObject}s
24  * <li>Triples of
25  * {@link LogicalDatastoreType}s/InstanceIdentifiers/DataObjects
26  * <li>{@link DataTreeIdentifierDataObjectPairBuilder}
27  * </ul>
28  *
29  * @author Michael Vorburger
30  */
31 public class DataBrokerPairsUtil {
32     // TODO use, when merged, pending https://git.opendaylight.org/gerrit/#/c/46534/ (and https://git.opendaylight.org/gerrit/#/c/46479/)
33
34     private final WriteTransaction tx;
35
36     @Inject
37     public DataBrokerPairsUtil(WriteTransaction tx) {
38         super();
39         this.tx = tx;
40     }
41
42     public <T extends DataObject> void put(LogicalDatastoreType type, Pair<InstanceIdentifier<T>, T> pair)
43             throws TransactionCommitFailedException {
44         tx.put(type, pair.getKey(), pair.getValue(), true);
45     }
46
47     public <T extends DataObject> void put(Pair<DataTreeIdentifier<T>, T> pair)
48             throws TransactionCommitFailedException {
49         tx.put(pair.getKey().getDatastoreType(), pair.getKey().getRootIdentifier(), pair.getValue(), true);
50     }
51
52     public <T extends DataObject> void put(DataTreeIdentifierDataObjectPairBuilder<T> builder)
53             throws TransactionCommitFailedException {
54         put(builder.build());
55     }
56
57 }