63c5664a0c4b2de63e5b6e90279eb59f37e050ef
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / test / java / org / opendaylight / controller / sal / compatibility / test / TestFromSalConversionsUtils.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3 *
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
7 */
8 package org.opendaylight.controller.sal.compatibility.test;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.ETHERNET_ARP;
14 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.CRUDP;
15 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.TCP;
16 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.UDP;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.action.*;
23 import org.opendaylight.controller.sal.compatibility.MDFlowMapping;
24 import org.opendaylight.controller.sal.compatibility.ToSalConversionsUtils;
25 import org.opendaylight.controller.sal.flowprogrammer.Flow;
26 import org.opendaylight.controller.sal.match.Match;
27 import org.opendaylight.controller.sal.match.MatchType;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.*;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanAction;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.Address;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer3Match;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.Layer4Match;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatch;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4Match;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatch;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatch;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatch;
41
42 import com.google.common.net.InetAddresses;
43
44 public class TestFromSalConversionsUtils {
45     private enum MtchType {
46         other, ipv4, ipv6, arp, sctp, tcp, udp
47     }
48
49     @Test
50     public void testFromSalConversion() {
51
52         Flow salFlow = prepareSalFlowCommon();
53         NodeFlow odNodeFlow = MDFlowMapping.flowAdded(salFlow);
54
55         checkOdFlow(odNodeFlow);
56
57         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.other));
58         checkOdMatch(odNodeFlow.getMatch(), MtchType.other);
59
60         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.arp));
61         checkOdMatch(odNodeFlow.getMatch(), MtchType.arp);
62
63         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.ipv4));
64         checkOdMatch(odNodeFlow.getMatch(), MtchType.ipv4);
65
66         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.ipv6));
67         checkOdMatch(odNodeFlow.getMatch(), MtchType.ipv6);
68
69         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.sctp));
70         checkOdMatch(odNodeFlow.getMatch(), MtchType.sctp);
71
72         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.tcp));
73         checkOdMatch(odNodeFlow.getMatch(), MtchType.tcp);
74
75         odNodeFlow = MDFlowMapping.flowAdded(prepareSalMatch(salFlow, MtchType.udp));
76         checkOdMatch(odNodeFlow.getMatch(), MtchType.udp);
77     }
78
79     private void checkOdMatch(org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match match,
80             MtchType mt) {
81         switch (mt) {
82         case arp:
83             assertEquals("Ether type is incorrect.", ETHERNET_ARP, (long) match.getEthernetMatch().getEthernetType()
84                     .getType().getValue());
85             Layer3Match layer3Match = match.getLayer3Match();
86             boolean arpFound = false;
87             if (layer3Match instanceof ArpMatch) {
88                 assertEquals("Source IP address is wrong.", "192.168.100.100", ((ArpMatch) layer3Match)
89                         .getArpSourceTransportAddress().getValue());
90                 assertEquals("Destination IP address is wrong.", "192.168.100.101", ((ArpMatch) layer3Match)
91                         .getArpTargetTransportAddress().getValue());
92                 assertEquals("Source MAC address is wrong.", "ff:ee:dd:cc:bb:aa", ((ArpMatch) layer3Match)
93                         .getArpSourceHardwareAddress().getAddress().getValue());
94                 assertEquals("Destination MAC address is wrong.", "ff:ee:dd:cc:bb:aa", ((ArpMatch) layer3Match)
95                         .getArpTargetHardwareAddress().getAddress().getValue());
96                 arpFound = true;
97             }
98             assertNotNull("Arp wasn't found", arpFound);
99             break;
100         case ipv4:
101             assertEquals("Ether type is incorrect.", 0xffff, (long) match.getEthernetMatch().getEthernetType()
102                     .getType().getValue());
103             boolean ipv4Found = false;
104             layer3Match = match.getLayer3Match();
105             if (layer3Match instanceof Ipv4Match) {
106                 assertEquals("Source IP address is wrong.", "192.168.100.102", ((Ipv4Match) layer3Match)
107                         .getIpv4Source().getValue());
108                 assertEquals("Destination IP address is wrong.", "192.168.100.103", ((Ipv4Match) layer3Match)
109                         .getIpv4Destination().getValue());
110             }
111             assertNotNull("Ipv4 wasn't found", ipv4Found);
112             break;
113         case ipv6:
114             assertEquals("Ether type is incorrect.", 0xffff, (long) match.getEthernetMatch().getEthernetType()
115                     .getType().getValue());
116             boolean ipv6Found = false;
117             layer3Match = match.getLayer3Match();
118             if (layer3Match instanceof Ipv6Match) {
119                 assertEquals("Source IP address is wrong.", "2001:db8:85a3::8a2e:370:7335", ((Ipv6Match) layer3Match)
120                         .getIpv6Source().getValue());
121                 assertEquals("Destination IP address is wrong.", "2001:db8:85a3::8a2e:370:7336",
122                         ((Ipv6Match) layer3Match).getIpv6Destination().getValue());
123             }
124             assertNotNull("Ipv6 wasn't found", ipv6Found);
125             break;
126         case other:
127             assertEquals("Source MAC address is wrong.", "ff:ee:dd:cc:bb:aa", match.getEthernetMatch()
128                     .getEthernetSource().getAddress().getValue());
129             assertEquals("Destinatio MAC address is wrong.", "ff:ee:dd:cc:bb:aa", match.getEthernetMatch()
130                     .getEthernetDestination().getAddress().getValue());
131             assertEquals("Vlan ID is wrong.", (Integer) 0xfff, match.getVlanMatch().getVlanId().getVlanId().getValue());
132             assertEquals("Vlan ID priority is wrong.", (short) 0x7, (short) match.getVlanMatch().getVlanPcp()
133                     .getValue());
134             assertEquals("DCSP is wrong.", (short) 0x3f, (short) match.getIpMatch().getIpDscp().getValue());
135             break;
136         case sctp:
137             boolean sctpFound = false;
138             assertEquals("Wrong protocol", CRUDP, match.getIpMatch().getIpProtocol().byteValue());
139             Layer4Match layer4Match = match.getLayer4Match();
140             if (layer4Match instanceof SctpMatch) {
141                 assertEquals("Sctp source port is incorrect.", 0xffff, (int) ((SctpMatch) layer4Match)
142                         .getSctpSourcePort().getValue());
143                 assertEquals("Sctp dest port is incorrect.", (int) 0xfffe, (int) ((SctpMatch) layer4Match)
144                         .getSctpDestinationPort().getValue());
145                 sctpFound = true;
146             }
147             assertNotNull("Sctp wasn't found", sctpFound);
148             break;
149         case tcp:
150             boolean tcpFound = false;
151             assertEquals("Wrong protocol", TCP, match.getIpMatch().getIpProtocol().byteValue());
152             layer4Match = match.getLayer4Match();
153             if (layer4Match instanceof TcpMatch) {
154                 assertEquals("Tcp source port is incorrect.", (int) 0xabcd, (int) ((TcpMatch) layer4Match)
155                         .getTcpSourcePort().getValue());
156                 assertEquals("Tcp dest port is incorrect.", (int) 0xdcba, (int) ((TcpMatch) layer4Match)
157                         .getTcpDestinationPort().getValue());
158                 sctpFound = true;
159             }
160             assertNotNull("Tcp wasn't found", tcpFound);
161             break;
162         case udp:
163             boolean udpFound = false;
164             assertEquals("Wrong protocol", UDP, match.getIpMatch().getIpProtocol().byteValue());
165             layer4Match = match.getLayer4Match();
166             if (layer4Match instanceof UdpMatch) {
167                 assertEquals("Udp source port is incorrect.", (int) 0xcdef, (int) ((UdpMatch) layer4Match)
168                         .getUdpSourcePort().getValue());
169                 assertEquals("Udp dest port is incorrect.", (int) 0xfedc, (int) ((UdpMatch) layer4Match)
170                         .getUdpDestinationPort().getValue());
171                 sctpFound = true;
172             }
173             assertNotNull("Udp wasn't found", udpFound);
174             break;
175         }
176
177     }
178
179     private void checkOdFlow(NodeFlow odNodeFlow) {
180         assertEquals("Cookie is incorrect.", 9223372036854775807L, odNodeFlow.getCookie().getValue().longValue());
181         assertEquals("Hard timeout is incorrect.", 32765, odNodeFlow.getHardTimeout().shortValue());
182         assertEquals("Iddle timeout is incorrect.", 32766, odNodeFlow.getIdleTimeout().shortValue());
183         assertEquals("Priority is incorrect.", 32767, odNodeFlow.getPriority().shortValue());
184
185         checkOdActions(ToSalConversionsUtils.getAction(odNodeFlow));
186     }
187
188     private void checkOdActions(
189             List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actions) {
190         checkOdAction(actions, FloodActionCase.class, false);
191         checkOdAction(actions, FloodAllActionCase.class, false);
192         checkOdAction(actions, HwPathActionCase.class, false);
193         checkOdAction(actions, LoopbackActionCase.class, false);
194         checkOdAction(actions, PopVlanActionCase.class, false);
195         checkOdAction(actions, PushVlanActionCase.class, true);
196         checkOdAction(actions, SetDlDstActionCase.class, true);
197         checkOdAction(actions, SetDlSrcActionCase.class, true);
198         checkOdAction(actions, SetDlTypeActionCase.class, true);
199         checkOdAction(actions, SetNwTosActionCase.class, true);
200         checkOdAction(actions, SetNwDstActionCase.class, true);
201         checkOdAction(actions, SetNwSrcActionCase.class, true);
202         checkOdAction(actions, SetNextHopActionCase.class, true);
203         checkOdAction(actions, SetTpDstActionCase.class, true);
204         checkOdAction(actions, SetTpSrcActionCase.class, true);
205         checkOdAction(actions, SetVlanCfiActionCase.class, true);
206         checkOdAction(actions, SetVlanIdActionCase.class, true);
207         checkOdAction(actions, SetVlanPcpActionCase.class, true);
208         checkOdAction(actions, SwPathActionCase.class, false);
209     }
210
211     private void checkOdAction(
212             List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actions, Class<?> cl,
213             boolean b) {
214         int numOfFoundActions = 0;
215         for (org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action action : actions) {
216                 org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.Action innerAction = action
217                     .getAction();
218             if (cl.isInstance(innerAction)) {
219                 numOfFoundActions++;
220                 if (innerAction instanceof PushVlanActionCase) {
221                     assertEquals("Wrong value of cfi in PushVlanAction.", (Integer) 1, ((PushVlanActionCase) innerAction).getPushVlanAction()
222                             .getCfi().getValue());
223                     assertEquals("Wrong value of pcp in PushVlanAction.", (Integer) 7,
224                             ((PushVlanActionCase) innerAction).getPushVlanAction().getPcp());
225                     assertEquals("Wrong value of tag in PushVlanAction.", (Integer) 0x8100,
226                             ((PushVlanActionCase) innerAction).getPushVlanAction().getTag());
227                     assertEquals("Wrong value of vlad ID in PushVlanAction.", (Integer) 4095,
228                             ((PushVlanActionCase) innerAction).getPushVlanAction().getVlanId().getValue());
229                 } else if (innerAction instanceof SetDlDstActionCase) {
230                     assertEquals("Wrong MAC destination address in SetDlDstAction.", "ff:ee:dd:cc:bb:aa", 
231                             ((SetDlDstActionCase) innerAction).getSetDlDstAction().getAddress().getValue());
232                 } else if (innerAction instanceof SetDlSrcActionCase) {
233                     assertEquals("Wrong MAC source address in SetDlDstAction.", "ff:ee:dd:cc:bb:aa", 
234                             ((SetDlSrcActionCase) innerAction).getSetDlSrcAction().getAddress().getValue());
235                 } else if (innerAction instanceof SetDlTypeActionCase) {
236                     assertEquals("Wrong data link type in SetDlTypeAction.", (long) 513,
237                             (long) ((SetDlTypeActionCase) innerAction).getSetDlTypeAction().getDlType().getValue());
238                 } else if (innerAction instanceof SetNextHopActionCase) {
239                     Address address = ((SetNextHopActionCase) innerAction).getSetNextHopAction().getAddress();
240                     boolean ipv4AddressFound = false;
241                     if (address instanceof Ipv4) {
242                         ipv4AddressFound = true;
243                         assertEquals("Wrong IP address type in SetNextHopAction.", "192.168.100.100", ((Ipv4) address)
244                                 .getIpv4Address().getValue());
245                     }
246                     assertTrue("Ipv4 address wasn't found.", ipv4AddressFound);
247                 } else if (innerAction instanceof SetNwTosActionCase) {
248                     assertEquals("Wrong TOS in SetNwTosAction.", (Integer) 63, ((SetNwTosActionCase) innerAction).getSetNwTosAction().getTos());
249                 } else if (innerAction instanceof SetNwDstActionCase) {
250                     Address address = ((SetNwDstActionCase) innerAction).getSetNwDstAction().getAddress();
251                     boolean ipv4AddressFound = false;
252                     if (address instanceof Ipv4) {
253                         ipv4AddressFound = true;
254                         assertEquals("Wrong IP address type in SetNwDstAction.", "192.168.100.101", ((Ipv4) address)
255                                 .getIpv4Address().getValue());
256                     }
257                     assertTrue("Ipv4 address wasn't found.", ipv4AddressFound);
258                 } else if (innerAction instanceof SetNwSrcActionCase) {
259                     Address address = ((SetNwSrcActionCase) innerAction).getSetNwSrcAction().getAddress();
260                     boolean ipv4AddressFound = false;
261                     if (address instanceof Ipv4) {
262                         ipv4AddressFound = true;
263                         assertEquals("Wrong IP address type in SetNwSrcAction.", "192.168.100.102", ((Ipv4) address)
264                                 .getIpv4Address().getValue());
265                     }
266                     assertTrue("Ipv4 address wasn't found.", ipv4AddressFound);
267                 } else if (innerAction instanceof SetTpDstActionCase) {
268                     assertEquals("Port number is incorrect in SetTpDstAction.", (Integer) 65534,
269                             ((SetTpDstActionCase) innerAction).getSetTpDstAction().getPort().getValue());
270                 } else if (innerAction instanceof SetTpSrcActionCase) {
271                     assertEquals("Port number is incorrect in SetTpSrcAction.", (Integer) 65535,
272                             ((SetTpSrcActionCase) innerAction).getSetTpSrcAction().getPort().getValue());
273                 } else if (innerAction instanceof SetVlanCfiActionCase) {
274                     assertEquals("Vlan cfi number is incorrect in SetVlanCfiAction.", (Integer) 1,
275                             ((SetVlanCfiActionCase) innerAction).getSetVlanCfiAction().getVlanCfi().getValue());
276                 } else if (innerAction instanceof SetVlanIdActionCase) {
277                     assertEquals("Vlan id number is incorrect in SetVlanIdAction.", (Integer) 4095,
278                             ((SetVlanIdActionCase) innerAction).getSetVlanIdAction().getVlanId().getValue());
279                 } else if (innerAction instanceof SetVlanPcpActionCase) {
280                     assertEquals("Vlan pcp number is incorrect in SetVlanPcpAction.", new Short((short) 7),
281                             ((SetVlanPcpActionCase) innerAction).getSetVlanPcpAction().getVlanPcp().getValue());
282                 }
283             }
284         }
285         assertEquals("Incorrrect number of action " + cl.getName() + ".", 1, numOfFoundActions);
286
287     }
288
289     private Flow prepareSalFlowCommon() {
290         Flow salFlow = new Flow();
291         salFlow.setId(9223372036854775807L);
292         salFlow.setHardTimeout((short) 32765);
293         salFlow.setIdleTimeout((short) 32766);
294         salFlow.setPriority((short) 32767);
295         salFlow.setActions(prepareSalActions());
296         salFlow.setMatch(new Match());
297
298         return salFlow;
299     }
300
301     private Flow prepareSalMatch(Flow salFlow, MtchType mt) {
302         Match salMatch = new Match();
303         switch (mt) {
304         case arp:
305             salMatch.setField(MatchType.DL_TYPE, ETHERNET_ARP);
306             salMatch.setField(MatchType.NW_SRC, InetAddresses.forString("192.168.100.100"));
307             salMatch.setField(MatchType.NW_DST, InetAddresses.forString("192.168.100.101"));
308             salMatch.setField(MatchType.DL_SRC, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
309             salMatch.setField(MatchType.DL_DST, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
310             break;
311         case ipv4:
312             salMatch.setField(MatchType.DL_TYPE, (short) 0xffff);
313             salMatch.setField(MatchType.NW_SRC, InetAddresses.forString("192.168.100.102"));
314             salMatch.setField(MatchType.NW_DST, InetAddresses.forString("192.168.100.103"));
315             break;
316         case ipv6:
317             salMatch.setField(MatchType.DL_TYPE, (short) 0xffff);
318             salMatch.setField(MatchType.NW_SRC, InetAddresses.forString("2001:0db8:85a3:0000:0000:8a2e:0370:7335"));
319             salMatch.setField(MatchType.NW_DST, InetAddresses.forString("2001:0db8:85a3:0000:0000:8a2e:0370:7336"));
320             break;
321         case other:
322             salMatch.setField(MatchType.DL_SRC, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
323             salMatch.setField(MatchType.DL_DST, new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa});
324             salMatch.setField(MatchType.DL_VLAN, (short) 0xfff);
325             salMatch.setField(MatchType.DL_VLAN_PR, (byte) 0x7);
326             salMatch.setField(MatchType.NW_TOS, (byte) 0x3f);
327             break;
328         case sctp:
329             salMatch.setField(MatchType.NW_PROTO, CRUDP);
330             salMatch.setField(MatchType.TP_SRC, (short) 0xffff);
331             salMatch.setField(MatchType.TP_DST, (short) 0xfffe);
332             break;
333         case tcp:
334             salMatch.setField(MatchType.NW_PROTO, TCP);
335             salMatch.setField(MatchType.TP_SRC, (short) 0xabcd);
336             salMatch.setField(MatchType.TP_DST, (short) 0xdcba);
337             break;
338         case udp:
339             salMatch.setField(MatchType.NW_PROTO, UDP);
340             salMatch.setField(MatchType.TP_SRC, (short) 0xcdef);
341             salMatch.setField(MatchType.TP_DST, (short) 0xfedc);
342             break;
343         default:
344             break;
345
346         }
347
348         salFlow.setMatch(salMatch);
349         return salFlow;
350     }
351
352     private List<Action> prepareSalActions() {
353         List<Action> salActions = new ArrayList<>();
354         salActions.add(new Flood());
355         salActions.add(new FloodAll());
356         salActions.add(new HwPath());
357         salActions.add(new Loopback());
358         // salActions.add(new Output //TODO: mapping is missing
359         salActions.add(new PopVlan());
360         salActions.add(new PushVlan(0x8100, 7, 1, 4095));
361         salActions.add(new SetDlDst(new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa}));
362         salActions.add(new SetDlSrc(new byte[]{(byte )0xff,(byte )0xee,(byte )0xdd,(byte )0xcc,(byte )0xbb,(byte )0xaa}));
363         salActions.add(new SetDlType(513));
364         salActions.add(new SetNextHop(InetAddresses.forString("192.168.100.100")));
365         salActions.add(new SetNwDst(InetAddresses.forString("192.168.100.101")));
366         salActions.add(new SetNwSrc(InetAddresses.forString("192.168.100.102")));
367         salActions.add(new SetNwTos(63));
368         salActions.add(new SetTpDst(65534));
369         salActions.add(new SetTpSrc(65535));
370         salActions.add(new SetVlanCfi(1));
371         salActions.add(new SetVlanId(4095));
372         salActions.add(new SetVlanPcp(7));
373         salActions.add(new SwPath());
374
375         return salActions;
376     }
377
378 }