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