Merge "Unified Two Phase Commit implementation, fixed BA to BI connection"
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / sal / binding / test / connect / dom / BrokerIntegrationTest.java
1 package org.opendaylight.controller.sal.binding.test.connect.dom;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertNull;
6
7 import java.util.concurrent.Executors;
8 import java.util.concurrent.Future;
9
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
13 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
14 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
15 import org.opendaylight.controller.sal.binding.impl.DataBrokerImpl;
16 import org.opendaylight.controller.sal.binding.impl.connect.dom.BindingIndependentDataServiceConnector;
17 import org.opendaylight.controller.sal.binding.impl.connect.dom.MappingServiceImpl;
18 import org.opendaylight.controller.sal.core.api.data.DataBrokerService;
19 import org.opendaylight.controller.sal.dom.broker.impl.HashMapDataStore;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.opendaylight.yangtools.yang.common.RpcResult;
29
30 import com.google.common.util.concurrent.ListeningExecutorService;
31 import com.google.common.util.concurrent.MoreExecutors;
32
33 public class BrokerIntegrationTest {
34
35     DataBrokerService biDataService;
36     DataProviderService baDataService;
37     private MappingServiceImpl mappingServiceImpl;
38     private MappingServiceImpl mappingService;
39     private DataBrokerImpl baDataImpl;
40     private org.opendaylight.controller.sal.dom.broker.DataBrokerImpl biDataImpl;
41     private ListeningExecutorService executor;
42     private BindingIndependentDataServiceConnector connectorServiceImpl;
43     private HashMapDataStore dataStore;
44     
45     
46     @Before
47     public void setUp() {
48         executor = MoreExecutors.sameThreadExecutor();
49         baDataImpl = new DataBrokerImpl();
50         baDataService = baDataImpl;
51         baDataImpl.setExecutor(executor);
52         
53         biDataImpl = new org.opendaylight.controller.sal.dom.broker.DataBrokerImpl();
54         biDataService =  biDataImpl;
55         biDataImpl.setExecutor(executor);
56         
57         dataStore = new HashMapDataStore();
58         org.opendaylight.yangtools.yang.data.api.InstanceIdentifier treeRoot = org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder().toInstance();
59         biDataImpl.registerConfigurationReader(treeRoot, dataStore);
60         biDataImpl.registerOperationalReader(treeRoot, dataStore);
61         biDataImpl.registerCommitHandler(treeRoot, dataStore);
62         
63         mappingServiceImpl = new MappingServiceImpl();
64         mappingService = mappingServiceImpl;
65         
66         
67         connectorServiceImpl = new BindingIndependentDataServiceConnector();
68         connectorServiceImpl.setBaDataService(baDataService);
69         connectorServiceImpl.setBiDataService(biDataService);
70         connectorServiceImpl.setMappingService(mappingServiceImpl);
71         connectorServiceImpl.start();
72         
73         String[] yangFiles = new String[] { "yang-ext.yang", "ietf-inet-types.yang", "ietf-yang-types.yang",
74         "node-inventory.yang" };
75         
76         mappingService.onGlobalContextUpdated(MappingServiceTest.getContext(yangFiles));
77     }
78     
79     @Test
80     public void simpleModifyOperation() throws Exception {
81         
82         DataModificationTransaction transaction = baDataService.beginTransaction();
83         assertNotNull(transaction);
84         
85         NodeRef node1 = createNodeRef("0");
86         DataObject  node = baDataService.readConfigurationData(node1.getValue());
87         assertNull(node);
88         Node nodeData1 = createNode("0");
89         
90         transaction.putConfigurationData(node1.getValue(), nodeData1);
91         Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
92         assertNotNull(commitResult);
93         
94         RpcResult<TransactionStatus> result = commitResult.get();
95         
96         assertNotNull(result);
97         assertNotNull(result.getResult());
98         assertEquals(TransactionStatus.COMMITED, result.getResult());
99         
100         Node readedData = (Node) baDataService.readConfigurationData(node1.getValue());
101         assertNotNull(readedData);
102         assertEquals(nodeData1.getKey(), readedData.getKey());
103         
104         
105         DataModificationTransaction transaction2 = baDataService.beginTransaction();
106         assertNotNull(transaction);
107         
108         transaction2.removeConfigurationData(node1.getValue());
109         
110         Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
111         assertNotNull(commitResult2);
112         
113         RpcResult<TransactionStatus> result2 = commitResult2.get();
114         
115         assertNotNull(result2);
116         assertNotNull(result2.getResult());
117         assertEquals(TransactionStatus.COMMITED, result2.getResult());
118     
119         DataObject readedData2 = baDataService.readConfigurationData(node1.getValue());
120         assertNull(readedData2);
121     }
122     
123     private static NodeRef createNodeRef(String string) {
124         NodeKey key = new NodeKey(new NodeId(string));
125         InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
126                 .toInstance();
127         return new NodeRef(path);
128     }
129     
130     private static Node createNode(String string) {
131         NodeBuilder ret = new NodeBuilder();
132         ret.setId(new NodeId(string));
133         ret.setKey(new NodeKey(ret.getId()));
134         return ret.build();
135     }
136 }