2 * Copyright (c) 2014 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.util;
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;
41 * Unit tests for TypeKeyMakerFactory.
43 * @author michal.polkorab
45 public class TypeKeyMakerFactoryTest {
48 * Tests {@link TypeKeyMakerFactory#createActionKeyMaker(short)}.
51 public void testActionKeyMaker() {
52 TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF_VERSION_1_3);
53 Assert.assertNotNull("Null keyMaker", keyMaker);
55 ActionBuilder builder = new ActionBuilder();
56 builder.setActionChoice(new OutputActionCaseBuilder().build());
57 Action action = builder.build();
58 MessageTypeKey<?> key = keyMaker.make(action);
60 Assert.assertNotNull("Null key", key);
61 Assert.assertEquals("Wrong key", new ActionSerializerKey<>(EncodeConstants.OF_VERSION_1_3,
62 OutputActionCase.class, null), key);
66 * Tests {@link TypeKeyMakerFactory#createActionKeyMaker(short)}.
69 public void testExperimenterActionKeyMaker() {
70 TypeKeyMaker<Action> keyMaker = TypeKeyMakerFactory.createActionKeyMaker(EncodeConstants.OF_VERSION_1_3);
71 Assert.assertNotNull("Null keyMaker", keyMaker);
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);
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);
86 * Tests {@link TypeKeyMakerFactory#createInstructionKeyMaker(short)}.
89 public void testInstructionKeyMaker() {
90 TypeKeyMaker<Instruction> keyMaker =
91 TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF_VERSION_1_3);
92 Assert.assertNotNull("Null keyMaker", keyMaker);
94 InstructionBuilder builder = new InstructionBuilder();
95 builder.setInstructionChoice(new GotoTableCaseBuilder().build());
96 Instruction instruction = builder.build();
97 MessageTypeKey<?> key = keyMaker.make(instruction);
99 Assert.assertNotNull("Null key", key);
100 Assert.assertEquals("Wrong key", new InstructionSerializerKey<>(EncodeConstants.OF_VERSION_1_3,
101 GotoTableCase.class, null), key);
105 * Tests {@link TypeKeyMakerFactory#createInstructionKeyMaker(short)}.
108 public void testExperimenterInstructionKeyMaker() {
109 TypeKeyMaker<Instruction> keyMaker =
110 TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF_VERSION_1_3);
111 Assert.assertNotNull("Null keyMaker", keyMaker);
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);
119 Assert.assertNotNull("Null key", key);
120 Assert.assertEquals("Wrong key", new InstructionSerializerKey<>(EncodeConstants.OF_VERSION_1_3,
121 ClearActionsCase.class, 42L), key);
125 * Tests {@link TypeKeyMakerFactory#createMatchEntriesKeyMaker(short)}.
128 public void testMatchEntriesKeyMaker() {
129 TypeKeyMaker<MatchEntry> keyMaker =
130 TypeKeyMakerFactory.createMatchEntriesKeyMaker(EncodeConstants.OF_VERSION_1_3);
131 Assert.assertNotNull("Null keyMaker", keyMaker);
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);
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);
147 * Tests {@link TypeKeyMakerFactory#createMatchEntriesKeyMaker(short)}.
150 public void testExperimenterMatchEntriesKeyMaker() {
151 TypeKeyMaker<MatchEntry> keyMaker =
152 TypeKeyMakerFactory.createMatchEntriesKeyMaker(EncodeConstants.OF_VERSION_1_3);
153 Assert.assertNotNull("Null keyMaker", keyMaker);
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);
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);
174 private interface OxmMatchFieldClass extends MatchField {
175 // only for testing purposes