33039ea2314329e0a132606bff58894e7cb84585
[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 com.google.inject.Inject;
15 import java.util.concurrent.Future;
16 import org.junit.Before;
17 import org.junit.Ignore;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
21 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
22 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
23 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
24 import org.opendaylight.controller.sal.core.api.Broker;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
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 DataServiceTest extends AbstractTest {
35
36     protected DataBrokerService consumerDataService;
37
38
39     @Inject
40     Broker broker2;
41
42     @Before
43     public void setUp() throws Exception {
44     }
45
46     /*
47      *
48      * Ignored this, because classes here are constructed from
49      * very different class loader as MD-SAL is run into,
50      * this is code is run from different classloader.
51      *
52      */
53     @Test
54     @Ignore
55     public void test() throws Exception {
56         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
57
58             @Override
59             public void onSessionInitialized(final ConsumerContext session) {
60                 consumerDataService = session.getSALService(DataBrokerService.class);
61             }
62         };
63         broker.registerConsumer(consumer1, getBundleContext());
64
65         assertNotNull(consumerDataService);
66
67
68         DataModificationTransaction transaction = consumerDataService.beginTransaction();
69         assertNotNull(transaction);
70
71         InstanceIdentifier<Node> node1 = createNodeRef("0");
72         DataObject  node = consumerDataService.readConfigurationData(node1);
73         assertNull(node);
74         Node nodeData1 = createNode("0");
75
76         transaction.putConfigurationData(node1, nodeData1);
77         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
78         assertNotNull(commitResult);
79
80         RpcResult<TransactionStatus> result = commitResult.get();
81
82         assertNotNull(result);
83         assertNotNull(result.getResult());
84         assertEquals(TransactionStatus.COMMITED, result.getResult());
85
86         Node readedData = (Node) consumerDataService.readConfigurationData(node1);
87         assertNotNull(readedData);
88         assertEquals(nodeData1.getKey(), readedData.getKey());
89
90
91         DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
92         assertNotNull(transaction);
93
94         transaction2.removeConfigurationData(node1);
95
96         Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
97         assertNotNull(commitResult2);
98
99         RpcResult<TransactionStatus> result2 = commitResult2.get();
100
101         assertNotNull(result2);
102         assertNotNull(result2.getResult());
103         assertEquals(TransactionStatus.COMMITED, result2.getResult());
104
105         DataObject readedData2 = consumerDataService.readConfigurationData(node1);
106         assertNull(readedData2);
107
108
109     }
110
111
112     private static InstanceIdentifier<Node> createNodeRef(final String string) {
113         NodeKey key = new NodeKey(new NodeId(string));
114         return  InstanceIdentifier.builder(Nodes.class).child(Node.class, key).build();
115     }
116
117     private static Node createNode(final String string) {
118         NodeBuilder ret = new NodeBuilder();
119         NodeId id = new NodeId(string);
120         ret.setKey(new NodeKey(id));
121         ret.setId(id);
122         return ret.build();
123     }
124 }

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