Unit tests for Instruction and Ethernet codecs
Added tests for codecs for Ethernet and Instruction classes Also fixed some bugs uncovered by the tests Change-Id: I29f82d169e81b3fca417f88fab992148bf36dc71
Showing
8 changed files
with
763 additions
and
5 deletions
... | @@ -94,6 +94,8 @@ public class InstructionsTest { | ... | @@ -94,6 +94,8 @@ public class InstructionsTest { |
94 | assertThatClassIsImmutable(L2ModificationInstruction.ModVlanIdInstruction.class); | 94 | assertThatClassIsImmutable(L2ModificationInstruction.ModVlanIdInstruction.class); |
95 | assertThatClassIsImmutable(L2ModificationInstruction.ModVlanPcpInstruction.class); | 95 | assertThatClassIsImmutable(L2ModificationInstruction.ModVlanPcpInstruction.class); |
96 | assertThatClassIsImmutable(L3ModificationInstruction.ModIPInstruction.class); | 96 | assertThatClassIsImmutable(L3ModificationInstruction.ModIPInstruction.class); |
97 | + assertThatClassIsImmutable(L2ModificationInstruction.ModMplsLabelInstruction.class); | ||
98 | + assertThatClassIsImmutable(L2ModificationInstruction.PushHeaderInstructions.class); | ||
97 | } | 99 | } |
98 | 100 | ||
99 | // DropInstruction | 101 | // DropInstruction | ... | ... |
... | @@ -36,13 +36,23 @@ public class EthernetCodec extends JsonCodec<Ethernet> { | ... | @@ -36,13 +36,23 @@ public class EthernetCodec extends JsonCodec<Ethernet> { |
36 | public ObjectNode encode(Ethernet ethernet, CodecContext context) { | 36 | public ObjectNode encode(Ethernet ethernet, CodecContext context) { |
37 | checkNotNull(ethernet, "Ethernet cannot be null"); | 37 | checkNotNull(ethernet, "Ethernet cannot be null"); |
38 | 38 | ||
39 | - return context.mapper().createObjectNode() | 39 | + final ObjectNode result = context.mapper().createObjectNode() |
40 | - .put("destinationMacAddress", ethernet.getDestinationMAC().toString()) | ||
41 | - .put("sourceMacAddress", ethernet.getSourceMAC().toString()) | ||
42 | - .put("priorityCode", ethernet.getPriorityCode()) | ||
43 | .put("vlanId", ethernet.getVlanID()) | 40 | .put("vlanId", ethernet.getVlanID()) |
44 | .put("etherType", ethernet.getEtherType()) | 41 | .put("etherType", ethernet.getEtherType()) |
42 | + .put("priorityCode", ethernet.getPriorityCode()) | ||
45 | .put("pad", ethernet.isPad()); | 43 | .put("pad", ethernet.isPad()); |
44 | + | ||
45 | + if (ethernet.getDestinationMAC() != null) { | ||
46 | + result.put("destMac", | ||
47 | + ethernet.getDestinationMAC().toString()); | ||
48 | + } | ||
49 | + | ||
50 | + if (ethernet.getSourceMAC() != null) { | ||
51 | + result.put("srcMac", | ||
52 | + ethernet.getSourceMAC().toString()); | ||
53 | + } | ||
54 | + | ||
55 | + return result; | ||
46 | } | 56 | } |
47 | 57 | ||
48 | } | 58 | } | ... | ... |
... | @@ -147,7 +147,7 @@ public class InstructionCodec extends JsonCodec<Instruction> { | ... | @@ -147,7 +147,7 @@ public class InstructionCodec extends JsonCodec<Instruction> { |
147 | case OUTPUT: | 147 | case OUTPUT: |
148 | final Instructions.OutputInstruction outputInstruction = | 148 | final Instructions.OutputInstruction outputInstruction = |
149 | (Instructions.OutputInstruction) instruction; | 149 | (Instructions.OutputInstruction) instruction; |
150 | - result.put("portNumber", outputInstruction.port().toLong()); | 150 | + result.put("port", outputInstruction.port().toLong()); |
151 | break; | 151 | break; |
152 | 152 | ||
153 | case DROP: | 153 | case DROP: | ... | ... |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.codec.impl; | ||
17 | + | ||
18 | +import org.junit.Test; | ||
19 | +import org.onlab.packet.Ethernet; | ||
20 | +import org.onosproject.codec.CodecContext; | ||
21 | +import org.onosproject.codec.JsonCodec; | ||
22 | + | ||
23 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
24 | + | ||
25 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
26 | +import static org.hamcrest.Matchers.notNullValue; | ||
27 | +import static org.onosproject.codec.impl.EthernetJsonMatcher.matchesEthernet; | ||
28 | + | ||
29 | +/** | ||
30 | + * Unit test for Ethernet class codec. | ||
31 | + */ | ||
32 | +public class EthernetCodecTest { | ||
33 | + | ||
34 | + /** | ||
35 | + * Unit test for the ethernet object codec. | ||
36 | + */ | ||
37 | + @Test | ||
38 | + public void ethernetCodecTest() { | ||
39 | + final CodecContext context = new MockCodecContext(); | ||
40 | + final JsonCodec<Ethernet> ethernetCodec = context.codec(Ethernet.class); | ||
41 | + assertThat(ethernetCodec, notNullValue()); | ||
42 | + | ||
43 | + final Ethernet eth1 = new Ethernet(); | ||
44 | + eth1.setSourceMACAddress("11:22:33:44:55:01"); | ||
45 | + eth1.setDestinationMACAddress("11:22:33:44:55:02"); | ||
46 | + eth1.setPad(true); | ||
47 | + eth1.setEtherType(Ethernet.TYPE_ARP); | ||
48 | + eth1.setPriorityCode((byte) 7); | ||
49 | + eth1.setVlanID((short) 33); | ||
50 | + | ||
51 | + final ObjectNode eth1Json = ethernetCodec.encode(eth1, context); | ||
52 | + assertThat(eth1Json, notNullValue()); | ||
53 | + assertThat(eth1Json, matchesEthernet(eth1)); | ||
54 | + } | ||
55 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.codec.impl; | ||
17 | + | ||
18 | +import org.hamcrest.Description; | ||
19 | +import org.hamcrest.TypeSafeMatcher; | ||
20 | +import org.onlab.packet.Ethernet; | ||
21 | + | ||
22 | +import com.fasterxml.jackson.databind.JsonNode; | ||
23 | + | ||
24 | +/** | ||
25 | + * Hamcrest matcher for ethernet objects. | ||
26 | + */ | ||
27 | +public class EthernetJsonMatcher extends TypeSafeMatcher<JsonNode> { | ||
28 | + | ||
29 | + private final Ethernet ethernet; | ||
30 | + private String reason = ""; | ||
31 | + | ||
32 | + public EthernetJsonMatcher(Ethernet ethernetValue) { | ||
33 | + ethernet = ethernetValue; | ||
34 | + } | ||
35 | + | ||
36 | + @Override | ||
37 | + public boolean matchesSafely(JsonNode jsonEthernet) { | ||
38 | + | ||
39 | + // check source MAC | ||
40 | + final JsonNode jsonSourceMacNode = jsonEthernet.get("srcMac"); | ||
41 | + if (ethernet.getSourceMAC() != null) { | ||
42 | + final String jsonSourceMac = jsonSourceMacNode.textValue(); | ||
43 | + final String sourceMac = ethernet.getSourceMAC().toString(); | ||
44 | + if (!jsonSourceMac.equals(sourceMac)) { | ||
45 | + reason = "source MAC " + ethernet.getSourceMAC().toString(); | ||
46 | + return false; | ||
47 | + } | ||
48 | + } else { | ||
49 | + // source MAC not specified, JSON representation must be empty | ||
50 | + if (jsonSourceMacNode != null) { | ||
51 | + reason = "source mac should be null "; | ||
52 | + return false; | ||
53 | + } | ||
54 | + } | ||
55 | + | ||
56 | + // check destination MAC | ||
57 | + final JsonNode jsonDestinationMacNode = jsonEthernet.get("destMac"); | ||
58 | + if (ethernet.getDestinationMAC() != null) { | ||
59 | + final String jsonDestinationMac = jsonDestinationMacNode.textValue(); | ||
60 | + final String destinationMac = ethernet.getDestinationMAC().toString(); | ||
61 | + if (!jsonDestinationMac.equals(destinationMac)) { | ||
62 | + reason = "destination MAC " + ethernet.getDestinationMAC().toString(); | ||
63 | + return false; | ||
64 | + } | ||
65 | + } else { | ||
66 | + // destination MAC not specified, JSON representation must be empty | ||
67 | + if (jsonDestinationMacNode != null) { | ||
68 | + reason = "destination mac should be null "; | ||
69 | + return false; | ||
70 | + } | ||
71 | + } | ||
72 | + | ||
73 | + // check priority code | ||
74 | + final short jsonPriorityCode = jsonEthernet.get("priorityCode").shortValue(); | ||
75 | + final short priorityCode = ethernet.getPriorityCode(); | ||
76 | + if (jsonPriorityCode != priorityCode) { | ||
77 | + reason = "priority code " + Short.toString(ethernet.getPriorityCode()); | ||
78 | + return false; | ||
79 | + } | ||
80 | + | ||
81 | + // check vlanId | ||
82 | + final short jsonVlanId = jsonEthernet.get("vlanId").shortValue(); | ||
83 | + final short vlanId = ethernet.getVlanID(); | ||
84 | + if (jsonVlanId != vlanId) { | ||
85 | + reason = "vlan id " + Short.toString(ethernet.getVlanID()); | ||
86 | + return false; | ||
87 | + } | ||
88 | + | ||
89 | + // check etherType | ||
90 | + final short jsonEtherType = jsonEthernet.get("etherType").shortValue(); | ||
91 | + final short etherType = ethernet.getEtherType(); | ||
92 | + if (jsonEtherType != etherType) { | ||
93 | + reason = "etherType " + Short.toString(ethernet.getEtherType()); | ||
94 | + return false; | ||
95 | + } | ||
96 | + | ||
97 | + // check pad | ||
98 | + final boolean jsonPad = jsonEthernet.get("pad").asBoolean(); | ||
99 | + final boolean pad = ethernet.isPad(); | ||
100 | + if (jsonPad != pad) { | ||
101 | + reason = "pad " + Boolean.toString(ethernet.isPad()); | ||
102 | + return false; | ||
103 | + } | ||
104 | + | ||
105 | + return true; | ||
106 | + } | ||
107 | + | ||
108 | + @Override | ||
109 | + public void describeTo(Description description) { | ||
110 | + description.appendText(reason); | ||
111 | + } | ||
112 | + | ||
113 | + /** | ||
114 | + * Factory to allocate a ethernet matcher. | ||
115 | + * | ||
116 | + * @param ethernet ethernet object we are looking for | ||
117 | + * @return matcher | ||
118 | + */ | ||
119 | + public static EthernetJsonMatcher matchesEthernet(Ethernet ethernet) { | ||
120 | + return new EthernetJsonMatcher(ethernet); | ||
121 | + } | ||
122 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.codec.impl; | ||
17 | + | ||
18 | +import org.junit.Before; | ||
19 | +import org.junit.Test; | ||
20 | +import org.onlab.packet.IpAddress; | ||
21 | +import org.onlab.packet.IpPrefix; | ||
22 | +import org.onlab.packet.MacAddress; | ||
23 | +import org.onlab.packet.VlanId; | ||
24 | +import org.onosproject.codec.CodecContext; | ||
25 | +import org.onosproject.codec.JsonCodec; | ||
26 | +import org.onosproject.net.PortNumber; | ||
27 | +import org.onosproject.net.flow.instructions.Instruction; | ||
28 | +import org.onosproject.net.flow.instructions.Instructions; | ||
29 | +import org.onosproject.net.flow.instructions.L0ModificationInstruction; | ||
30 | +import org.onosproject.net.flow.instructions.L2ModificationInstruction; | ||
31 | +import org.onosproject.net.flow.instructions.L3ModificationInstruction; | ||
32 | + | ||
33 | +import com.fasterxml.jackson.databind.node.ObjectNode; | ||
34 | + | ||
35 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
36 | +import static org.hamcrest.Matchers.notNullValue; | ||
37 | +import static org.onosproject.codec.impl.InstructionJsonMatcher.matchesInstruction; | ||
38 | + | ||
39 | +/** | ||
40 | + * Unit tests for Instruction codec. | ||
41 | + */ | ||
42 | +public class InstructionCodecTest { | ||
43 | + CodecContext context; | ||
44 | + JsonCodec<Instruction> instructionCodec; | ||
45 | + | ||
46 | + /** | ||
47 | + * Sets up for each tests. Creates a context and fetches the instruction | ||
48 | + * codec. | ||
49 | + */ | ||
50 | + @Before | ||
51 | + public void setUp() { | ||
52 | + context = new MockCodecContext(); | ||
53 | + instructionCodec = context.codec(Instruction.class); | ||
54 | + assertThat(instructionCodec, notNullValue()); | ||
55 | + } | ||
56 | + | ||
57 | + /** | ||
58 | + * Tests the encoding of push header instructions. | ||
59 | + */ | ||
60 | + @Test | ||
61 | + public void pushHeaderInstructionsTest() { | ||
62 | + final L2ModificationInstruction.PushHeaderInstructions instruction = | ||
63 | + (L2ModificationInstruction.PushHeaderInstructions) Instructions.pushMpls(); | ||
64 | + final ObjectNode instructionJson = instructionCodec.encode(instruction, context); | ||
65 | + | ||
66 | + assertThat(instructionJson, matchesInstruction(instruction)); | ||
67 | + } | ||
68 | + | ||
69 | + /** | ||
70 | + * Tests the encoding of drop instructions. | ||
71 | + */ | ||
72 | + @Test | ||
73 | + public void dropInstructionTest() { | ||
74 | + final Instructions.DropInstruction instruction = | ||
75 | + new Instructions.DropInstruction(); | ||
76 | + final ObjectNode instructionJson = | ||
77 | + instructionCodec.encode(instruction, context); | ||
78 | + assertThat(instructionJson, matchesInstruction(instruction)); | ||
79 | + } | ||
80 | + | ||
81 | + /** | ||
82 | + * Tests the encoding of output instructions. | ||
83 | + */ | ||
84 | + @Test | ||
85 | + public void outputInstructionTest() { | ||
86 | + final Instructions.OutputInstruction instruction = | ||
87 | + Instructions.createOutput(PortNumber.portNumber(22)); | ||
88 | + final ObjectNode instructionJson = | ||
89 | + instructionCodec.encode(instruction, context); | ||
90 | + assertThat(instructionJson, matchesInstruction(instruction)); | ||
91 | + } | ||
92 | + | ||
93 | + /** | ||
94 | + * Tests the encoding of mod lambda instructions. | ||
95 | + */ | ||
96 | + @Test | ||
97 | + public void modLambdaInstructionTest() { | ||
98 | + final L0ModificationInstruction.ModLambdaInstruction instruction = | ||
99 | + (L0ModificationInstruction.ModLambdaInstruction) | ||
100 | + Instructions.modL0Lambda((short) 55); | ||
101 | + final ObjectNode instructionJson = | ||
102 | + instructionCodec.encode(instruction, context); | ||
103 | + assertThat(instructionJson, matchesInstruction(instruction)); | ||
104 | + } | ||
105 | + | ||
106 | + /** | ||
107 | + * Tests the encoding of mod ether instructions. | ||
108 | + */ | ||
109 | + @Test | ||
110 | + public void modEtherInstructionTest() { | ||
111 | + final L2ModificationInstruction.ModEtherInstruction instruction = | ||
112 | + (L2ModificationInstruction.ModEtherInstruction) | ||
113 | + Instructions.modL2Src(MacAddress.valueOf("11:22:33:44:55:66")); | ||
114 | + final ObjectNode instructionJson = | ||
115 | + instructionCodec.encode(instruction, context); | ||
116 | + assertThat(instructionJson, matchesInstruction(instruction)); | ||
117 | + } | ||
118 | + | ||
119 | + /** | ||
120 | + * Tests the encoding of mod vlan id instructions. | ||
121 | + */ | ||
122 | + @Test | ||
123 | + public void modVlanIdInstructionTest() { | ||
124 | + final L2ModificationInstruction.ModVlanIdInstruction instruction = | ||
125 | + (L2ModificationInstruction.ModVlanIdInstruction) | ||
126 | + Instructions.modVlanId(VlanId.vlanId((short) 12)); | ||
127 | + | ||
128 | + final ObjectNode instructionJson = | ||
129 | + instructionCodec.encode(instruction, context); | ||
130 | + assertThat(instructionJson, matchesInstruction(instruction)); | ||
131 | + } | ||
132 | + | ||
133 | + /** | ||
134 | + * Tests the encoding of mod vlan pcp instructions. | ||
135 | + */ | ||
136 | + @Test | ||
137 | + public void modVlanPcpInstructionTest() { | ||
138 | + final L2ModificationInstruction.ModVlanPcpInstruction instruction = | ||
139 | + (L2ModificationInstruction.ModVlanPcpInstruction) | ||
140 | + Instructions.modVlanPcp((byte) 9); | ||
141 | + final ObjectNode instructionJson = | ||
142 | + instructionCodec.encode(instruction, context); | ||
143 | + assertThat(instructionJson, matchesInstruction(instruction)); | ||
144 | + } | ||
145 | + | ||
146 | + /** | ||
147 | + * Tests the encoding of mod ip instructions. | ||
148 | + */ | ||
149 | + @Test | ||
150 | + public void modIPInstructionTest() { | ||
151 | + final IpAddress ip = IpPrefix.valueOf("1.2.3.4/24").address(); | ||
152 | + final L3ModificationInstruction.ModIPInstruction instruction = | ||
153 | + (L3ModificationInstruction.ModIPInstruction) | ||
154 | + Instructions.modL3Src(ip); | ||
155 | + final ObjectNode instructionJson = | ||
156 | + instructionCodec.encode(instruction, context); | ||
157 | + assertThat(instructionJson, matchesInstruction(instruction)); | ||
158 | + } | ||
159 | + | ||
160 | + /** | ||
161 | + * Tests the encoding of mod MPLS label instructions. | ||
162 | + */ | ||
163 | + @Test | ||
164 | + public void modMplsLabelInstructionTest() { | ||
165 | + final L2ModificationInstruction.ModMplsLabelInstruction instruction = | ||
166 | + (L2ModificationInstruction.ModMplsLabelInstruction) | ||
167 | + Instructions.modMplsLabel(99); | ||
168 | + final ObjectNode instructionJson = | ||
169 | + instructionCodec.encode(instruction, context); | ||
170 | + assertThat(instructionJson, matchesInstruction(instruction)); | ||
171 | + } | ||
172 | + | ||
173 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.codec.impl; | ||
17 | + | ||
18 | +import org.hamcrest.Description; | ||
19 | +import org.hamcrest.Matcher; | ||
20 | +import org.hamcrest.TypeSafeDiagnosingMatcher; | ||
21 | +import org.onosproject.net.flow.instructions.Instruction; | ||
22 | + | ||
23 | +import com.fasterxml.jackson.databind.JsonNode; | ||
24 | + | ||
25 | +import static org.onosproject.codec.impl.EthernetJsonMatcher.matchesEthernet; | ||
26 | +import static org.onosproject.net.flow.instructions.Instructions.*; | ||
27 | +import static org.onosproject.net.flow.instructions.L0ModificationInstruction.*; | ||
28 | +import static org.onosproject.net.flow.instructions.L2ModificationInstruction.*; | ||
29 | +import static org.onosproject.net.flow.instructions.L3ModificationInstruction.*; | ||
30 | + | ||
31 | +/** | ||
32 | + * Hamcrest matcher for instructions. | ||
33 | + */ | ||
34 | +public class InstructionJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> { | ||
35 | + | ||
36 | + private final Instruction instruction; | ||
37 | + | ||
38 | + public InstructionJsonMatcher(Instruction instructionValue) { | ||
39 | + instruction = instructionValue; | ||
40 | + } | ||
41 | + | ||
42 | + /** | ||
43 | + * Matches the contents of a push header instruction. | ||
44 | + * | ||
45 | + * @param instructionJson JSON instruction to match | ||
46 | + * @return true if contents match, false otherwise | ||
47 | + */ | ||
48 | + private boolean matchPushHeaderInstruction(JsonNode instructionJson, | ||
49 | + Description description) { | ||
50 | + PushHeaderInstructions instructionToMatch = | ||
51 | + (PushHeaderInstructions) instruction; | ||
52 | + final String jsonSubtype = instructionJson.get("subtype").textValue(); | ||
53 | + if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { | ||
54 | + description.appendText("subtype was " + jsonSubtype); | ||
55 | + return false; | ||
56 | + } | ||
57 | + | ||
58 | + final String jsonType = instructionJson.get("type").textValue(); | ||
59 | + if (!instructionToMatch.type().name().equals(jsonType)) { | ||
60 | + description.appendText("type was " + jsonType); | ||
61 | + return false; | ||
62 | + } | ||
63 | + | ||
64 | + final JsonNode ethJson = instructionJson.get("ethernetType"); | ||
65 | + if (ethJson == null) { | ||
66 | + description.appendText("ethernetType was not null"); | ||
67 | + return false; | ||
68 | + } | ||
69 | + | ||
70 | + final Matcher ethernetMatcher = | ||
71 | + matchesEthernet(instructionToMatch.ethernetType()); | ||
72 | + final boolean ethernetMatches = ethernetMatcher.matches(ethJson); | ||
73 | + if (!ethernetMatches) { | ||
74 | + ethernetMatcher.describeMismatch(ethernetMatcher, description); | ||
75 | + return false; | ||
76 | + } | ||
77 | + return true; | ||
78 | + } | ||
79 | + | ||
80 | + /** | ||
81 | + * Matches the contents of an output instruction. | ||
82 | + * | ||
83 | + * @param instructionJson JSON instruction to match | ||
84 | + * @return true if contents match, false otherwise | ||
85 | + */ | ||
86 | + private boolean matchOutputInstruction(JsonNode instructionJson, | ||
87 | + Description description) { | ||
88 | + OutputInstruction instructionToMatch = (OutputInstruction) instruction; | ||
89 | + | ||
90 | + final String jsonType = instructionJson.get("type").textValue(); | ||
91 | + if (!instructionToMatch.type().name().equals(jsonType)) { | ||
92 | + description.appendText("type was " + jsonType); | ||
93 | + return false; | ||
94 | + } | ||
95 | + | ||
96 | + final long jsonPort = instructionJson.get("port").asLong(); | ||
97 | + if (instructionToMatch.port().toLong() != jsonPort) { | ||
98 | + description.appendText("port was " + jsonPort); | ||
99 | + return false; | ||
100 | + } | ||
101 | + | ||
102 | + return true; | ||
103 | + } | ||
104 | + | ||
105 | + /** | ||
106 | + * Matches the contents of a mod lambda instruction. | ||
107 | + * | ||
108 | + * @param instructionJson JSON instruction to match | ||
109 | + * @return true if contents match, false otherwise | ||
110 | + */ | ||
111 | + private boolean matchModLambdaInstruction(JsonNode instructionJson, | ||
112 | + Description description) { | ||
113 | + ModLambdaInstruction instructionToMatch = | ||
114 | + (ModLambdaInstruction) instruction; | ||
115 | + final String jsonSubtype = instructionJson.get("subtype").textValue(); | ||
116 | + if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { | ||
117 | + description.appendText("subtype was " + jsonSubtype); | ||
118 | + return false; | ||
119 | + } | ||
120 | + | ||
121 | + final String jsonType = instructionJson.get("type").textValue(); | ||
122 | + if (!instructionToMatch.type().name().equals(jsonType)) { | ||
123 | + description.appendText("type was " + jsonType); | ||
124 | + return false; | ||
125 | + } | ||
126 | + | ||
127 | + final long jsonLambda = instructionJson.get("lambda").shortValue(); | ||
128 | + if (instructionToMatch.lambda() != jsonLambda) { | ||
129 | + description.appendText("lambda was " + jsonLambda); | ||
130 | + return false; | ||
131 | + } | ||
132 | + | ||
133 | + return true; | ||
134 | + } | ||
135 | + | ||
136 | + /** | ||
137 | + * Matches the contents of a mod lambda instruction. | ||
138 | + * | ||
139 | + * @param instructionJson JSON instruction to match | ||
140 | + * @return true if contents match, false otherwise | ||
141 | + */ | ||
142 | + private boolean matchModEtherInstruction(JsonNode instructionJson, | ||
143 | + Description description) { | ||
144 | + ModEtherInstruction instructionToMatch = | ||
145 | + (ModEtherInstruction) instruction; | ||
146 | + final String jsonSubtype = instructionJson.get("subtype").textValue(); | ||
147 | + if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { | ||
148 | + description.appendText("subtype was " + jsonSubtype); | ||
149 | + return false; | ||
150 | + } | ||
151 | + | ||
152 | + final String jsonType = instructionJson.get("type").textValue(); | ||
153 | + if (!instructionToMatch.type().name().equals(jsonType)) { | ||
154 | + description.appendText("type was " + jsonType); | ||
155 | + return false; | ||
156 | + } | ||
157 | + | ||
158 | + final String jsonMac = instructionJson.get("mac").textValue(); | ||
159 | + final String mac = instructionToMatch.mac().toString(); | ||
160 | + if (!mac.equals(jsonMac)) { | ||
161 | + description.appendText("mac was " + jsonMac); | ||
162 | + return false; | ||
163 | + } | ||
164 | + | ||
165 | + return true; | ||
166 | + } | ||
167 | + | ||
168 | + /** | ||
169 | + * Matches the contents of a mod vlan id instruction. | ||
170 | + * | ||
171 | + * @param instructionJson JSON instruction to match | ||
172 | + * @return true if contents match, false otherwise | ||
173 | + */ | ||
174 | + private boolean matchModVlanIdInstruction(JsonNode instructionJson, | ||
175 | + Description description) { | ||
176 | + ModVlanIdInstruction instructionToMatch = | ||
177 | + (ModVlanIdInstruction) instruction; | ||
178 | + final String jsonSubtype = instructionJson.get("subtype").textValue(); | ||
179 | + if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { | ||
180 | + description.appendText("subtype was " + jsonSubtype); | ||
181 | + return false; | ||
182 | + } | ||
183 | + | ||
184 | + final String jsonType = instructionJson.get("type").textValue(); | ||
185 | + if (!instructionToMatch.type().name().equals(jsonType)) { | ||
186 | + description.appendText("type was " + jsonType); | ||
187 | + return false; | ||
188 | + } | ||
189 | + | ||
190 | + final short jsonVlanId = instructionJson.get("vlanId").shortValue(); | ||
191 | + final short vlanId = instructionToMatch.vlanId().toShort(); | ||
192 | + if (jsonVlanId != vlanId) { | ||
193 | + description.appendText("vlan id was " + jsonVlanId); | ||
194 | + return false; | ||
195 | + } | ||
196 | + | ||
197 | + return true; | ||
198 | + } | ||
199 | + | ||
200 | + /** | ||
201 | + * Matches the contents of a mod vlan pcp instruction. | ||
202 | + * | ||
203 | + * @param instructionJson JSON instruction to match | ||
204 | + * @return true if contents match, false otherwise | ||
205 | + */ | ||
206 | + private boolean matchModVlanPcpInstruction(JsonNode instructionJson, | ||
207 | + Description description) { | ||
208 | + ModVlanPcpInstruction instructionToMatch = | ||
209 | + (ModVlanPcpInstruction) instruction; | ||
210 | + final String jsonSubtype = instructionJson.get("subtype").textValue(); | ||
211 | + if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { | ||
212 | + description.appendText("subtype was " + jsonSubtype); | ||
213 | + return false; | ||
214 | + } | ||
215 | + | ||
216 | + final String jsonType = instructionJson.get("type").textValue(); | ||
217 | + if (!instructionToMatch.type().name().equals(jsonType)) { | ||
218 | + description.appendText("type was " + jsonType); | ||
219 | + return false; | ||
220 | + } | ||
221 | + | ||
222 | + final short jsonVlanPcp = instructionJson.get("vlanPcp").shortValue(); | ||
223 | + final short vlanId = instructionToMatch.vlanPcp(); | ||
224 | + if (jsonVlanPcp != vlanId) { | ||
225 | + description.appendText("vlan pcp was " + jsonVlanPcp); | ||
226 | + return false; | ||
227 | + } | ||
228 | + | ||
229 | + return true; | ||
230 | + } | ||
231 | + | ||
232 | + /** | ||
233 | + * Matches the contents of a mod ip instruction. | ||
234 | + * | ||
235 | + * @param instructionJson JSON instruction to match | ||
236 | + * @return true if contents match, false otherwise | ||
237 | + */ | ||
238 | + private boolean matchModIpInstruction(JsonNode instructionJson, | ||
239 | + Description description) { | ||
240 | + ModIPInstruction instructionToMatch = | ||
241 | + (ModIPInstruction) instruction; | ||
242 | + final String jsonSubtype = instructionJson.get("subtype").textValue(); | ||
243 | + if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { | ||
244 | + description.appendText("subtype was " + jsonSubtype); | ||
245 | + return false; | ||
246 | + } | ||
247 | + | ||
248 | + final String jsonType = instructionJson.get("type").textValue(); | ||
249 | + if (!instructionToMatch.type().name().equals(jsonType)) { | ||
250 | + description.appendText("type was " + jsonType); | ||
251 | + return false; | ||
252 | + } | ||
253 | + | ||
254 | + final String jsonIp = instructionJson.get("ip").textValue(); | ||
255 | + final String ip = instructionToMatch.ip().toString(); | ||
256 | + if (!ip.equals(jsonIp)) { | ||
257 | + description.appendText("ip was " + jsonIp); | ||
258 | + return false; | ||
259 | + } | ||
260 | + | ||
261 | + return true; | ||
262 | + } | ||
263 | + | ||
264 | + /** | ||
265 | + * Matches the contents of a mod ip instruction. | ||
266 | + * | ||
267 | + * @param instructionJson JSON instruction to match | ||
268 | + * @return true if contents match, false otherwise | ||
269 | + */ | ||
270 | + private boolean matchModMplsLabelInstruction(JsonNode instructionJson, | ||
271 | + Description description) { | ||
272 | + ModMplsLabelInstruction instructionToMatch = | ||
273 | + (ModMplsLabelInstruction) instruction; | ||
274 | + final String jsonSubtype = instructionJson.get("subtype").textValue(); | ||
275 | + if (!instructionToMatch.subtype().name().equals(jsonSubtype)) { | ||
276 | + description.appendText("subtype was " + jsonSubtype); | ||
277 | + return false; | ||
278 | + } | ||
279 | + | ||
280 | + final String jsonType = instructionJson.get("type").textValue(); | ||
281 | + if (!instructionToMatch.type().name().equals(jsonType)) { | ||
282 | + description.appendText("type was " + jsonType); | ||
283 | + return false; | ||
284 | + } | ||
285 | + | ||
286 | + final int jsonLabel = instructionJson.get("label").intValue(); | ||
287 | + final int label = instructionToMatch.label(); | ||
288 | + if (label != jsonLabel) { | ||
289 | + description.appendText("ip was " + jsonLabel); | ||
290 | + return false; | ||
291 | + } | ||
292 | + | ||
293 | + return true; | ||
294 | + } | ||
295 | + | ||
296 | + @Override | ||
297 | + public boolean matchesSafely(JsonNode jsonInstruction, Description description) { | ||
298 | + | ||
299 | + // check type | ||
300 | + final JsonNode jsonTypeNode = jsonInstruction.get("type"); | ||
301 | + final String jsonType = jsonTypeNode.textValue(); | ||
302 | + final String type = instruction.type().name(); | ||
303 | + if (!jsonType.equals(type)) { | ||
304 | + description.appendText("type was " + type); | ||
305 | + return false; | ||
306 | + } | ||
307 | + | ||
308 | + if (instruction instanceof PushHeaderInstructions) { | ||
309 | + return matchPushHeaderInstruction(jsonInstruction, description); | ||
310 | + } else if (instruction instanceof DropInstruction) { | ||
311 | + return true; | ||
312 | + } else if (instruction instanceof OutputInstruction) { | ||
313 | + return matchOutputInstruction(jsonInstruction, description); | ||
314 | + } else if (instruction instanceof ModLambdaInstruction) { | ||
315 | + return matchModLambdaInstruction(jsonInstruction, description); | ||
316 | + } else if (instruction instanceof ModEtherInstruction) { | ||
317 | + return matchModEtherInstruction(jsonInstruction, description); | ||
318 | + } else if (instruction instanceof ModVlanIdInstruction) { | ||
319 | + return matchModVlanIdInstruction(jsonInstruction, description); | ||
320 | + } else if (instruction instanceof ModVlanPcpInstruction) { | ||
321 | + return matchModVlanPcpInstruction(jsonInstruction, description); | ||
322 | + } else if (instruction instanceof ModIPInstruction) { | ||
323 | + return matchModIpInstruction(jsonInstruction, description); | ||
324 | + } else if (instruction instanceof ModMplsLabelInstruction) { | ||
325 | + return matchModMplsLabelInstruction(jsonInstruction, description); | ||
326 | + } | ||
327 | + | ||
328 | + return false; | ||
329 | + } | ||
330 | + | ||
331 | + @Override | ||
332 | + public void describeTo(Description description) { | ||
333 | + description.appendText(instruction.toString()); | ||
334 | + } | ||
335 | + | ||
336 | + /** | ||
337 | + * Factory to allocate an instruction matcher. | ||
338 | + * | ||
339 | + * @param instruction instruction object we are looking for | ||
340 | + * @return matcher | ||
341 | + */ | ||
342 | + public static InstructionJsonMatcher matchesInstruction(Instruction instruction) { | ||
343 | + return new InstructionJsonMatcher(instruction); | ||
344 | + } | ||
345 | +} |
1 | +/* | ||
2 | + * Copyright 2015 Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.codec.impl; | ||
17 | + | ||
18 | +import org.onosproject.codec.CodecContext; | ||
19 | +import org.onosproject.codec.JsonCodec; | ||
20 | + | ||
21 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
22 | + | ||
23 | +/** | ||
24 | + * Mock codec context for use in codec unit tests. | ||
25 | + */ | ||
26 | +public final class MockCodecContext implements CodecContext { | ||
27 | + | ||
28 | + private ObjectMapper mapper = new ObjectMapper(); | ||
29 | + private CodecManager manager = new CodecManager(); | ||
30 | + | ||
31 | + public MockCodecContext() { | ||
32 | + manager.activate(); | ||
33 | + } | ||
34 | + | ||
35 | + @Override | ||
36 | + public ObjectMapper mapper() { | ||
37 | + return mapper; | ||
38 | + } | ||
39 | + | ||
40 | + @Override | ||
41 | + @SuppressWarnings("unchecked") | ||
42 | + public <T> JsonCodec<T> codec(Class<T> entityClass) { | ||
43 | + return manager.getCodec(entityClass); | ||
44 | + } | ||
45 | + | ||
46 | + @Override | ||
47 | + public <T> T get(Class<T> serviceClass) { | ||
48 | + return null; | ||
49 | + } | ||
50 | + | ||
51 | +} |
-
Please register or login to post a comment