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

©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.