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.Action;
16 import org.opendaylight.controller.sal.action.Flood;
17 import org.opendaylight.controller.sal.action.FloodAll;
18 import org.opendaylight.controller.sal.action.HwPath;
19 import org.opendaylight.controller.sal.action.Loopback;
20 import org.opendaylight.controller.sal.action.PopVlan;
21 import org.opendaylight.controller.sal.action.PushVlan;
22 import org.opendaylight.controller.sal.action.SetDlDst;
23 import org.opendaylight.controller.sal.action.SetDlSrc;
24 import org.opendaylight.controller.sal.action.SetDlType;
25 import org.opendaylight.controller.sal.action.SetNextHop;
26 import org.opendaylight.controller.sal.action.SetNwDst;
27 import org.opendaylight.controller.sal.action.SetNwSrc;
28 import org.opendaylight.controller.sal.action.SetNwTos;
29 import org.opendaylight.controller.sal.action.SetTpDst;
30 import org.opendaylight.controller.sal.action.SetTpSrc;
31 import org.opendaylight.controller.sal.action.SetVlanCfi;
32 import org.opendaylight.controller.sal.action.SetVlanId;
33 import org.opendaylight.controller.sal.action.SetVlanPcp;
34 import org.opendaylight.controller.sal.action.SwPath;
35 import org.opendaylight.controller.sal.flowprogrammer.Flow;
36 import org.opendaylight.controller.sal.match.Match;
37 import org.opendaylight.controller.sal.match.MatchType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.FloodAction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.FloodAllAction;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.HwPathAction;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.LoopbackAction;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.PopVlanAction;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.PushVlanAction;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetDlDstAction;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetDlSrcAction;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetDlTypeAction;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetNextHopAction;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetNwDstAction;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetNwSrcAction;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetNwTosAction;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetTpDstAction;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetTpSrcAction;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetVlanCfiAction;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetVlanIdAction;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SetVlanPcpAction;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.SwPathAction;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.Address;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv4;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
69 import com.google.common.net.InetAddresses;
71 public class TestFromSalConversionsUtils {
72 private enum MtchType {
73 other, ipv4, ipv6, arp, sctp, tcp, udp
77 public void testFromSalConversion() {
79 Flow salFlow = prepareSalFlowCommon();
80 NodeFlow odNodeFlow = MDFlowMapping.flowAdded(salFlow);
82 checkOdFlow(odNodeFlow);
84 odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.other));
85 checkOdMatch(odNodeFlow.getMatch(), MtchType.other);
87 odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.arp));
88 checkOdMatch(odNodeFlow.getMatch(), MtchType.arp);
90 odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.ipv4));
91 checkOdMatch(odNodeFlow.getMatch(), MtchType.ipv4);
93 odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.ipv6));
94 checkOdMatch(odNodeFlow.getMatch(), MtchType.ipv6);
96 odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.sctp));
97 checkOdMatch(odNodeFlow.getMatch(), MtchType.sctp);
99 odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.tcp));
100 checkOdMatch(odNodeFlow.getMatch(), MtchType.tcp);
102 odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.udp));
103 checkOdMatch(odNodeFlow.getMatch(), MtchType.udp);
106 private void checkOdMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match match,
110 assertEquals("Ether type is incorrect.", ETHERNET_ARP, (long) match.getEthernetMatch().getEthernetType()
111 .getType().getValue());
112 Layer3Match layer3Match = match.getLayer3Match();
113 boolean arpFound = false;
114 if (layer3Match instanceof ArpMatch) {
115 assertEquals("Source IP address is wrong.", "192.168.100.100", ((ArpMatch) layer3Match)
116 .getArpSourceTransportAddress().getValue());
117 assertEquals("Destination IP address is wrong.", "192.168.100.101", ((ArpMatch) layer3Match)
118 .getArpTargetTransportAddress().getValue());
119 assertEquals("Source MAC address is wrong.", "ff:ee:dd:cc:bb:aa", ((ArpMatch) layer3Match)
120 .getArpSourceHardwareAddress().getAddress().getValue());
121 assertEquals("Destination MAC address is wrong.", "ff:ee:dd:cc:bb:aa", ((ArpMatch) layer3Match)
122 .getArpTargetHardwareAddress().getAddress().getValue());
125 assertNotNull("Arp wasn't found", arpFound);
128 assertEquals("Ether type is incorrect.", 0xffff, (long) match.getEthernetMatch().getEthernetType()
129 .getType().getValue());
130 boolean ipv4Found = false;
131 layer3Match = match.getLayer3Match();
132 if (layer3Match instanceof Ipv4Match) {
133 assertEquals("Source IP address is wrong.", "192.168.100.102", ((Ipv4Match) layer3Match)
134 .getIpv4Source().getValue());
135 assertEquals("Destination IP address is wrong.", "192.168.100.103", ((Ipv4Match) layer3Match)
136 .getIpv4Destination().getValue());
138 assertNotNull("Ipv4 wasn't found", ipv4Found);
141 assertEquals("Ether type is incorrect.", 0xffff, (long) match.getEthernetMatch().getEthernetType()
142 .getType().getValue());
143 boolean ipv6Found = false;
144 layer3Match = match.getLayer3Match();
145 if (layer3Match instanceof Ipv6Match) {
146 assertEquals("Source IP address is wrong.", "2001:db8:85a3::8a2e:370:7335", ((Ipv6Match) layer3Match)
147 .getIpv6Source().getValue());
148 assertEquals("Destination IP address is wrong.", "2001:db8:85a3::8a2e:370:7336",
149 ((Ipv6Match) layer3Match).getIpv6Destination().getValue());
151 assertNotNull("Ipv6 wasn't found", ipv6Found);
154 assertEquals("Source MAC address is wrong.", "ff:ee:dd:cc:bb:aa", match.getEthernetMatch()
155 .getEthernetSource().getAddress().getValue());
156 assertEquals("Destinatio MAC address is wrong.", "ff:ee:dd:cc:bb:aa", match.getEthernetMatch()
157 .getEthernetDestination().getAddress().getValue());
158 assertEquals("Vlan ID is wrong.", (Integer) 0xfff, match.getVlanMatch().getVlanId().getVlanId().getValue());
159 assertEquals("Vlan ID priority is wrong.", (short) 0x7, (short) match.getVlanMatch().getVlanPcp()
161 assertEquals("DCSP is wrong.", (short) 0x3f, (short) match.getIpMatch().getIpDscp().getValue());
164 boolean sctpFound = false;
165 assertEquals("Wrong protocol", SCTP, match.getIpMatch().getIpProtocol().byteValue());
166 Layer4Match layer4Match = match.getLayer4Match();
167 if (layer4Match instanceof SctpMatch) {
168 assertEquals("Sctp source port is incorrect.", 0xffff, (int) ((SctpMatch) layer4Match)
169 .getSctpSourcePort().getValue());
170 assertEquals("Sctp dest port is incorrect.", (int) 0xfffe, (int) ((SctpMatch) layer4Match)
171 .getSctpDestinationPort().getValue());
174 assertNotNull("Sctp wasn't found", sctpFound);
177 boolean tcpFound = false;
178 assertEquals("Wrong protocol", TCP, match.getIpMatch().getIpProtocol().byteValue());
179 layer4Match = match.getLayer4Match();
180 if (layer4Match instanceof TcpMatch) {
181 assertEquals("Tcp source port is incorrect.", (int) 0xabcd, (int) ((TcpMatch) layer4Match)
182 .getTcpSourcePort().getValue());
183 assertEquals("Tcp dest port is incorrect.", (int) 0xdcba, (int) ((TcpMatch) layer4Match)
184 .getTcpDestinationPort().getValue());
187 assertNotNull("Tcp wasn't found", tcpFound);
190 boolean udpFound = false;
191 assertEquals("Wrong protocol", UDP, match.getIpMatch().getIpProtocol().byteValue());
192 layer4Match = match.getLayer4Match();
193 if (layer4Match instanceof UdpMatch) {
194 assertEquals("Udp source port is incorrect.", (int) 0xcdef, (int) ((UdpMatch) layer4Match)
195 .getUdpSourcePort().getValue());
196 assertEquals("Udp dest port is incorrect.", (int) 0xfedc, (int) ((UdpMatch) layer4Match)
197 .getUdpDestinationPort().getValue());
200 assertNotNull("Udp wasn't found", udpFound);
206 private void checkOdFlow(NodeFlow odNodeFlow) {
207 assertEquals("Cookie is incorrect.", 9223372036854775807L, odNodeFlow.getCookie().longValue());
208 assertEquals("Hard timeout is incorrect.", 32765, odNodeFlow.getHardTimeout().shortValue());
209 assertEquals("Iddle timeout is incorrect.", 32766, odNodeFlow.getIdleTimeout().shortValue());
210 assertEquals("Priority is incorrect.", 32767, odNodeFlow.getPriority().shortValue());
212 checkOdActions(ToSalConversionsUtils.getAction(odNodeFlow));
215 private void checkOdActions(
216 List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.list.Action> actions) {
217 checkOdAction(actions, FloodAction.class, false);
218 checkOdAction(actions, FloodAllAction.class, false);
219 checkOdAction(actions, HwPathAction.class, false);
220 checkOdAction(actions, LoopbackAction.class, false);
221 checkOdAction(actions, PopVlanAction.class, false);
222 checkOdAction(actions, PushVlanAction.class, true);
223 checkOdAction(actions, SetDlDstAction.class, true);
224 checkOdAction(actions, SetDlSrcAction.class, true);
225 checkOdAction(actions, SetDlTypeAction.class, true);
226 checkOdAction(actions, SetNwTosAction.class, true);
227 checkOdAction(actions, SetNwDstAction.class, true);
228 checkOdAction(actions, SetNwSrcAction.class, true);
229 checkOdAction(actions, SetNextHopAction.class, true);
230 checkOdAction(actions, SetTpDstAction.class, true);
231 checkOdAction(actions, SetTpSrcAction.class, true);
232 checkOdAction(actions, SetVlanCfiAction.class, true);
233 checkOdAction(actions, SetVlanIdAction.class, true);
234 checkOdAction(actions, SetVlanPcpAction.class, true);
235 checkOdAction(actions, SwPathAction.class, false);
238 private void checkOdAction(
239 List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.list.Action> actions, Class<?> cl,
241 int numOfFoundActions = 0;
242 for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.list.Action action : actions) {
243 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.Action innerAction = action
245 if (cl.isInstance(innerAction)) {
247 if (innerAction instanceof PushVlanAction) {
248 assertEquals("Wrong value of cfi in PushVlanAction.", (Integer) 1, ((PushVlanAction) innerAction)
249 .getCfi().getValue());
250 assertEquals("Wrong value of pcp in PushVlanAction.", (Integer) 7,
251 ((PushVlanAction) innerAction).getPcp());
252 assertEquals("Wrong value of tag in PushVlanAction.", (Integer) 0x8100,
253 ((PushVlanAction) innerAction).getTag());
254 assertEquals("Wrong value of vlad ID in PushVlanAction.", (Integer) 4095,
255 ((PushVlanAction) innerAction).getVlanId().getValue());
256 } else if (innerAction instanceof SetDlDstAction) {
257 assertEquals("Wrong MAC destination address in SetDlDstAction.", "ff:ee:dd:cc:bb:aa",
258 ((SetDlDstAction) innerAction).getAddress().getValue());
259 } else if (innerAction instanceof SetDlSrcAction) {
260 assertEquals("Wrong MAC source address in SetDlDstAction.", "ff:ee:dd:cc:bb:aa",
261 ((SetDlSrcAction) innerAction).getAddress().getValue());
262 } else if (innerAction instanceof SetDlTypeAction) {
263 assertEquals("Wrong data link type in SetDlTypeAction.", (long) 513,
264 (long) ((SetDlTypeAction) innerAction).getDlType().getValue());
265 } else if (innerAction instanceof SetNextHopAction) {
266 Address address = ((SetNextHopAction) innerAction).getAddress();
267 boolean ipv4AddressFound = false;
268 if (address instanceof Ipv4) {
269 ipv4AddressFound = true;
270 assertEquals("Wrong IP address type in SetNextHopAction.", "192.168.100.100", ((Ipv4) address)
271 .getIpv4Address().getValue());
273 assertTrue("Ipv4 address wasn't found.", ipv4AddressFound);
274 } else if (innerAction instanceof SetNwTosAction) {
275 assertEquals("Wrong TOS in SetNwTosAction.", (Integer) 63, ((SetNwTosAction) innerAction).getTos());
276 } else if (innerAction instanceof SetNwDstAction) {
277 Address address = ((SetNwDstAction) innerAction).getAddress();
278 boolean ipv4AddressFound = false;
279 if (address instanceof Ipv4) {
280 ipv4AddressFound = true;
281 assertEquals("Wrong IP address type in SetNwDstAction.", "192.168.100.101", ((Ipv4) address)
282 .getIpv4Address().getValue());
284 assertTrue("Ipv4 address wasn't found.", ipv4AddressFound);
285 } else if (innerAction instanceof SetNwSrcAction) {
286 Address address = ((SetNwSrcAction) innerAction).getAddress();
287 boolean ipv4AddressFound = false;
288 if (address instanceof Ipv4) {
289 ipv4AddressFound = true;
290 assertEquals("Wrong IP address type in SetNwSrcAction.", "192.168.100.102", ((Ipv4) address)
291 .getIpv4Address().getValue());
293 assertTrue("Ipv4 address wasn't found.", ipv4AddressFound);
294 } else if (innerAction instanceof SetTpDstAction) {
295 assertEquals("Port number is incorrect in SetTpDstAction.", (Integer) 65534,
296 ((SetTpDstAction) innerAction).getPort().getValue());
297 } else if (innerAction instanceof SetTpSrcAction) {
298 assertEquals("Port number is incorrect in SetTpSrcAction.", (Integer) 65535,
299 ((SetTpSrcAction) innerAction).getPort().getValue());
300 } else if (innerAction instanceof SetVlanCfiAction) {
301 assertEquals("Vlan cfi number is incorrect in SetVlanCfiAction.", (Integer) 1,
302 ((SetVlanCfiAction) innerAction).getVlanCfi().getValue());
303 } else if (innerAction instanceof SetVlanIdAction) {
304 assertEquals("Vlan id number is incorrect in SetVlanIdAction.", (Integer) 4095,
305 ((SetVlanIdAction) innerAction).getVlanId().getValue());
306 } else if (innerAction instanceof SetVlanPcpAction) {
307 assertEquals("Vlan pcp number is incorrect in SetVlanPcpAction.", new Short((short) 7),
308 ((SetVlanPcpAction) innerAction).getVlanPcp().getValue());
312 assertEquals("Incorrrect number of action " + cl.getName() + ".", 1, numOfFoundActions);
316 private Flow prepareSalFlowCommon() {
317 Flow salFlow = new Flow();
318 salFlow.setId(9223372036854775807L);
319 salFlow.setHardTimeout((short) 32765);
320 salFlow.setIdleTimeout((short) 32766);
321 salFlow.setPriority((short) 32767);
322 salFlow.setActions(prepareSalActions());
323 salFlow.setMatch(new Match());
328 private Flow prepareSalMatch(Flow salFlow, MtchType mt) {
329 Match salMatch = new Match();
332 salMatch.setField(MatchType.DL_TYPE, ETHERNET_ARP);
333 salMatch.setField(MatchType.NW_SRC, InetAddresses.forString("192.168.100.100"));
334 salMatch.setField(MatchType.NW_DST, InetAddresses.forString("192.168.100.101"));
335 salMatch.setField(MatchType.DL_SRC, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
336 salMatch.setField(MatchType.DL_DST, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
339 salMatch.setField(MatchType.DL_TYPE, (short) 0xffff);
340 salMatch.setField(MatchType.NW_SRC, InetAddresses.forString("192.168.100.102"));
341 salMatch.setField(MatchType.NW_DST, InetAddresses.forString("192.168.100.103"));
344 salMatch.setField(MatchType.DL_TYPE, (short) 0xffff);
345 salMatch.setField(MatchType.NW_SRC, InetAddresses.forString("2001:0db8:85a3:0000:0000:8a2e:0370:7335"));
346 salMatch.setField(MatchType.NW_DST, InetAddresses.forString("2001:0db8:85a3:0000:0000:8a2e:0370:7336"));
349 salMatch.setField(MatchType.DL_SRC, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
350 salMatch.setField(MatchType.DL_DST, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
351 salMatch.setField(MatchType.DL_VLAN, (short) 0xfff);
352 salMatch.setField(MatchType.DL_VLAN_PR, (byte) 0x7);
353 salMatch.setField(MatchType.NW_TOS, (byte) 0x3f);
356 salMatch.setField(MatchType.NW_PROTO, SCTP);
357 salMatch.setField(MatchType.TP_SRC, (short) 0xffff);
358 salMatch.setField(MatchType.TP_DST, (short) 0xfffe);
361 salMatch.setField(MatchType.NW_PROTO, TCP);
362 salMatch.setField(MatchType.TP_SRC, (short) 0xabcd);
363 salMatch.setField(MatchType.TP_DST, (short) 0xdcba);
366 salMatch.setField(MatchType.NW_PROTO, UDP);
367 salMatch.setField(MatchType.TP_SRC, (short) 0xcdef);
368 salMatch.setField(MatchType.TP_DST, (short) 0xfedc);
375 salFlow.setMatch(salMatch);
379 private List<Action> prepareSalActions() {
380 List<Action> salActions = new ArrayList<>();
381 salActions.add(new Flood());
382 salActions.add(new FloodAll());
383 salActions.add(new HwPath());
384 salActions.add(new Loopback());
385 // salActions.add(new Output //TODO: mapping is missing
386 salActions.add(new PopVlan());
387 salActions.add(new PushVlan(0x8100, 7, 1, 4095));
388 salActions.add(new SetDlDst(new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa}));
389 salActions.add(new SetDlSrc(new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa}));
390 salActions.add(new SetDlType(513));
391 salActions.add(new SetNextHop(InetAddresses.forString("192.168.100.100")));
392 salActions.add(new SetNwDst(InetAddresses.forString("192.168.100.101")));
393 salActions.add(new SetNwSrc(InetAddresses.forString("192.168.100.102")));
394 salActions.add(new SetNwTos(63));
395 salActions.add(new SetTpDst(65534));
396 salActions.add(new SetTpSrc(65535));
397 salActions.add(new SetVlanCfi(1));
398 salActions.add(new SetVlanId(4095));
399 salActions.add(new SetVlanPcp(7));
400 salActions.add(new SwPath());