Merge "Adding more unit tests for remote rpc connector and Integrating routing table"
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / DataServiceTest.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.test.sal.binding.it;
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.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
21 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
22 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
23 import org.opendaylight.controller.sal.core.api.Broker;
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 import com.google.inject.Inject;
35
36 public class DataServiceTest extends AbstractTest {
37
38     protected DataBrokerService consumerDataService;
39
40
41     @Inject
42     Broker broker2;
43
44     @Before
45     public void setUp() throws Exception {
46     }
47
48     @Test
49     public void test() throws Exception {
50         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
51
52             @Override
53             public void onSessionInitialized(ConsumerContext session) {
54                 consumerDataService = session.getSALService(DataBrokerService.class);
55             }
56         };
57         broker.registerConsumer(consumer1, getBundleContext());
58
59         assertNotNull(consumerDataService);
60
61
62         DataModificationTransaction transaction = consumerDataService.beginTransaction();
63         assertNotNull(transaction);
64
65         NodeRef node1 = createNodeRef("0");
66         DataObject  node = consumerDataService.readConfigurationData(node1.getValue());
67         assertNull(node);
68         Node nodeData1 = createNode("0");
69
70         transaction.putConfigurationData(node1.getValue(), nodeData1);
71         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
72         assertNotNull(commitResult);
73
74         RpcResult<TransactionStatus> result = commitResult.get();
75
76         assertNotNull(result);
77         assertNotNull(result.getResult());
78         assertEquals(TransactionStatus.COMMITED, result.getResult());
79
80         Node readedData = (Node) consumerDataService.readConfigurationData(node1.getValue());
81         assertNotNull(readedData);
82         assertEquals(nodeData1.getKey(), readedData.getKey());
83
84
85         DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
86         assertNotNull(transaction);
87
88         transaction2.removeConfigurationData(node1.getValue());
89
90         Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
91         assertNotNull(commitResult2);
92
93         RpcResult<TransactionStatus> result2 = commitResult2.get();
94
95         assertNotNull(result2);
96         assertNotNull(result2.getResult());
97         assertEquals(TransactionStatus.COMMITED, result2.getResult());
98
99         DataObject readedData2 = consumerDataService.readConfigurationData(node1.getValue());
100         assertNull(readedData2);
101
102
103     }
104
105
106     private static NodeRef createNodeRef(String string) {
107         NodeKey key = new NodeKey(new NodeId(string));
108         InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).build();
109
110         return new NodeRef(path);
111     }
112
113     private static Node createNode(String string) {
114         NodeBuilder ret = new NodeBuilder();
115         NodeId id = new NodeId(string);
116         ret.setKey(new NodeKey(id));
117         ret.setId(id);
118         return ret.build();
119     }
120 }