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

©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.