c6278285f214966b1eebbacf02c0455f4f023866
[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.ExperimenterInstructionSerializerKey;
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.MessageTypeKey;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.oxm.container.match.entry.value.ExperimenterIdCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.oxm.container.match.entry.value.experimenter.id._case.ExperimenterBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.ExperimenterActionSubType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlInCase;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.CopyTtlInCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCase;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.action.grouping.action.choice.OutputActionCaseBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.Action;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev150203.actions.grouping.ActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCase;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instruction.grouping.instruction.choice.GotoTableCaseBuilder;
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         builder.setExperimenterId(new ExperimenterId(42L));
74         builder.setActionChoice(new CopyTtlInCaseBuilder().build());
75         Action action = builder.build();
76         MessageTypeKey<?> key = keyMaker.make(action);
77
78         Assert.assertNotNull("Null key", key);
79         Assert.assertEquals("Wrong key", new ActionSerializerKey(EncodeConstants.OF13_VERSION_ID,
80                 CopyTtlInCase.class, 42L), key);
81     }
82
83     /**
84      * Tests {@link TypeKeyMakerFactory#createInstructionKeyMaker(short)}
85      */
86     @Test
87     public void testInstructionKeyMaker() {
88         TypeKeyMaker<Instruction> keyMaker = TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
89         Assert.assertNotNull("Null keyMaker", keyMaker);
90
91         InstructionBuilder builder = new InstructionBuilder();
92         builder.setInstructionChoice(new GotoTableCaseBuilder().build());
93         Instruction instruction = builder.build();
94         MessageTypeKey<?> key = keyMaker.make(instruction);
95
96         Assert.assertNotNull("Null key", key);
97         Assert.assertEquals("Wrong key", new InstructionSerializerKey<>(EncodeConstants.OF13_VERSION_ID,
98                         GotoTableCase.class, null), key);
99     }
100
101     /**
102      * Tests {@link TypeKeyMakerFactory#createInstructionKeyMaker(short)}
103      */
104     @Test
105     public void testExperimenterInstructionKeyMaker() {
106         TypeKeyMaker<Instruction> keyMaker = TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
107         Assert.assertNotNull("Null keyMaker", keyMaker);
108
109         InstructionBuilder builder = new InstructionBuilder();
110         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.instruction.container
111         .instruction.choice.ExperimenterIdCaseBuilder caseBuilder = new org.opendaylight.yang.gen.v1.urn
112         .opendaylight.openflow.augments.rev150225.instruction.container.instruction.choice.ExperimenterIdCaseBuilder();
113         org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.instruction.container
114         .instruction.choice.experimenter.id._case.ExperimenterBuilder expIdBuilder = new org.opendaylight.yang.gen
115         .v1.urn.opendaylight.openflow.augments.rev150225.instruction.container.instruction.choice.experimenter.id
116         ._case.ExperimenterBuilder();
117         expIdBuilder.setExperimenterId(new ExperimenterId(42L));
118         caseBuilder.setExperimenter(expIdBuilder.build());
119         builder.setInstructionChoice(caseBuilder.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<MatchEntry> keyMaker = TypeKeyMakerFactory.createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
134         Assert.assertNotNull("Null keyMaker", keyMaker);
135
136         MatchEntryBuilder builder = new MatchEntryBuilder();
137         builder.setOxmClass(OpenflowBasicClass.class);
138         builder.setOxmMatchField(InPort.class);
139         builder.setHasMask(true);
140         MatchEntry 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<MatchEntry> keyMaker = TypeKeyMakerFactory.createMatchEntriesKeyMaker(EncodeConstants.OF13_VERSION_ID);
155         Assert.assertNotNull("Null keyMaker", keyMaker);
156
157         MatchEntryBuilder builder = new MatchEntryBuilder();
158         builder.setOxmClass(ExperimenterClass.class);
159         builder.setOxmMatchField(OxmMatchFieldClass.class);
160         builder.setHasMask(true);
161         ExperimenterIdCaseBuilder caseBuilder = new ExperimenterIdCaseBuilder();
162         ExperimenterBuilder expBuilder = new ExperimenterBuilder();
163         expBuilder.setExperimenter(new ExperimenterId(42L));
164         caseBuilder.setExperimenter(expBuilder.build());
165         builder.setMatchEntryValue(caseBuilder.build());
166         MatchEntry entry = builder.build();
167         MessageTypeKey<?> key = keyMaker.make(entry);
168
169         Assert.assertNotNull("Null key", key);
170         MatchEntrySerializerKey<?, ?> comparationKey = new MatchEntrySerializerKey<>(EncodeConstants.OF13_VERSION_ID,
171                 ExperimenterClass.class, OxmMatchFieldClass.class);
172         comparationKey.setExperimenterId(42L);
173         Assert.assertEquals("Wrong key", comparationKey, key);
174     }
175
176     private class ActionSubtypeClass extends ExperimenterActionSubType {
177         // only for testing purposes
178     }
179
180     private class OxmMatchFieldClass extends MatchField {
181         // only for testing purposes
182     }
183 }