36a8d474da86f428fb1ba9fbbf821f36d814ffd6
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / core / sal / convertor / OFToMDSalFlowConvertorTest.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.openflowplugin.openflow.md.core.sal.convertor;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import org.junit.Test;
18 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionsInstruction;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.ActionsInstructionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MetadataInstruction;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MetadataInstructionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MeterIdInstruction;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.MeterIdInstructionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.TableIdInstruction;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.TableIdInstructionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ActionBase;
39
40 /**
41  * Created by Martin Bobak mbobak@cisco.com on 9/18/14.
42  */
43 public class OFToMDSalFlowConvertorTest {
44
45     private static final int PRESET_COUNT = 7;
46
47     /**
48      * Test method for {@link OFToMDSalFlowConvertor#wrapOF10ActionsToInstruction(java.util.List, org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion)}
49      */
50     @Test
51     public void testWrapOF10ActionsToInstruction() {
52         ActionBuilder actionBuilder = new ActionBuilder();
53         List<Action> actions = new ArrayList<>();
54         for (int j = 0; j < PRESET_COUNT; j++) {
55             actionBuilder.setType(MockActionBase.class);
56             actions.add(actionBuilder.build());
57         }
58         Instructions instructions = OFToMDSalFlowConvertor.wrapOF10ActionsToInstruction(actions, OpenflowVersion.OF13);
59         assertNotNull(instructions);
60     }
61
62     /**
63      * Test method for {@link OFToMDSalFlowConvertor#toSALInstruction(java.util.List, org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion)}
64      */
65     @Test
66     public void testToSALInstruction() {
67         List<Instruction> instructionsList = new ArrayList<>();
68         InstructionBuilder instructionBuilder = new InstructionBuilder();
69         for (int i = 0; i < PRESET_COUNT; i++) {
70             instructionBuilder.setType(ApplyActions.class);
71             ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
72             ActionBuilder actionBuilder = new ActionBuilder();
73             List<Action> actions = new ArrayList<>();
74             for (int j = 0; j < PRESET_COUNT; j++) {
75                 actionBuilder.setType(MockActionBase.class);
76                 actions.add(actionBuilder.build());
77             }
78             actionsInstructionBuilder.setAction(actions);
79             instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
80             instructionsList.add(instructionBuilder.build());
81         }
82
83         Instructions instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
84         assertNotNull(instructions);
85         assertEquals(PRESET_COUNT, instructions.getInstruction().size());
86
87         instructionsList = new ArrayList<>();
88         for (int i = 0; i < PRESET_COUNT; i++) {
89             instructionBuilder.setType(GotoTable.class);
90             TableIdInstructionBuilder tableIdInstructionBuilder = new TableIdInstructionBuilder();
91             tableIdInstructionBuilder.setTableId((short) i);
92             instructionBuilder.addAugmentation(TableIdInstruction.class, tableIdInstructionBuilder.build());
93             instructionsList.add(instructionBuilder.build());
94         }
95
96         instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
97         assertNotNull(instructions);
98         assertEquals(PRESET_COUNT, instructions.getInstruction().size());
99
100         instructionsList = new ArrayList<>();
101         for (int i = 0; i < PRESET_COUNT; i++) {
102             instructionBuilder.setType(Meter.class);
103             MeterIdInstructionBuilder meterIdInstructionBuilder = new MeterIdInstructionBuilder();
104             meterIdInstructionBuilder.setMeterId(Long.valueOf(i));
105             instructionBuilder.addAugmentation(MeterIdInstruction.class, meterIdInstructionBuilder.build());
106             instructionsList.add(instructionBuilder.build());
107         }
108
109         instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
110         assertNotNull(instructions);
111         assertEquals(PRESET_COUNT, instructions.getInstruction().size());
112
113
114         instructionsList = new ArrayList<>();
115         for (int i = 0; i < PRESET_COUNT; i++) {
116             instructionBuilder.setType(WriteActions.class);
117             ActionsInstructionBuilder actionsInstructionBuilder = new ActionsInstructionBuilder();
118             ActionBuilder actionBuilder = new ActionBuilder();
119             List<Action> actions = new ArrayList<>();
120             for (int j = 0; j < PRESET_COUNT; j++) {
121                 actionBuilder.setType(MockActionBase.class);
122                 actions.add(actionBuilder.build());
123             }
124             actionsInstructionBuilder.setAction(actions);
125             instructionBuilder.addAugmentation(ActionsInstruction.class, actionsInstructionBuilder.build());
126             instructionsList.add(instructionBuilder.build());
127         }
128
129         instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
130         assertNotNull(instructions);
131         assertEquals(PRESET_COUNT, instructions.getInstruction().size());
132
133         instructionsList = new ArrayList<>();
134         for (int i = 0; i < PRESET_COUNT; i++) {
135             instructionBuilder.setType(ClearActions.class);
136             instructionsList.add(instructionBuilder.build());
137         }
138
139         instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
140         assertNotNull(instructions);
141         assertEquals(PRESET_COUNT, instructions.getInstruction().size());
142
143         instructionsList = new ArrayList<>();
144         for (int i = 0; i < PRESET_COUNT; i++) {
145             instructionBuilder.setType(WriteMetadata.class);
146             MetadataInstructionBuilder metadataInstructionBuilder = new MetadataInstructionBuilder();
147             metadataInstructionBuilder.setMetadata(new byte[i]);
148             metadataInstructionBuilder.setMetadataMask(new byte[i]);
149             instructionBuilder.addAugmentation(MetadataInstruction.class, metadataInstructionBuilder.build());
150             instructionsList.add(instructionBuilder.build());
151         }
152
153         instructions = OFToMDSalFlowConvertor.toSALInstruction(instructionsList, OpenflowVersion.OF13);
154         assertNotNull(instructions);
155         assertEquals(PRESET_COUNT, instructions.getInstruction().size());
156     }
157
158     private static final class MockActionBase extends ActionBase {
159         // for testing purposes
160     }
161 }