Instruction experimenterId fix
[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.keys.ActionSerializerKey;
14 import org.opendaylight.openflowjava.protocol.api.keys.InstructionSerializerKey;
15 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.oxm.container.match.entry.value.ExperimenterIdCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.oxm.container.match.entry.value.experimenter.id._case.ExperimenterBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.ExperimenterActionSubType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlInCase;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlInCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCase;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.ClearActionsCaseBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCaseBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.InstructionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.ExperimenterId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.ExperimenterClass;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.InPort;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
40
41 /**
42  * @author michal.polkorab
43  *
44  */
45 public class TypeKeyMakerFactoryTest {
46
47     /**
48      * Tests {@link TypeKeyMakerFactory#createActionKeyMaker(short)}
49      */
50     @Test
51     public void testActionKeyMaker() {
52         TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF13_VERSION_ID);
53         Assert.assertNotNull("Null keyMaker", keyMaker);
54
55         ActionBuilder builder = new ActionBuilder();
56         builder.setActionChoice(new OutputActionCaseBuilder().build());
57         Action action = builder.build();
58         MessageTypeKey<?> key = keyMaker.make(action);
59
60         Assert.assertNotNull("Null key", key);
61         Assert.assertEquals("Wrong key", new ActionSerializerKey<>(EncodeConstants.OF13_VERSION_ID,
62                         OutputActionCase.class, null), key);
63     }
64
65     /**
66      * Tests {@link TypeKeyMakerFactory#createActionKeyMaker(short)}
67      */
68     @Test
69     public void testExperimenterActionKeyMaker() {
70         TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF13_VERSION_ID);
71         Assert.assertNotNull("Null keyMaker", keyMaker);
72
73         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder builder = new ActionBuilder();
74         builder.setExperimenterId(new ExperimenterId(42L));
75         builder.setActionChoice(new CopyTtlInCaseBuilder().build());
76         Action action = builder.build();
77         MessageTypeKey<?> key = keyMaker.make(action);
78
79         Assert.assertNotNull("Null key", key);
80         Assert.assertEquals("Wrong key", new ActionSerializerKey(EncodeConstants.OF13_VERSION_ID,
81                 CopyTtlInCase.class, 42L), key);
82     }
83
84     /**
85      * Tests {@link TypeKeyMakerFactory#createInstructionKeyMaker(short)}
86      */
87     @Test
88     public void testInstructionKeyMaker() {
89         TypeKeyMaker<Instruction> keyMaker = TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
90         Assert.assertNotNull("Null keyMaker", keyMaker);
91
92         InstructionBuilder builder = new InstructionBuilder();
93         builder.setInstructionChoice(new GotoTableCaseBuilder().build());
94         Instruction instruction = builder.build();
95         MessageTypeKey<?> key = keyMaker.make(instruction);
96
97         Assert.assertNotNull("Null key", key);
98         Assert.assertEquals("Wrong key", new InstructionSerializerKey<>(EncodeConstants.OF13_VERSION_ID,
99                         GotoTableCase.class, null), key);
100     }
101
102     /**
103      * Tests {@link TypeKeyMakerFactory#createInstructionKeyMaker(short)}
104      */
105     @Test
106     public void testExperimenterInstructionKeyMaker() {
107         TypeKeyMaker<Instruction> keyMaker = TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
108         Assert.assertNotNull("Null keyMaker", keyMaker);
109
110         InstructionBuilder builder = new InstructionBuilder();
111         builder.setExperimenterId(new ExperimenterId(42L));
112         builder.setInstructionChoice(new ClearActionsCaseBuilder().build());
113         Instruction instruction = builder.build();
114         MessageTypeKey<?> key = keyMaker.make(instruction);
115
116         Assert.assertNotNull("Null key", key);
117         Assert.assertEquals("Wrong key", new InstructionSerializerKey(EncodeConstants.OF13_VERSION_ID,
118                         ClearActionsCase.class, 42L), key);
119     }
120
121     /**
122      * Tests {@link TypeKeyMakerFactory#createMatchEntriesKeyMaker(short)}
123      */
124     @Test
125     public void testMatchEntriesKeyMaker() {
126         TypeKeyMaker<MatchEntry> keyMaker = TypeKeyMakerFactory.createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
127         Assert.assertNotNull("Null keyMaker", keyMaker);
128
129         MatchEntryBuilder builder = new MatchEntryBuilder();
130         builder.setOxmClass(OpenflowBasicClass.class);
131         builder.setOxmMatchField(InPort.class);
132         builder.setHasMask(true);
133         MatchEntry entry = builder.build();
134         MessageTypeKey<?> key = keyMaker.make(entry);
135
136         Assert.assertNotNull("Null key", key);
137         MatchEntrySerializerKey<?, ?> comparationKey = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
138                 OpenflowBasicClass.class, InPort.class);
139         Assert.assertEquals("Wrong key", comparationKey, key);
140     }
141
142     /**
143      * Tests {@link TypeKeyMakerFactory#createMatchEntriesKeyMaker(short)}
144      */
145     @Test
146     public void testExperimenterMatchEntriesKeyMaker() {
147         TypeKeyMaker<MatchEntry> keyMaker = TypeKeyMakerFactory.createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
148         Assert.assertNotNull("Null keyMaker", keyMaker);
149
150         MatchEntryBuilder builder = new MatchEntryBuilder();
151         builder.setOxmClass(ExperimenterClass.class);
152         builder.setOxmMatchField(OxmMatchFieldClass.class);
153         builder.setHasMask(true);
154         ExperimenterIdCaseBuilder caseBuilder = new ExperimenterIdCaseBuilder();
155         ExperimenterBuilder expBuilder = new ExperimenterBuilder();
156         expBuilder.setExperimenter(new ExperimenterId(42L));
157         caseBuilder.setExperimenter(expBuilder.build());
158         builder.setMatchEntryValue(caseBuilder.build());
159         MatchEntry entry = builder.build();
160         MessageTypeKey<?> key = keyMaker.make(entry);
161
162         Assert.assertNotNull("Null key", key);
163         MatchEntrySerializerKey<?, ?> comparationKey = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
164                 ExperimenterClass.class, OxmMatchFieldClass.class);
165         comparationKey.setExperimenterId(42L);
166         Assert.assertEquals("Wrong key", comparationKey, key);
167     }
168
169     private class ActionSubtypeClass extends ExperimenterActionSubType {
170         // only for testing purposes
171     }
172
173     private class OxmMatchFieldClass extends MatchField {
174         // only for testing purposes
175     }
176 }