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.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
14 import java.util.concurrent.Future;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
19 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
20 import org.opendaylight.controller.sal.binding.api.BindingAwareConsumer;
21 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
22 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
23 import org.opendaylight.controller.sal.core.api.Broker;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
30 import org.opendaylight.yangtools.yang.binding.DataObject;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import com.google.inject.Inject;
36 public class DataServiceTest extends AbstractTest {
38 protected DataBrokerService consumerDataService;
45 public void setUp() throws Exception {
49 public void test() throws Exception {
50 BindingAwareConsumer consumer1 = new BindingAwareConsumer() {
53 public void onSessionInitialized(ConsumerContext session) {
54 consumerDataService = session.getSALService(DataBrokerService.class);
57 broker.registerConsumer(consumer1, getBundleContext());
59 assertNotNull(consumerDataService);
62 DataModificationTransaction transaction = consumerDataService.beginTransaction();
63 assertNotNull(transaction);
65 NodeRef node1 = createNodeRef("0");
66 DataObject node = consumerDataService.readConfigurationData(node1.getValue());
68 Node nodeData1 = createNode("0");
70 transaction.putConfigurationData(node1.getValue(), nodeData1);
71 Future<RpcResult<TransactionStatus>> commitResult = transaction.commit();
72 assertNotNull(commitResult);
74 RpcResult<TransactionStatus> result = commitResult.get();
76 assertNotNull(result);
77 assertNotNull(result.getResult());
78 assertEquals(TransactionStatus.COMMITED, result.getResult());
80 Node readedData = (Node) consumerDataService.readConfigurationData(node1.getValue());
81 assertNotNull(readedData);
82 assertEquals(nodeData1.getKey(), readedData.getKey());
85 DataModificationTransaction transaction2 = consumerDataService.beginTransaction();
86 assertNotNull(transaction);
88 transaction2.removeConfigurationData(node1.getValue());
90 Future<RpcResult<TransactionStatus>> commitResult2 = transaction2.commit();
91 assertNotNull(commitResult2);
93 RpcResult<TransactionStatus> result2 = commitResult2.get();
95 assertNotNull(result2);
96 assertNotNull(result2.getResult());
97 assertEquals(TransactionStatus.COMMITED, result2.getResult());
99 DataObject readedData2 = consumerDataService.readConfigurationData(node1.getValue());
100 assertNull(readedData2);
106 private static NodeRef createNodeRef(String string) {
107 NodeKey key = new NodeKey(new NodeId(string));
108 InstanceIdentifier<Node> path = InstanceIdentifier.builder(Nodes.class).child(Node.class, key).build();
110 return new NodeRef(path);
113 private static Node createNode(String string) {
114 NodeBuilder ret = new NodeBuilder();
115 NodeId id = new NodeId(string);
116 ret.setKey(new NodeKey(id));