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