Streamline updating out-of-sync follower
[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.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 import com.google.inject.Inject;
33
34 /**
35  * covers creating, reading and deleting of an item in dataStore
36  */
37 public class DataServiceTest extends AbstractTest {
38
39     protected DataBrokerService consumerDataService;
40
41     @Inject
42     Broker broker2;
43
44     /**
45      *
46      * Ignored this, because classes here are constructed from
47      * very different class loader as MD-SAL is run into,
48      * this is code is run from different classloader.
49      *
50      * @throws Exception
51      */
52     @Test
53     public void test() throws Exception {
54         BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
55
56             @Override
57             public void onSessionInitialized(final ConsumerContext session) {
58                 consumerDataService = session.getSALService(DataBrokerService.class);
59             }
60         };
61         broker.registerConsumer(consumer1);
62
63         assertNotNull(consumerDataService);
64
65
66         DataModificationTransaction transaction = consumerDataService.beginTransaction();
67         assertNotNull(transaction);
68
69         InstanceIdentifier<UnorderedList> node1 = createNodeRef("0");
70         DataObject node = consumerDataService.readConfigurationData(node1);
71         assertNull(node);
72         UnorderedList nodeData1 = createNode("0");
73
74         transaction.putConfigurationData(node1, nodeData1);
75         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
76         assertNotNull(commitResult);
77
78         RpcResult<TransactionStatus> result = commitResult.get();
79
80         assertNotNull(result);
81         assertNotNull(result.getResult());
82         assertEquals(TransactionStatus.COMMITED, result.getResult());
83
84         UnorderedList readedData = (UnorderedList) consumerDataService.readConfigurationData(node1);
85         assertNotNull(readedData);
86         assertEquals(nodeData1.getKey(), readedData.getKey());
87
88
89         DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
90         assertNotNull(transaction2);
91
92         transaction2.removeConfigurationData(node1);
93
94         Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
95         assertNotNull(commitResult2);
96
97         RpcResult<TransactionStatus> result2 = commitResult2.get();
98
99         assertNotNull(result2);
100         assertNotNull(result2.getResult());
101         assertEquals(TransactionStatus.COMMITED, result2.getResult());
102
103         DataObject readedData2 = consumerDataService.readConfigurationData(node1);
104         assertNull(readedData2);
105     }
106
107
108     private static InstanceIdentifier<UnorderedList> createNodeRef(final String string) {
109         UnorderedListKey key = new UnorderedListKey(string);
110         return  InstanceIdentifier.builder(Lists.class).child(UnorderedContainer.class).child(UnorderedList.class, key).build();
111     }
112
113     private static UnorderedList createNode(final String string) {
114         UnorderedListBuilder ret = new UnorderedListBuilder();
115         UnorderedListKey nodeKey = new UnorderedListKey(string);
116         ret.setKey(nodeKey);
117         ret.setName("name of " + string);
118         ret.setName("value of " + string);
119         return ret.build();
120     }
121 }