1 package org.opendaylight.controller.sal.compability;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6 import static org.opendaylight.controller.sal.compability.ProtocolConstants.ETHERNET_ARP;
7 import static org.opendaylight.controller.sal.compability.ProtocolConstants.SCTP;
8 import static org.opendaylight.controller.sal.compability.ProtocolConstants.TCP;
9 import static org.opendaylight.controller.sal.compability.ProtocolConstants.UDP;
11 import java.util.ArrayList;
12 import java.util.List;
14 import org.junit.Test;
15 import org.opendaylight.controller.sal.action.*;
16 import org.opendaylight.controller.sal.flowprogrammer.Flow;
17 import org.opendaylight.controller.sal.match.Match;
18 import org.opendaylight.controller.sal.match.MatchType;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.*;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.Address;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv4;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.Layer3Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.Layer4Match;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.ArpMatch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv4Match;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._3.match.Ipv6Match;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.SctpMatch;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.TcpMatch;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.layer._4.match.UdpMatch;
32 import com.google.common.net.InetAddresses;
34 public class TestFromSalConversionsUtils {
35 private enum MtchType {
36 other, ipv4, ipv6, arp, sctp, tcp, udp
40 public void testFromSalConversion() {
42 Flow salFlow = prepareSalFlowCommon();
43 NodeFlow odNodeFlow = FromSalConversionsUtils.flowAdded(salFlow);
45 checkOdFlow(odNodeFlow);
47 odNodeFlow = FromSalConversionsUtils.flowAdded(prepareSalMatch(salFlow, MtchType.other));
48 checkOdMatch(odNodeFlow.getMatch(), MtchType.other);
50 odNodeFlow = FromSalConversionsUtils.flowAdded(prepareSalMatch(salFlow, MtchType.arp));
51 checkOdMatch(odNodeFlow.getMatch(), MtchType.arp);
53 odNodeFlow = FromSalConversionsUtils.flowAdded(prepareSalMatch(salFlow, MtchType.ipv4));
54 checkOdMatch(odNodeFlow.getMatch(), MtchType.ipv4);
56 odNodeFlow = FromSalConversionsUtils.flowAdded(prepareSalMatch(salFlow, MtchType.ipv6));
57 checkOdMatch(odNodeFlow.getMatch(), MtchType.ipv6);
59 odNodeFlow = FromSalConversionsUtils.flowAdded(prepareSalMatch(salFlow, MtchType.sctp));
60 checkOdMatch(odNodeFlow.getMatch(), MtchType.sctp);
62 odNodeFlow = FromSalConversionsUtils.flowAdded(prepareSalMatch(salFlow, MtchType.tcp));
63 checkOdMatch(odNodeFlow.getMatch(), MtchType.tcp);
65 odNodeFlow = FromSalConversionsUtils.flowAdded(prepareSalMatch(salFlow, MtchType.udp));
66 checkOdMatch(odNodeFlow.getMatch(), MtchType.udp);
69 private void checkOdMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match match,
73 assertEquals("Ether type is incorrect.", ETHERNET_ARP, (long) match.getEthernetMatch().getEthernetType()
74 .getType().getValue());
75 Layer3Match layer3Match = match.getLayer3Match();
76 boolean arpFound = false;
77 if (layer3Match instanceof ArpMatch) {
78 assertEquals("Source IP address is wrong.", "192.168.100.100", ((ArpMatch) layer3Match)
79 .getArpSourceTransportAddress().getValue());
80 assertEquals("Destination IP address is wrong.", "192.168.100.101", ((ArpMatch) layer3Match)
81 .getArpTargetTransportAddress().getValue());
82 assertEquals("Source MAC address is wrong.", "aa:bb:cc:dd:ee:ff", ((ArpMatch) layer3Match)
83 .getArpSourceHardwareAddress().getAddress().getValue());
84 assertEquals("Destination MAC address is wrong.", "ff:ee:dd:cc:bb:aa", ((ArpMatch) layer3Match)
85 .getArpTargetHardwareAddress().getAddress().getValue());
88 assertNotNull("Arp wasn't found", arpFound);
91 assertEquals("Ether type is incorrect.", 0xffff, (long) match.getEthernetMatch().getEthernetType()
92 .getType().getValue());
93 boolean ipv4Found = false;
94 layer3Match = match.getLayer3Match();
95 if (layer3Match instanceof Ipv4Match) {
96 assertEquals("Source IP address is wrong.", "192.168.100.102", ((Ipv4Match) layer3Match)
97 .getIpv4Source().getValue());
98 assertEquals("Destination IP address is wrong.", "192.168.100.103", ((Ipv4Match) layer3Match)
99 .getIpv4Destination().getValue());
101 assertNotNull("Ipv4 wasn't found", ipv4Found);
104 assertEquals("Ether type is incorrect.", 0xffff, (long) match.getEthernetMatch().getEthernetType()
105 .getType().getValue());
106 boolean ipv6Found = false;
107 layer3Match = match.getLayer3Match();
108 if (layer3Match instanceof Ipv6Match) {
109 assertEquals("Source IP address is wrong.", "2001:db8:85a3::8a2e:370:7335", ((Ipv6Match) layer3Match)
110 .getIpv6Source().getValue());
111 assertEquals("Destination IP address is wrong.", "2001:db8:85a3::8a2e:370:7336",
112 ((Ipv6Match) layer3Match).getIpv6Destination().getValue());
114 assertNotNull("Ipv6 wasn't found", ipv6Found);
117 assertEquals("Source MAC address is wrong.", "24:77:03:7C:C5:F1", match.getEthernetMatch()
118 .getEthernetSource().getAddress().getValue());
119 assertEquals("Destinatio MAC address is wrong.", "3C:A9:F4:00:E0:C8", match.getEthernetMatch()
120 .getEthernetDestination().getAddress().getValue());
121 assertEquals("Vlan ID is wrong.", (Integer) 0xfff, match.getVlanMatch().getVlanId().getVlanId().getValue());
122 assertEquals("Vlan ID priority is wrong.", (short) 0x7, (short) match.getVlanMatch().getVlanPcp()
124 assertEquals("DCSP is wrong.", (short) 0x3f, (short) match.getIpMatch().getIpDscp().getValue());
127 boolean sctpFound = false;
128 assertEquals("Wrong protocol", SCTP, match.getIpMatch().getIpProtocol().byteValue());
129 Layer4Match layer4Match = match.getLayer4Match();
130 if (layer4Match instanceof SctpMatch) {
131 assertEquals("Sctp source port is incorrect.", 0xffff, (int) ((SctpMatch) layer4Match)
132 .getSctpSourcePort().getValue());
133 assertEquals("Sctp dest port is incorrect.", (int) 0xfffe, (int) ((SctpMatch) layer4Match)
134 .getSctpDestinationPort().getValue());
137 assertNotNull("Sctp wasn't found", sctpFound);
140 boolean tcpFound = false;
141 assertEquals("Wrong protocol", TCP, match.getIpMatch().getIpProtocol().byteValue());
142 layer4Match = match.getLayer4Match();
143 if (layer4Match instanceof TcpMatch) {
144 assertEquals("Tcp source port is incorrect.", (int) 0xabcd, (int) ((TcpMatch) layer4Match)
145 .getTcpSourcePort().getValue());
146 assertEquals("Tcp dest port is incorrect.", (int) 0xdcba, (int) ((TcpMatch) layer4Match)
147 .getTcpDestinationPort().getValue());
150 assertNotNull("Tcp wasn't found", tcpFound);
153 boolean udpFound = false;
154 assertEquals("Wrong protocol", UDP, match.getIpMatch().getIpProtocol().byteValue());
155 layer4Match = match.getLayer4Match();
156 if (layer4Match instanceof UdpMatch) {
157 assertEquals("Udp source port is incorrect.", (int) 0xcdef, (int) ((UdpMatch) layer4Match)
158 .getUdpSourcePort().getValue());
159 assertEquals("Udp dest port is incorrect.", (int) 0xfedc, (int) ((UdpMatch) layer4Match)
160 .getUdpDestinationPort().getValue());
163 assertNotNull("Udp wasn't found", udpFound);
169 private void checkOdFlow(NodeFlow odNodeFlow) {
170 assertEquals("Cookie is incorrect.", 9223372036854775807L, odNodeFlow.getCookie().longValue());
171 assertEquals("Hard timeout is incorrect.", 32765, odNodeFlow.getHardTimeout().shortValue());
172 assertEquals("Iddle timeout is incorrect.", 32766, odNodeFlow.getIdleTimeout().shortValue());
173 assertEquals("Priority is incorrect.", 32767, odNodeFlow.getPriority().shortValue());
175 checkOdActions(odNodeFlow.getAction());
178 private void checkOdActions(
179 List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action> actions) {
180 checkOdAction(actions, FloodAction.class, false);
181 checkOdAction(actions, FloodAllAction.class, false);
182 checkOdAction(actions, HwPathAction.class, false);
183 checkOdAction(actions, LoopbackAction.class, false);
184 checkOdAction(actions, PopVlanAction.class, false);
185 checkOdAction(actions, PushVlanAction.class, true);
186 checkOdAction(actions, SetDlDstAction.class, true);
187 checkOdAction(actions, SetDlSrcAction.class, true);
188 checkOdAction(actions, SetDlTypeAction.class, true);
189 checkOdAction(actions, SetNwTosAction.class, true);
190 checkOdAction(actions, SetNwDstAction.class, true);
191 checkOdAction(actions, SetNwSrcAction.class, true);
192 checkOdAction(actions, SetNextHopAction.class, true);
193 checkOdAction(actions, SetTpDstAction.class, true);
194 checkOdAction(actions, SetTpSrcAction.class, true);
195 checkOdAction(actions, SetVlanCfiAction.class, true);
196 checkOdAction(actions, SetVlanIdAction.class, true);
197 checkOdAction(actions, SetVlanPcpAction.class, true);
198 checkOdAction(actions, SwPathAction.class, false);
201 private void checkOdAction(
202 List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action> actions, Class<?> cl,
204 int numOfFoundActions = 0;
205 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action action : actions) {
206 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.Action innerAction = action
208 if (cl.isInstance(innerAction)) {
210 if (innerAction instanceof PushVlanAction) {
211 assertEquals("Wrong value of cfi in PushVlanAction.", (Integer) 1, ((PushVlanAction) innerAction)
212 .getCfi().getValue());
213 assertEquals("Wrong value of pcp in PushVlanAction.", (Integer) 7,
214 ((PushVlanAction) innerAction).getPcp());
215 assertEquals("Wrong value of tag in PushVlanAction.", (Integer) 0x8100,
216 ((PushVlanAction) innerAction).getTag());
217 assertEquals("Wrong value of vlad ID in PushVlanAction.", (Integer) 4095,
218 ((PushVlanAction) innerAction).getVlanId().getValue());
219 } else if (innerAction instanceof SetDlDstAction) {
220 assertEquals("Wrong MAC destination address in SetDlDstAction.", "3C:A9:F4:00:E0:C8", new String(
221 ((SetDlDstAction) innerAction).getAddress().getValue()));
222 } else if (innerAction instanceof SetDlSrcAction) {
223 assertEquals("Wrong MAC source address in SetDlDstAction.", "24:77:03:7C:C5:F1", new String(
224 ((SetDlSrcAction) innerAction).getAddress().getValue()));
225 } else if (innerAction instanceof SetDlTypeAction) {
226 assertEquals("Wrong data link type in SetDlTypeAction.", (long) 513,
227 (long) ((SetDlTypeAction) innerAction).getDlType().getValue());
228 } else if (innerAction instanceof SetNextHopAction) {
229 Address address = ((SetNextHopAction) innerAction).getAddress();
230 boolean ipv4AddressFound = false;
231 if (address instanceof Ipv4) {
232 ipv4AddressFound = true;
233 assertEquals("Wrong IP address type in SetNextHopAction.", "192.168.100.100", ((Ipv4) address)
234 .getIpv4Address().getValue());
236 assertTrue("Ipv4 address wasn't found.", ipv4AddressFound);
237 } else if (innerAction instanceof SetNwTosAction) {
238 assertEquals("Wrong TOS in SetNwTosAction.", (Integer) 63, ((SetNwTosAction) innerAction).getTos());
239 } else if (innerAction instanceof SetNwDstAction) {
240 Address address = ((SetNwDstAction) innerAction).getAddress();
241 boolean ipv4AddressFound = false;
242 if (address instanceof Ipv4) {
243 ipv4AddressFound = true;
244 assertEquals("Wrong IP address type in SetNwDstAction.", "192.168.100.101", ((Ipv4) address)
245 .getIpv4Address().getValue());
247 assertTrue("Ipv4 address wasn't found.", ipv4AddressFound);
248 } else if (innerAction instanceof SetNwSrcAction) {
249 Address address = ((SetNwSrcAction) innerAction).getAddress();
250 boolean ipv4AddressFound = false;
251 if (address instanceof Ipv4) {
252 ipv4AddressFound = true;
253 assertEquals("Wrong IP address type in SetNwSrcAction.", "192.168.100.102", ((Ipv4) address)
254 .getIpv4Address().getValue());
256 assertTrue("Ipv4 address wasn't found.", ipv4AddressFound);
257 } else if (innerAction instanceof SetTpDstAction) {
258 assertEquals("Port number is incorrect in SetTpDstAction.", (Integer) 65534,
259 ((SetTpDstAction) innerAction).getPort().getValue());
260 } else if (innerAction instanceof SetTpSrcAction) {
261 assertEquals("Port number is incorrect in SetTpSrcAction.", (Integer) 65535,
262 ((SetTpSrcAction) innerAction).getPort().getValue());
263 } else if (innerAction instanceof SetVlanCfiAction) {
264 assertEquals("Vlan cfi number is incorrect in SetVlanCfiAction.", (Integer) 1,
265 ((SetVlanCfiAction) innerAction).getVlanCfi().getValue());
266 } else if (innerAction instanceof SetVlanIdAction) {
267 assertEquals("Vlan id number is incorrect in SetVlanIdAction.", (Integer) 4095,
268 ((SetVlanIdAction) innerAction).getVlanId().getValue());
269 } else if (innerAction instanceof SetVlanPcpAction) {
270 assertEquals("Vlan pcp number is incorrect in SetVlanPcpAction.", new Short((short) 7),
271 ((SetVlanPcpAction) innerAction).getVlanPcp().getValue());
275 assertEquals("Incorrrect number of action " + cl.getName() + ".", 1, numOfFoundActions);
279 private Flow prepareSalFlowCommon() {
280 Flow salFlow = new Flow();
281 salFlow.setId(9223372036854775807L);
282 salFlow.setHardTimeout((short) 32765);
283 salFlow.setIdleTimeout((short) 32766);
284 salFlow.setPriority((short) 32767);
285 salFlow.setActions(prepareSalActions());
286 salFlow.setMatch(new Match());
291 private Flow prepareSalMatch(Flow salFlow, MtchType mt) {
292 Match salMatch = new Match();
295 salMatch.setField(MatchType.DL_TYPE, ETHERNET_ARP);
296 salMatch.setField(MatchType.NW_SRC, InetAddresses.forString("192.168.100.100"));
297 salMatch.setField(MatchType.NW_DST, InetAddresses.forString("192.168.100.101"));
298 salMatch.setField(MatchType.DL_SRC, "aa:bb:cc:dd:ee:ff".getBytes());
299 salMatch.setField(MatchType.DL_DST, "ff:ee:dd:cc:bb:aa".getBytes());
302 salMatch.setField(MatchType.DL_TYPE, (short) 0xffff);
303 salMatch.setField(MatchType.NW_SRC, InetAddresses.forString("192.168.100.102"));
304 salMatch.setField(MatchType.NW_DST, InetAddresses.forString("192.168.100.103"));
307 salMatch.setField(MatchType.DL_TYPE, (short) 0xffff);
308 salMatch.setField(MatchType.NW_SRC, InetAddresses.forString("2001:0db8:85a3:0000:0000:8a2e:0370:7335"));
309 salMatch.setField(MatchType.NW_DST, InetAddresses.forString("2001:0db8:85a3:0000:0000:8a2e:0370:7336"));
312 salMatch.setField(MatchType.DL_SRC, "24:77:03:7C:C5:F1".getBytes());
313 salMatch.setField(MatchType.DL_DST, "3C:A9:F4:00:E0:C8".getBytes());
314 salMatch.setField(MatchType.DL_VLAN, (short) 0xfff);
315 salMatch.setField(MatchType.DL_VLAN_PR, (byte) 0x7);
316 salMatch.setField(MatchType.NW_TOS, (byte) 0x3f);
319 salMatch.setField(MatchType.NW_PROTO, SCTP);
320 salMatch.setField(MatchType.TP_SRC, (short) 0xffff);
321 salMatch.setField(MatchType.TP_DST, (short) 0xfffe);
324 salMatch.setField(MatchType.NW_PROTO, TCP);
325 salMatch.setField(MatchType.TP_SRC, (short) 0xabcd);
326 salMatch.setField(MatchType.TP_DST, (short) 0xdcba);
329 salMatch.setField(MatchType.NW_PROTO, UDP);
330 salMatch.setField(MatchType.TP_SRC, (short) 0xcdef);
331 salMatch.setField(MatchType.TP_DST, (short) 0xfedc);
338 salFlow.setMatch(salMatch);
342 private List<Action> prepareSalActions() {
343 List<Action> salActions = new ArrayList<>();
344 salActions.add(new Flood());
345 salActions.add(new FloodAll());
346 salActions.add(new HwPath());
347 salActions.add(new Loopback());
348 // salActions.add(new Output //TODO: mapping is missing
349 salActions.add(new PopVlan());
350 salActions.add(new PushVlan(0x8100, 7, 1, 4095));
351 salActions.add(new SetDlDst("3C:A9:F4:00:E0:C8".getBytes()));
352 salActions.add(new SetDlSrc("24:77:03:7C:C5:F1".getBytes()));
353 salActions.add(new SetDlType(513));
354 salActions.add(new SetNextHop(InetAddresses.forString("192.168.100.100")));
355 salActions.add(new SetNwDst(InetAddresses.forString("192.168.100.101")));
356 salActions.add(new SetNwSrc(InetAddresses.forString("192.168.100.102")));
357 salActions.add(new SetNwTos(63));
358 salActions.add(new SetTpDst(65534));
359 salActions.add(new SetTpSrc(65535));
360 salActions.add(new SetVlanCfi(1));
361 salActions.add(new SetVlanId(4095));
362 salActions.add(new SetVlanPcp(7));
363 salActions.add(new SwPath());