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