b73cb421986ef0018a7e825dffcdb3df38ce1bf5
[controller.git] / opendaylight / md-sal / sal-binding-it / src / test / java / org / opendaylight / controller / test / sal / binding / it / DataServiceIT.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 javax.inject.Inject;
15 import java.util.concurrent.Future;
16 import org.junit.Test;
17 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
20 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
21 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
22 import org.opendaylight.controller.sal.core.api.Broker;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.Lists;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.UnorderedContainer;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedList;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.store.rev140422.lists.unordered.container.UnorderedListKey;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31
32 /**
33  * covers creating, reading and deleting of an item in dataStore
34  */
35 public class DataServiceIT extends AbstractIT {
36
37     protected DataBrokerService consumerDataService;
38
39     @Inject
40     Broker broker2;
41
42     /**
43      *
44      * Ignored this, because classes here are constructed from
45      * very different class loader as MD-SAL is run into,
46      * this is code is run from different classloader.
47      *
48      * @throws Exception
49      */
50     @Test
51     public void test() throws Exception {
52         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
53
54             @Override
55             public void onSessionInitialized(final ConsumerContext session) {
56                 consumerDataService = session.getSALService(DataBrokerService.class);
57             }
58         };
59         broker.registerConsumer(consumer1);
60
61         assertNotNull(consumerDataService);
62
63
64         DataModificationTransaction transaction = consumerDataService.beginTransaction();
65         assertNotNull(transaction);
66
67         InstanceIdentifier<UnorderedList> node1 = createNodeRef("0");
68         DataObject node = consumerDataService.readConfigurationData(node1);
69         assertNull(node);
70         UnorderedList nodeData1 = createNode("0");
71
72         transaction.putConfigurationData(node1, nodeData1);
73         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
74         assertNotNull(commitResult);
75
76         RpcResult<TransactionStatus> result = commitResult.get();
77
78         assertNotNull(result);
79         assertNotNull(result.getResult());
80         assertEquals(TransactionStatus.COMMITED, result.getResult());
81
82         UnorderedList readedData = (UnorderedList) consumerDataService.readConfigurationData(node1);
83         assertNotNull(readedData);
84         assertEquals(nodeData1.getKey(), readedData.getKey());
85
86         DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
87         assertNotNull(transaction2);
88
89         transaction2.removeConfigurationData(node1);
90
91         Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
92         assertNotNull(commitResult2);
93
94         RpcResult<TransactionStatus> result2 = commitResult2.get();
95
96         assertNotNull(result2);
97         assertNotNull(result2.getResult());
98         assertEquals(TransactionStatus.COMMITED, result2.getResult());
99
100         DataObject readedData2 = consumerDataService.readConfigurationData(node1);
101         assertNull(readedData2);
102     }
103
104
105     private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
106         UnorderedListKey key = new UnorderedListKey(string);
107         return  InstanceIdentifier.builder(Lists.class).child(UnorderedContainer.class).child(UnorderedList.class, key).build();
108     }
109
110     private static UnorderedList createNode(final String string) {
111         UnorderedListBuilder ret = new UnorderedListBuilder();
112         UnorderedListKey nodeKey = new UnorderedListKey(string);
113         ret.setKey(nodeKey);
114         ret.setName("name of " + string);
115         ret.setName("value of " + string);
116         return ret.build();
117     }
118 }