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