Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / TypeKeyMakerFactoryTest.java
1 /*
2  * Copyright (c) 2014 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.util;
10
11 import org.junit.Assert;
12 import org.junit.Test;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.MessageTypeKey;
14 import org.opendaylight.openflowjava.protocol.api.keys.ActionSerializerKey;
15 import org.opendaylight.openflowjava.protocol.api.keys.InstructionSerializerKey;
16 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
17 import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterActionSerializerKey;
18 import org.opendaylight.openflowjava.protocol.api.keys.experimenter.ExperimenterInstructionSerializerKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdAction;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdActionBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdInstruction;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdInstructionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdMatchEntry;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdMatchEntryBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Experimenter;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.ExperimenterActionSubType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.grouping.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ExperimenterClass;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.InPort;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.MatchField;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OpenflowBasicClass;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntriesBuilder;
41
42 /**
43  * @author michal.polkorab
44  *
45  */
46 public class TypeKeyMakerFactoryTest {
47
48     /**
49      * Tests {@link TypeKeyMakerFactory#createActionKeyMaker(short)}
50      */
51     @Test
52     public void testActionKeyMaker() {
53         TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF13_VERSION_ID);
54         Assert.assertNotNull("Null keyMaker", keyMaker);
55
56         ActionBuilder builder = new ActionBuilder();
57         builder.setType(Output.class);
58         Action action = builder.build();
59         MessageTypeKey<?> key = keyMaker.make(action);
60
61         Assert.assertNotNull("Null key", key);
62         Assert.assertEquals("Wrong key", new ActionSerializerKey<>(EncodeConstants.OF13_VERSION_ID,
63                         Output.class, null), key);
64     }
65
66     /**
67      * Tests {@link TypeKeyMakerFactory#createActionKeyMaker(short)}
68      */
69     @Test
70     public void testExperimenterActionKeyMaker() {
71         TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF13_VERSION_ID);
72         Assert.assertNotNull("Null keyMaker", keyMaker);
73
74         ActionBuilder builder = new ActionBuilder();
75         builder.setType(Experimenter.class);
76         ExperimenterIdActionBuilder expIdBuilder = new ExperimenterIdActionBuilder();
77         expIdBuilder.setExperimenter(new ExperimenterId(42L));
78         expIdBuilder.setSubType(ActionSubtypeClass.class);
79         builder.addAugmentation(ExperimenterIdAction.class, expIdBuilder.build());
80         Action action = builder.build();
81         MessageTypeKey<?> key = keyMaker.make(action);
82
83         Assert.assertNotNull("Null key", key);
84         Assert.assertEquals("Wrong key", new ExperimenterActionSerializerKey(EncodeConstants.OF13_VERSION_ID, 42L,
85                 ActionSubtypeClass.class), key);
86     }
87
88     /**
89      * Tests {@link TypeKeyMakerFactory#createInstructionKeyMaker(short)}
90      */
91     @Test
92     public void testInstructionKeyMaker() {
93         TypeKeyMaker<Instruction> keyMaker = TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
94         Assert.assertNotNull("Null keyMaker", keyMaker);
95
96         InstructionBuilder builder = new InstructionBuilder();
97         builder.setType(GotoTable.class);
98         Instruction instruction = builder.build();
99         MessageTypeKey<?> key = keyMaker.make(instruction);
100
101         Assert.assertNotNull("Null key", key);
102         Assert.assertEquals("Wrong key", new InstructionSerializerKey<>(EncodeConstants.OF13_VERSION_ID,
103                         GotoTable.class, null), key);
104     }
105
106     /**
107      * Tests {@link TypeKeyMakerFactory#createInstructionKeyMaker(short)}
108      */
109     @Test
110     public void testExperimenterInstructionKeyMaker() {
111         TypeKeyMaker<Instruction> keyMaker = TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
112         Assert.assertNotNull("Null keyMaker", keyMaker);
113
114         InstructionBuilder builder = new InstructionBuilder();
115         builder.setType(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow
116                 .common.instruction.rev130731.Experimenter.class);
117         ExperimenterIdInstructionBuilder expIdBuilder = new ExperimenterIdInstructionBuilder();
118         expIdBuilder.setExperimenter(new ExperimenterId(42L));
119         builder.addAugmentation(ExperimenterIdInstruction.class, expIdBuilder.build());
120         Instruction instruction = builder.build();
121         MessageTypeKey<?> key = keyMaker.make(instruction);
122
123         Assert.assertNotNull("Null key", key);
124         Assert.assertEquals("Wrong key", new ExperimenterInstructionSerializerKey(EncodeConstants.OF13_VERSION_ID,
125                         42L), key);
126     }
127
128     /**
129      * Tests {@link TypeKeyMakerFactory#createMatchEntriesKeyMaker(short)}
130      */
131     @Test
132     public void testMatchEntriesKeyMaker() {
133         TypeKeyMaker<MatchEntries> keyMaker = TypeKeyMakerFactory.createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
134         Assert.assertNotNull("Null keyMaker", keyMaker);
135
136         MatchEntriesBuilder builder = new MatchEntriesBuilder();
137         builder.setOxmClass(OpenflowBasicClass.class);
138         builder.setOxmMatchField(InPort.class);
139         builder.setHasMask(true);
140         MatchEntries entry = builder.build();
141         MessageTypeKey<?> key = keyMaker.make(entry);
142
143         Assert.assertNotNull("Null key", key);
144         MatchEntrySerializerKey<?, ?> comparationKey = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
145                 OpenflowBasicClass.class, InPort.class);
146         Assert.assertEquals("Wrong key", comparationKey, key);
147     }
148
149     /**
150      * Tests {@link TypeKeyMakerFactory#createMatchEntriesKeyMaker(short)}
151      */
152     @Test
153     public void testExperimenterMatchEntriesKeyMaker() {
154         TypeKeyMaker<MatchEntries> keyMaker = TypeKeyMakerFactory.createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
155         Assert.assertNotNull("Null keyMaker", keyMaker);
156
157         MatchEntriesBuilder builder = new MatchEntriesBuilder();
158         builder.setOxmClass(ExperimenterClass.class);
159         builder.setOxmMatchField(OxmMatchFieldClass.class);
160         builder.setHasMask(true);
161         ExperimenterIdMatchEntryBuilder expIdBuilder = new ExperimenterIdMatchEntryBuilder();
162         expIdBuilder.setExperimenter(new ExperimenterId(42L));
163         builder.addAugmentation(ExperimenterIdMatchEntry.class, expIdBuilder.build());
164         MatchEntries entry = builder.build();
165         MessageTypeKey<?> key = keyMaker.make(entry);
166
167         Assert.assertNotNull("Null key", key);
168         MatchEntrySerializerKey<?, ?> comparationKey = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
169                 ExperimenterClass.class, OxmMatchFieldClass.class);
170         comparationKey.setExperimenterId(42L);
171         Assert.assertEquals("Wrong key", comparationKey, key);
172     }
173
174     private class ActionSubtypeClass extends ExperimenterActionSubType {
175         // only for testing purposes
176     }
177
178     private class OxmMatchFieldClass extends MatchField {
179         // only for testing purposes
180     }
181 }