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

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