Merge "Bug 849: Fixed NPE in Translated Data Change Events."
[controller.git] / opendaylight / md-sal / sal-binding-dom-it / src / test / java / org / opendaylight / controller / sal / binding / test / bugfix / FlagsSerializationTest.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.sal.binding.test.bugfix;
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.ArrayList;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.junit.Test;
20 import org.opendaylight.controller.md.sal.common.api.TransactionStatus;
21 import org.opendaylight.controller.sal.binding.api.data.DataModificationTransaction;
22 import org.opendaylight.controller.sal.binding.test.AbstractDataServiceTest;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.mpls.action._case.PopMplsActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
49 import org.opendaylight.yangtools.yang.binding.DataObject;
50 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.common.RpcResult;
53 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
54
55 import com.google.common.collect.ImmutableSet;
56
57 public class FlagsSerializationTest extends AbstractDataServiceTest {
58
59     private static final QName NODE_ID_QNAME = QName.create(Node.QNAME, "id");
60     private static final QName FLOW_ID_QNAME = QName.create(Flow.QNAME, "id");
61     private static final QName FLOW_NODE_QNAME = QName.create(Flow.QNAME, "node");
62     private static final String FLOW_ID = "1234";
63     private static final short TABLE_ID = (short)0;
64     private static final String NODE_ID = "node:1";
65
66     private static final NodeKey NODE_KEY = new NodeKey(new NodeId(NODE_ID));
67     private static final FlowKey FLOW_KEY = new FlowKey(new FlowId(FLOW_ID));
68     private static final TableKey TABLE_KEY = new TableKey(TABLE_ID);
69
70     private static final Map<QName, Object> NODE_KEY_BI = Collections.<QName, Object> singletonMap(NODE_ID_QNAME,
71             NODE_ID);
72
73     private static final InstanceIdentifier<Node> NODE_INSTANCE_ID_BA = InstanceIdentifier.builder(Nodes.class) //
74             .child(Node.class, NODE_KEY).toInstance();
75
76     private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier NODE_INSTANCE_ID_BI = //
77     org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
78             .node(Nodes.QNAME) //
79             .nodeWithKey(Node.QNAME, NODE_KEY_BI) //
80             .toInstance();
81     private static final NodeRef NODE_REF = new NodeRef(NODE_INSTANCE_ID_BA);
82
83
84
85 //    private static final org.opendaylight.yangtools.yang.data.api.InstanceIdentifier FLOW_INSTANCE_ID_BI = //
86 //    org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.builder() //
87 //
88 //            .node(Flows.QNAME) //
89 //            .nodeWithKey(Flow.QNAME, FLOW_KEY_BI) //
90 //            .toInstance();
91     private static final InstanceIdentifier<? extends DataObject> FLOW_INSTANCE_ID_BA = //
92             NODE_INSTANCE_ID_BA.builder() //
93             .augmentation(FlowCapableNode.class)
94             .child(Table.class,TABLE_KEY)
95             .child(Flow.class, FLOW_KEY) //
96             .toInstance();
97     private static final QName FLOW_FLAGS_QNAME = QName.create(Flow.QNAME, "flags");
98
99     @Test
100     public void testIndirectGeneration() throws Exception {
101
102         FlowModFlags checkOverlapFlags = new FlowModFlags(true,false,false,false,false);
103         ImmutableSet<String> domCheckOverlapFlags = ImmutableSet.<String>of("CHECK_OVERLAP");
104         testFlags(checkOverlapFlags,domCheckOverlapFlags);
105
106
107
108         FlowModFlags allFalseFlags = new FlowModFlags(false,false,false,false,false);
109         ImmutableSet<String> domAllFalseFlags = ImmutableSet.<String>of();
110         testFlags(allFalseFlags,domAllFalseFlags);
111
112         FlowModFlags allTrueFlags = new FlowModFlags(true,true,true,true,true);
113         ImmutableSet<String> domAllTrueFlags = ImmutableSet.<String>of("CHECK_OVERLAP","NO_BYT_COUNTS", "NO_PKT_COUNTS", "RESET_COUNTS", "SEND_FLOW_REM");
114         testFlags(allTrueFlags,domAllTrueFlags);
115
116         FlowModFlags nullFlags = null;
117         ImmutableSet<String> domNullFlags = null;
118         testFlags(null,null);
119
120
121
122     }
123
124     private void testFlags(FlowModFlags flagsToTest, ImmutableSet<String> domFlags) throws Exception {
125         Flow flow = createFlow(flagsToTest);
126         assertNotNull(flow);
127
128         CompositeNode domFlow = biDataService.readConfigurationData(mappingService.toDataDom(FLOW_INSTANCE_ID_BA));
129
130         assertNotNull(domFlow);
131         org.opendaylight.yangtools.yang.data.api.Node<?> readedFlags = domFlow.getFirstSimpleByName(FLOW_FLAGS_QNAME);
132
133         if(domFlags != null) {
134             assertNotNull(readedFlags);
135             assertEquals(domFlags,readedFlags.getValue());
136         } else {
137             assertNull(readedFlags);
138         }
139         assertEquals(flagsToTest, flow.getFlags());
140
141         DataModificationTransaction transaction = baDataService.beginTransaction();
142         transaction.removeConfigurationData(FLOW_INSTANCE_ID_BA);
143         RpcResult<TransactionStatus> result = transaction.commit().get();
144         assertEquals(TransactionStatus.COMMITED, result.getResult());
145
146     }
147
148     private Flow createFlow(FlowModFlags flagsToTest) throws Exception {
149
150         DataModificationTransaction modification = baDataService.beginTransaction();
151
152         FlowBuilder flow = new FlowBuilder();
153         MatchBuilder match = new MatchBuilder();
154         VlanMatchBuilder vlanBuilder = new VlanMatchBuilder();
155         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
156         VlanId vlanId = new VlanId(10);
157         vlanBuilder.setVlanId(vlanIdBuilder.setVlanId(vlanId).build());
158         match.setVlanMatch(vlanBuilder.build());
159
160         flow.setKey(FLOW_KEY);
161         flow.setMatch(match.build());
162
163         flow.setFlags(flagsToTest);
164
165         InstructionsBuilder instructions = new InstructionsBuilder();
166         InstructionBuilder instruction = new InstructionBuilder();
167
168         instruction.setOrder(10);
169         ApplyActionsBuilder applyActions = new ApplyActionsBuilder();
170         List<Action> actionList = new ArrayList<>();
171         PopMplsActionBuilder popMplsAction = new PopMplsActionBuilder();
172         popMplsAction.setEthernetType(34);
173         actionList.add(new ActionBuilder().setAction(new PopMplsActionCaseBuilder().setPopMplsAction(popMplsAction.build()).build()).setOrder(10).build());
174
175         applyActions.setAction(actionList );
176
177         instruction.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(applyActions.build()).build());
178
179
180         List<Instruction> instructionList = Collections.<Instruction>singletonList(instruction.build());
181         instructions.setInstruction(instructionList );
182
183         flow.setInstructions(instructions.build());
184         modification.putConfigurationData(FLOW_INSTANCE_ID_BA, flow.build());
185         RpcResult<TransactionStatus> ret = modification.commit().get();
186         assertNotNull(ret);
187         assertEquals(TransactionStatus.COMMITED, ret.getResult());
188         return (Flow) baDataService.readConfigurationData(FLOW_INSTANCE_ID_BA);
189     }
190 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.