481a7ddfa2c42863b3cb17b17826d0a5748fa778
[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.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29
30 // FIXME: Migrate to use new Data Broker APIs
31 @SuppressWarnings("deprecation")
32 public class BrokerIntegrationTest extends AbstractDataServiceTest {
33
34     @Test
35     public void simpleModifyOperation() throws Exception {
36
37         NodeRef node1 = createNodeRef("0");
38         DataObject node = baDataService.readConfigurationData(node1.getValue());
39         assertNull(node);
40         Node nodeData1 = createNode("0");
41
42         DataModificationTransaction transaction = baDataService.beginTransaction();
43         transaction.putConfigurationData(node1.getValue(), nodeData1);
44         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
45         assertNotNull(commitResult);
46
47         RpcResult<TransactionStatus> result = commitResult.get();
48
49         assertNotNull(result);
50         assertNotNull(result.getResult());
51         assertEquals(TransactionStatus.COMMITED, result.getResult());
52
53         Node readedData = (Node) baDataService.readConfigurationData(node1.getValue());
54         assertNotNull(readedData);
55         assertEquals(nodeData1.getKey(), readedData.getKey());
56
57         NodeRef nodeFoo = createNodeRef("foo");
58         NodeRef nodeBar = createNodeRef("bar");
59         Node nodeFooData = createNode("foo");
60         Node nodeBarData = createNode("bar");
61
62         DataModificationTransaction insertMoreTr = baDataService.beginTransaction();
63         insertMoreTr.putConfigurationData(nodeFoo.getValue(), nodeFooData);
64         insertMoreTr.putConfigurationData(nodeBar.getValue(), nodeBarData);
65         RpcResult<TransactionStatus> result2 = insertMoreTr.commit().get();
66
67         assertNotNull(result2);
68         assertNotNull(result2.getResult());
69         assertEquals(TransactionStatus.COMMITED, result.getResult());
70
71         Nodes allNodes = (Nodes) baDataService.readConfigurationData(InstanceIdentifier.builder(Nodes.class)
72                 .toInstance());
73         assertNotNull(allNodes);
74         assertNotNull(allNodes.getNode());
75         assertEquals(3, allNodes.getNode().size());
76
77         /**
78          * We create transaction no 2
79          *
80          */
81         DataModificationTransaction removalTransaction = baDataService.beginTransaction();
82         assertNotNull(transaction);
83
84         /**
85          * We remove node 1
86          *
87          */
88         removalTransaction.removeConfigurationData(node1.getValue());
89
90         /**
91          * We commit transaction
92          */
93         Future<RpcResult<TransactionStatus>> commitResult2 = removalTransaction.commit();
94         assertNotNull(commitResult2);
95
96         RpcResult<TransactionStatus> result3 = commitResult2.get();
97
98         assertNotNull(result3);
99         assertNotNull(result3.getResult());
100         assertEquals(TransactionStatus.COMMITED, result2.getResult());
101
102         DataObject readedData2 = baDataService.readConfigurationData(node1.getValue());
103         assertNull(readedData2);
104     }
105
106     private static NodeRef createNodeRef(final String string) {
107         NodeKey key = new NodeKey(new NodeId(string));
108         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class).child(Node.class, key)
109                 .toInstance();
110         return new NodeRef(path);
111     }
112
113     private static Node createNode(final String string) {
114         NodeBuilder ret = new NodeBuilder();
115         ret.setId(new NodeId(string));
116         ret.setKey(new NodeKey(ret.getId()));
117         return ret.build();
118     }
119 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.