BUG-648: Improve performance by reusing old data nodes
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / bugfix / WriteParentReadChildTest.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
9 package org.opendaylight.controller.sal.binding.test.bugfix;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
14
15 import org.junit.Test;
16 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
17 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
18 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
37 import org.opendaylight.yangtools.yang.binding.DataObject;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39 import org.opendaylight.yangtools.yang.common.RpcResult;
40
41 import com.google.common.collect.ImmutableList;
42
43 public class WriteParentReadChildTest extends AbstractDataServiceTest {
44
45     private static final String FLOW_ID = "1234";
46     private static final short TABLE_ID = (short) 0;
47     private static final String NODE_ID = "node:1";
48
49     private static final NodeKey NODE_KEY = new NodeKey(new NodeId(NODE_ID));
50     private static final FlowKey FLOW_KEY = new FlowKey(new FlowId(FLOW_ID));
51     private static final TableKey TABLE_KEY = new TableKey(TABLE_ID);
52
53     private static final InstanceIdentifier<Node> NODE_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
54             .child(Node.class, NODE_KEY).toInstance();
55
56     private static final InstanceIdentifier<Table> TABLE_INSTANCE_ID_BA = //
57     InstanceIdentifier.builder(NODE_INSTANCE_ID_BA) //
58             .augmentation(FlowCapableNode.class).child(Table.class, TABLE_KEY).build();
59
60     private static final InstanceIdentifier<? extends DataObject> FLOW_INSTANCE_ID_BA = //
61     InstanceIdentifier.builder(TABLE_INSTANCE_ID_BA) //
62             .child(Flow.class, FLOW_KEY) //
63             .toInstance();
64     /**
65      *
66      * The scenario tests writing parent node, which also contains child items
67      * and then reading child directly, by specifying path to the child.
68      *
69      * Expected behaviour is child is returned.
70      *
71      * @throws Exception
72      */
73     @Test
74     public void writeTableReadFlow() throws Exception {
75
76         DataModificationTransaction modification = baDataService.beginTransaction();
77
78         Flow flow = new FlowBuilder() //
79                 .setKey(FLOW_KEY) //
80                 .setMatch(new MatchBuilder() //
81                         .setVlanMatch(new VlanMatchBuilder() //
82                                 .setVlanId(new VlanIdBuilder() //
83                                         .setVlanId(new VlanId(10)) //
84                                         .build()) //
85                                 .build()) //
86                         .build()) //
87                         .setInstructions(new InstructionsBuilder() //
88                             .setInstruction(ImmutableList.<Instruction>builder() //
89                                     .build()) //
90                         .build()) //
91                 .build();
92
93         Table table = new TableBuilder()
94             .setKey(TABLE_KEY)
95             .setFlow(ImmutableList.of(flow))
96         .build();
97
98         modification.putConfigurationData(TABLE_INSTANCE_ID_BA, table);
99         RpcResult<TransactionStatus> ret = modification.commit().get();
100         assertNotNull(ret);
101         assertEquals(TransactionStatus.COMMITED, ret.getResult());
102
103         DataObject readedTable = baDataService.readConfigurationData(TABLE_INSTANCE_ID_BA);
104         assertNotNull("Readed table should not be nul.", readedTable);
105         assertTrue(readedTable instanceof Table);
106         
107         DataObject readedFlow = baDataService.readConfigurationData(FLOW_INSTANCE_ID_BA);
108         assertNotNull("Readed flow should not be null.",readedFlow);
109         assertTrue(readedFlow instanceof Flow);
110         assertEquals(flow, readedFlow);
111
112     }
113 }