2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.test.sal.binding.it;
10 import static org.junit.Assert.*;
12 import java.util.concurrent.Future;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
19 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
20 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
21 import org.opendaylight.controller.sal.core.api.Broker;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
28 import org.opendaylight.yangtools.yang.binding.DataObject;
29 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
32 import com.google.inject.Inject;
34 public class DataServiceTest extends AbstractTest {
36 protected DataBrokerService consumerDataService;
43 public void setUp() throws Exception {
47 public void test() throws Exception {
48 BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
51 public void onSessionInitialized(ConsumerContext session) {
52 consumerDataService = session.getSALService(DataBrokerService.class);
55 broker.registerConsumer(consumer1, getBundleContext());
57 assertNotNull(consumerDataService);
60 DataModificationTransaction transaction = consumerDataService.beginTransaction();
61 assertNotNull(transaction);
63 NodeRef node1 = createNodeRef("0");
64 DataObject node = consumerDataService.readConfigurationData(node1.getValue());
66 Node nodeData1 = createNode("0");
68 transaction.putConfigurationData(node1.getValue(), nodeData1);
69 Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
70 assertNotNull(commitResult);
72 RpcResult<TransactionStatus> result = commitResult.get();
74 assertNotNull(result);
75 assertNotNull(result.getResult());
76 assertEquals(TransactionStatus.COMMITED, result.getResult());
78 Node readedData = (Node) consumerDataService.readConfigurationData(node1.getValue());
79 assertNotNull(readedData);
80 assertEquals(nodeData1.getKey(), readedData.getKey());
83 DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
84 assertNotNull(transaction);
86 transaction2.removeConfigurationData(node1.getValue());
88 Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
89 assertNotNull(commitResult2);
91 RpcResult<TransactionStatus> result2 = commitResult2.get();
93 assertNotNull(result2);
94 assertNotNull(result2.getResult());
95 assertEquals(TransactionStatus.COMMITED, result2.getResult());
97 DataObject readedData2 = consumerDataService.readConfigurationData(node1.getValue());
98 assertNull(readedData2);
104 private static NodeRef createNodeRef(String string) {
105 NodeKey key = new NodeKey(new NodeId(string));
106 InstanceIdentifier<Node> path = InstanceIdentifier.builder().node(Nodes.class).node(Node.class, key)
109 return new NodeRef(path);
112 private static Node createNode(String string) {
113 NodeBuilder ret = new NodeBuilder();
114 NodeId id = new NodeId(string);
115 ret.setKey(new NodeKey(id));