3baa1bd0748c08785654935babd5049b5e080f83
[controller.git] / opendaylight / md-sal / sal-compability / src / test / java / org / opendaylight / controller / sal / compability / TestFromSalConversionsUtils.java
1 package org.opendaylight.controller.sal.compability;
2
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;
10
11 import java.util.ArrayList;
12 import java.util.List;
13
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.rev131026.action.action.*;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.Address;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv4;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
31
32 import com.google.common.net.InetAddresses;
33
34 public class TestFromSalConversionsUtils {
35     private enum MtchType {
36         other, ipv4, ipv6, arp, sctp, tcp, udp
37     }
38
39     @Test
40     public void testFromSalConversion() {
41
42         Flow salFlow = prepareSalFlowCommon();
43         NodeFlow odNodeFlow = MDFlowMapping.flowAdded(salFlow);
44
45         checkOdFlow(odNodeFlow);
46
47         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.other));
48         checkOdMatch(odNodeFlow.getMatch(), MtchType.other);
49
50         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.arp));
51         checkOdMatch(odNodeFlow.getMatch(), MtchType.arp);
52
53         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.ipv4));
54         checkOdMatch(odNodeFlow.getMatch(), MtchType.ipv4);
55
56         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.ipv6));
57         checkOdMatch(odNodeFlow.getMatch(), MtchType.ipv6);
58
59         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.sctp));
60         checkOdMatch(odNodeFlow.getMatch(), MtchType.sctp);
61
62         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.tcp));
63         checkOdMatch(odNodeFlow.getMatch(), MtchType.tcp);
64
65         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.udp));
66         checkOdMatch(odNodeFlow.getMatch(), MtchType.udp);
67     }
68
69     private void checkOdMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match match,
70             MtchType mt) {
71         switch (mt) {
72         case arp:
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.", "ff:ee:dd:cc:bb:aa", ((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());
86                 arpFound = true;
87             }
88             assertNotNull("Arp wasn't found", arpFound);
89             break;
90         case ipv4:
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());
100             }
101             assertNotNull("Ipv4 wasn't found", ipv4Found);
102             break;
103         case ipv6:
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());
113             }
114             assertNotNull("Ipv6 wasn't found", ipv6Found);
115             break;
116         case other:
117             assertEquals("Source MAC address is wrong.", "ff:ee:dd:cc:bb:aa", match.getEthernetMatch()
118                     .getEthernetSource().getAddress().getValue());
119             assertEquals("Destinatio MAC address is wrong.", "ff:ee:dd:cc:bb:aa", 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()
123                     .getValue());
124             assertEquals("DCSP is wrong.", (short) 0x3f, (short) match.getIpMatch().getIpDscp().getValue());
125             break;
126         case sctp:
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());
135                 sctpFound = true;
136             }
137             assertNotNull("Sctp wasn't found", sctpFound);
138             break;
139         case tcp:
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());
148                 sctpFound = true;
149             }
150             assertNotNull("Tcp wasn't found", tcpFound);
151             break;
152         case udp:
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());
161                 sctpFound = true;
162             }
163             assertNotNull("Udp wasn't found", udpFound);
164             break;
165         }
166
167     }
168
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());
174
175         checkOdActions(ToSalConversionsUtils.getAction(odNodeFlow));
176     }
177
178     private void checkOdActions(
179             List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.list.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);
199     }
200
201     private void checkOdAction(
202             List<org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.list.Action> actions, Class<?> cl,
203             boolean b) {
204         int numOfFoundActions = 0;
205         for (org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.list.Action action : actions) {
206             org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.Action innerAction = action
207                     .getAction();
208             if (cl.isInstance(innerAction)) {
209                 numOfFoundActions++;
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.", "ff:ee:dd:cc:bb:aa", 
221                             ((SetDlDstAction) innerAction).getAddress().getValue());
222                 } else if (innerAction instanceof SetDlSrcAction) {
223                     assertEquals("Wrong MAC source address in SetDlDstAction.", "ff:ee:dd:cc:bb:aa", 
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());
235                     }
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());
246                     }
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());
255                     }
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());
272                 }
273             }
274         }
275         assertEquals("Incorrrect number of action " + cl.getName() + ".", 1, numOfFoundActions);
276
277     }
278
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());
287
288         return salFlow;
289     }
290
291     private Flow prepareSalMatch(Flow salFlow, MtchType mt) {
292         Match salMatch = new Match();
293         switch (mt) {
294         case arp:
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, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
299             salMatch.setField(MatchType.DL_DST, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
300             break;
301         case ipv4:
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"));
305             break;
306         case ipv6:
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"));
310             break;
311         case other:
312             salMatch.setField(MatchType.DL_SRC, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
313             salMatch.setField(MatchType.DL_DST, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
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);
317             break;
318         case sctp:
319             salMatch.setField(MatchType.NW_PROTO, SCTP);
320             salMatch.setField(MatchType.TP_SRC, (short) 0xffff);
321             salMatch.setField(MatchType.TP_DST, (short) 0xfffe);
322             break;
323         case tcp:
324             salMatch.setField(MatchType.NW_PROTO, TCP);
325             salMatch.setField(MatchType.TP_SRC, (short) 0xabcd);
326             salMatch.setField(MatchType.TP_DST, (short) 0xdcba);
327             break;
328         case udp:
329             salMatch.setField(MatchType.NW_PROTO, UDP);
330             salMatch.setField(MatchType.TP_SRC, (short) 0xcdef);
331             salMatch.setField(MatchType.TP_DST, (short) 0xfedc);
332             break;
333         default:
334             break;
335
336         }
337
338         salFlow.setMatch(salMatch);
339         return salFlow;
340     }
341
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(new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa}));
352         salActions.add(new SetDlSrc(new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa}));
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());
364
365         return salActions;
366     }
367
368 }