Merge "Bug 2731: Discard changes only when transaction exist."
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / connect / dom / BrokerIntegrationTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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.controller.sal.binding.test.connect.dom;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13
14 import java.util.concurrent.Future;
15
16 import org.junit.Test;
17 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
18 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
19 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.Top;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.list.rev140701.two.level.list.TopLevelListKey;
24 import org.opendaylight.yangtools.yang.binding.DataObject;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27
28 /**
29  * FIXME: Migrate to use new Data Broker APIs
30  */
31 @SuppressWarnings("deprecation")
32 public class BrokerIntegrationTest extends AbstractDataServiceTest {
33
34     private static final TopLevelListKey TLL_FOO_KEY = new TopLevelListKey("foo");
35     private static final TopLevelListKey TLL_BAR_KEY = new TopLevelListKey("bar");
36     private static final TopLevelListKey TLL_BAZ_KEY = new TopLevelListKey("baz");
37     private static final InstanceIdentifier<Top> TOP_PATH = InstanceIdentifier.builder(Top.class).build();
38     private static final InstanceIdentifier<TopLevelList> FOO_PATH = TOP_PATH.child(TopLevelList.class, TLL_FOO_KEY);
39     private static final InstanceIdentifier<TopLevelList> BAR_PATH = TOP_PATH.child(TopLevelList.class, TLL_BAR_KEY);
40     private static final InstanceIdentifier<TopLevelList> BAZ_PATH = TOP_PATH.child(TopLevelList.class, TLL_BAZ_KEY);
41
42     @Test
43     public void simpleModifyOperation() throws Exception {
44
45         DataObject tllFoo = baDataService.readConfigurationData(FOO_PATH);
46         assertNull(tllFoo);
47         TopLevelList tllFooData = createTll(TLL_FOO_KEY);
48
49         DataModificationTransaction transaction = baDataService.beginTransaction();
50         transaction.putConfigurationData(FOO_PATH, tllFooData);
51         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
52         assertNotNull(commitResult);
53
54         RpcResult<TransactionStatus> result = commitResult.get();
55
56         assertNotNull(result);
57         assertNotNull(result.getResult());
58         assertEquals(TransactionStatus.COMMITED, result.getResult());
59
60         TopLevelList readedData = (TopLevelList) baDataService.readConfigurationData(FOO_PATH);
61         assertNotNull(readedData);
62         assertEquals(tllFooData.getKey(), readedData.getKey());
63
64         TopLevelList nodeBarData = createTll(TLL_BAR_KEY);
65         TopLevelList nodeBazData = createTll(TLL_BAZ_KEY);
66
67         DataModificationTransaction insertMoreTr = baDataService.beginTransaction();
68         insertMoreTr.putConfigurationData(BAR_PATH, nodeBarData);
69         insertMoreTr.putConfigurationData(BAZ_PATH, nodeBazData);
70         RpcResult<TransactionStatus> result2 = insertMoreTr.commit().get();
71
72         assertNotNull(result2);
73         assertNotNull(result2.getResult());
74         assertEquals(TransactionStatus.COMMITED, result.getResult());
75
76         Top top = (Top) baDataService.readConfigurationData(TOP_PATH);
77         assertNotNull(top);
78         assertNotNull(top.getTopLevelList());
79         assertEquals(3, top.getTopLevelList().size());
80
81         /**
82          * We create transaction no 2
83          *
84          */
85         DataModificationTransaction removalTransaction = baDataService.beginTransaction();
86         assertNotNull(transaction);
87
88         /**
89          * We remove node 1
90          *
91          */
92         removalTransaction.removeConfigurationData(BAR_PATH);
93
94         /**
95          * We commit transaction
96          */
97         Future<RpcResult<TransactionStatus>> commitResult2 = removalTransaction.commit();
98         assertNotNull(commitResult2);
99
100         RpcResult<TransactionStatus> result3 = commitResult2.get();
101
102         assertNotNull(result3);
103         assertNotNull(result3.getResult());
104         assertEquals(TransactionStatus.COMMITED, result2.getResult());
105
106         DataObject readedData2 = baDataService.readConfigurationData(BAR_PATH);
107         assertNull(readedData2);
108     }
109
110     private static TopLevelList createTll(final TopLevelListKey key) {
111         TopLevelListBuilder ret = new TopLevelListBuilder();
112         ret.setKey(key);
113         return ret.build();
114     }
115 }