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