Merge "Added Inventory Reader for SwitchManager"
[controller.git] / opendaylight / md-sal / compatibility / sal-compatibility / src / test / java / org / opendaylight / controller / sal / compatibility / test / TestToSalConversionsUtils.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.assertTrue;
12
13 import java.math.BigInteger;
14 import java.net.Inet4Address;
15 import java.net.Inet6Address;
16 import java.net.InetAddress;
17 import java.util.ArrayList;
18 import java.util.Collections;
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.ToSalConversionsUtils;
24 import org.opendaylight.controller.sal.flowprogrammer.Flow;
25 import org.opendaylight.controller.sal.match.MatchType;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.*;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowAddedBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.NodeFlow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.VlanCfi;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.action.*;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.Address;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv4Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.address.address.Ipv6Builder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.list.Action;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.action.list.ActionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Match;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpSourceHardwareAddressBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.arp.match.fields.ArpTargetHardwareAddressBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.*;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.*;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.ArpMatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.SctpMatchBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.TcpMatchBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
57 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActions;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsBuilder;
59
60 import com.google.common.net.InetAddresses;
61
62 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.ETHERNET_ARP;
63 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.SCTP;
64 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.TCP;
65 import static org.opendaylight.controller.sal.compatibility.ProtocolConstants.UDP;
66
67 public class TestToSalConversionsUtils {
68     // prefix:
69     // od|Od = Open Daylight
70     private enum MtchType {
71         other, ipv4, ipv6, arp, sctp, tcp, udp
72     }
73
74     @Test
75     public void testToSalConversion() {
76         FlowAddedBuilder odNodeFlowBuilder = new FlowAddedBuilder();
77         odNodeFlowBuilder = prepareOdFlowCommon();
78
79         Flow salFlow = ToSalConversionsUtils.toFlow(prepareOdFlow(odNodeFlowBuilder, MtchType.other));
80         checkSalMatch(salFlow.getMatch(), MtchType.other);
81
82         salFlow = ToSalConversionsUtils.toFlow(prepareOdFlow(odNodeFlowBuilder, MtchType.ipv4));
83         checkSalMatch(salFlow.getMatch(), MtchType.ipv4);
84
85         salFlow = ToSalConversionsUtils.toFlow(prepareOdFlow(odNodeFlowBuilder, MtchType.ipv6));
86         checkSalMatch(salFlow.getMatch(), MtchType.ipv6);
87
88         salFlow = ToSalConversionsUtils.toFlow(prepareOdFlow(odNodeFlowBuilder, MtchType.arp));
89         checkSalMatch(salFlow.getMatch(), MtchType.arp);
90
91         salFlow = ToSalConversionsUtils.toFlow(prepareOdFlow(odNodeFlowBuilder, MtchType.sctp));
92         checkSalMatch(salFlow.getMatch(), MtchType.sctp);
93
94         salFlow = ToSalConversionsUtils.toFlow(prepareOdFlow(odNodeFlowBuilder, MtchType.tcp));
95         checkSalMatch(salFlow.getMatch(), MtchType.tcp);
96
97         salFlow = ToSalConversionsUtils.toFlow(prepareOdFlow(odNodeFlowBuilder, MtchType.udp));
98         checkSalMatch(salFlow.getMatch(), MtchType.udp);
99
100         checkSalFlow(salFlow);
101     }
102
103     private void checkSalMatch(org.opendaylight.controller.sal.match.Match match, MtchType mt) {
104         switch (mt) {
105         case other:
106             /*assertNotNull("DL_DST isn't equal.", "3C:A9:F4:00:E0:C8", 
107                     new String((byte[]) match.getField(MatchType.DL_DST).getValue()));
108             assertEquals("DL_SRC isn't equal.", "24:77:03:7C:C5:F1",
109                     new String((byte[]) match.getField(MatchType.DL_SRC).getValue()));
110             */
111             assertEquals("DL_TYPE isn't equal.", (short) 0xffff, (short) match.getField(MatchType.DL_TYPE).getValue());
112             assertEquals("NW_TOS isn't equal.", (byte) 0x33, (byte) match.getField(MatchType.NW_TOS).getValue());
113             assertEquals("NW_PROTO isn't equal.", (byte) 0x3f, (byte) match.getField(MatchType.NW_PROTO).getValue());
114             assertEquals("DL_VLAN isn't equal.", (short) 0xfff, (short) match.getField(MatchType.DL_VLAN).getValue());
115             assertEquals("DL_VLAN_PR isn't equal.", (byte) 0x7, (byte) match.getField(MatchType.DL_VLAN_PR).getValue());
116             break;
117         case arp:
118             /*
119             assertEquals("DL_SRC isn't equal.", "22:44:66:88:AA:CC",
120                     new String((byte[]) match.getField(MatchType.DL_SRC).getValue()));
121             assertEquals("DL_DST isn't equal.", "11:33:55:77:BB:DD",
122                     new String((byte[]) match.getField(MatchType.DL_DST).getValue()));
123             */
124             assertEquals("NW_SRC isn't equal.", "192.168.1.101",
125                     InetAddresses.toAddrString((InetAddress) match.getField(MatchType.NW_SRC).getValue()));
126             assertEquals("NW_DST isn't equal.", "192.168.1.102",
127                     InetAddresses.toAddrString((InetAddress) match.getField(MatchType.NW_DST).getValue()));
128             assertEquals("DL_TYPE isn't equal.", ETHERNET_ARP, match.getField(MatchType.DL_TYPE).getValue());
129             break;
130         case ipv4:
131             assertEquals("NW_SRC isn't equal.", "192.168.1.104",
132                     InetAddresses.toAddrString((InetAddress) match.getField(MatchType.NW_SRC).getValue()));
133             assertEquals("NW_DST isn't equal.", "192.168.1.105",
134                     InetAddresses.toAddrString((InetAddress) match.getField(MatchType.NW_DST).getValue()));
135             break;
136         case ipv6:
137             assertEquals("NW_SRC isn't equal.", "3001:db8:85a3::8a2e:370:7334",
138                     InetAddresses.toAddrString((InetAddress) match.getField(MatchType.NW_SRC).getValue()));
139             assertEquals("NW_DST isn't equal.", "3001:db8:85a3::8a2e:370:7335",
140                     InetAddresses.toAddrString((InetAddress) match.getField(MatchType.NW_DST).getValue()));
141             break;
142         case sctp:
143             assertEquals("TP_SRC isn't equal.", 31, (short) match.getField(MatchType.TP_SRC).getValue());
144             assertEquals("TP_DST isn't equal.", 32, (short) match.getField(MatchType.TP_DST).getValue());
145             assertEquals("NW_PROTO isn't equal.", SCTP, (byte) match.getField(MatchType.NW_PROTO).getValue());
146             break;
147         case tcp:
148             assertEquals("TP_SRC isn't equal.", 21, (short) match.getField(MatchType.TP_SRC).getValue());
149             assertEquals("TP_DST isn't equal.", 22, (short) match.getField(MatchType.TP_DST).getValue());
150             assertEquals("NW_PROTO isn't equal.", TCP, (byte) match.getField(MatchType.NW_PROTO).getValue());
151             break;
152         case udp:
153             assertEquals("TP_SRC isn't equal.", 11, (short) match.getField(MatchType.TP_SRC).getValue());
154             assertEquals("TP_DST isn't equal.", 12, (short) match.getField(MatchType.TP_DST).getValue());
155             assertEquals("NW_PROTO isn't equal.", UDP, (byte) match.getField(MatchType.NW_PROTO).getValue());
156             break;
157         default:
158             break;
159
160         }
161
162     }
163
164     private void checkSalFlow(Flow salFlow) {
165         assertTrue("Id value is incorrect.", salFlow.getId() == 9223372036854775807L);
166         assertTrue("Hard timeout is incorrect.", salFlow.getHardTimeout() == 32767);
167         assertTrue("Iddle timeout is incorrect.", salFlow.getIdleTimeout() == 32767);
168         assertTrue("Priority value is incorrect.", salFlow.getPriority() == 32767);
169
170         checkSalActions(salFlow.getActions());
171     }
172
173     private void checkSalActions(List<org.opendaylight.controller.sal.action.Action> actions) {
174         checkSalAction(actions, Flood.class, 1);
175         checkSalAction(actions, FloodAll.class, 1);
176         checkSalAction(actions, HwPath.class, 1);
177         checkSalAction(actions, Loopback.class, 1);
178         checkSalAction(actions, Output.class, 2, true);
179         checkSalAction(actions, PopVlan.class, 1);
180         checkSalAction(actions, PushVlan.class, 1, true);
181         checkSalAction(actions, SetDlDst.class, 1, true);
182         checkSalAction(actions, SetDlSrc.class, 1, true);
183         checkSalAction(actions, SetDlType.class, 1, true);
184         checkSalAction(actions, SetNextHop.class, 2, true);
185         checkSalAction(actions, SetNwDst.class, 2, true);
186         checkSalAction(actions, SetNwSrc.class, 2, true);
187         checkSalAction(actions, SetNwTos.class, 1, true);
188         checkSalAction(actions, SetTpDst.class, 1, true);
189         checkSalAction(actions, SetTpSrc.class, 1, true);
190         checkSalAction(actions, SetVlanCfi.class, 1, true);
191         checkSalAction(actions, SetVlanId.class, 1, true);
192         checkSalAction(actions, SetVlanPcp.class, 1, true);
193         checkSalAction(actions, SwPath.class, 1);
194     }
195
196     private void checkSalAction(List<org.opendaylight.controller.sal.action.Action> actions, Class<?> cls,
197             int numOfActions) {
198         checkSalAction(actions, cls, numOfActions, false);
199     }
200
201     private void checkSalAction(List<org.opendaylight.controller.sal.action.Action> actions, Class<?> cls,
202             int numOfActions, boolean additionalCheck) {
203         int numOfEqualClass = 0;
204         for (org.opendaylight.controller.sal.action.Action action : actions) {
205             if (action.getClass().equals(cls)) {
206                 if (additionalCheck) {
207                     additionalActionCheck(action);
208                 }
209                 numOfEqualClass++;
210             }
211         }
212         assertEquals("Incorrect number of actions of type " + cls.getName() + " was found.", numOfActions,
213                 numOfEqualClass);
214     }
215
216     // implement special checks
217     private void additionalActionCheck(org.opendaylight.controller.sal.action.Action action) {
218         if (action instanceof Output) {
219             // ((Output)action).getPort() //TODO finish check when mapping will
220             // be defined
221         } else if (action instanceof PushVlan) {
222             assertEquals("Wrong value for action PushVlan for tag.", 0x8100, ((PushVlan) action).getTag());
223             assertEquals("Wrong value for action PushVlan for pcp.", 7, ((PushVlan) action).getPcp());
224             assertEquals("Wrong value for action PushVlan for cfi.", 1, ((PushVlan) action).getCfi());
225             assertEquals("Wrong value for action PushVlan for vlanID.", 4095, ((PushVlan) action).getVlanId());
226         } else if (action instanceof SetDlDst) {
227             //assertEquals("Wrong value for action SetDlDst for MAC address.", "3C:A9:F4:00:E0:C8", new String(
228             //        ((SetDlDst) action).getDlAddress()));
229         } else if (action instanceof SetDlSrc) {
230             //assertEquals("Wrong value for action SetDlSrc for MAC address.", "24:77:03:7C:C5:F1", new String(
231             //      ((SetDlSrc) action).getDlAddress()));
232         } else if (action instanceof SetDlType) {
233             assertEquals("Wrong value for action SetDlType for.", 513l, ((SetDlType) action).getDlType());
234         } else if (action instanceof SetNextHop) {
235             InetAddress inetAddress = ((SetNextHop) action).getAddress();
236             checkIpAddresses(inetAddress, "192.168.100.100", "2001:db8:85a3::8a2e:370:7334");
237         } else if (action instanceof SetNwDst) {
238             InetAddress inetAddress = ((SetNwDst) action).getAddress();
239             checkIpAddresses(inetAddress, "192.168.100.101", "2001:db8:85a3::8a2e:370:7335");
240         } else if (action instanceof SetNwSrc) {
241             InetAddress inetAddress = ((SetNwSrc) action).getAddress();
242             checkIpAddresses(inetAddress, "192.168.100.102", "2001:db8:85a3::8a2e:370:7336");
243         } else if (action instanceof SetNwTos) {
244             assertEquals("Wrong value for action SetNwTos for tos.", 63, ((SetNwTos) action).getNwTos());
245         } else if (action instanceof SetTpDst) {
246             assertEquals("Wrong value for action SetTpDst for port.", 65535, ((SetTpDst) action).getPort());
247         } else if (action instanceof SetTpSrc) {
248             assertEquals("Wrong value for action SetTpSrc for port.", 65535, ((SetTpSrc) action).getPort());
249         } else if (action instanceof SetVlanCfi) {
250             assertEquals("Wrong value for action SetVlanCfi for port.", 1, ((SetVlanCfi) action).getCfi());
251         } else if (action instanceof SetVlanId) {
252             assertEquals("Wrong value for action SetVlanId for vlan ID.", 4095, ((SetVlanId) action).getVlanId());
253         } else if (action instanceof SetVlanPcp) {
254             assertEquals("Wrong value for action SetVlanPcp for vlan ID.", 7, ((SetVlanPcp) action).getPcp());
255         }
256     }
257
258     private void checkIpAddresses(InetAddress inetAddress, String ipv4, String ipv6) {
259         if (inetAddress instanceof Inet4Address) {
260             assertEquals("Wrong value for IP address.", ipv4, InetAddresses.toAddrString(inetAddress));
261         } else if (inetAddress instanceof Inet6Address) {
262             assertEquals("Wrong value for IP address.", ipv6, InetAddresses.toAddrString(inetAddress));
263         }
264     }
265
266     private FlowAddedBuilder prepareOdFlowCommon() {
267         FlowAddedBuilder odNodeFlowBuilder = new FlowAddedBuilder();
268
269         odNodeFlowBuilder.setCookie(new BigInteger("9223372036854775807"));
270         odNodeFlowBuilder.setHardTimeout(32767);
271         odNodeFlowBuilder.setIdleTimeout(32767);
272         odNodeFlowBuilder.setPriority(32767);
273         odNodeFlowBuilder.setInstructions(prepareOdActions());
274         return odNodeFlowBuilder;
275     }
276
277     private NodeFlow prepareOdFlow(FlowAddedBuilder odNodeFlowBuilder, MtchType mt) {
278         odNodeFlowBuilder.setMatch(prepOdMatch(mt));
279         return odNodeFlowBuilder.build();
280     }
281
282     private Instructions prepareOdActions() {
283         List<Action> odActions = new ArrayList<>();
284
285         ControllerActionBuilder controllerActionBuilder = new ControllerActionBuilder();
286         DropActionBuilder dropActionBuilder = new DropActionBuilder();
287         FloodActionBuilder floodActionBuilder = new FloodActionBuilder();
288         FloodAllActionBuilder floodAllActionBuilder = new FloodAllActionBuilder();
289         HwPathActionBuilder hwPathActionBuilder = new HwPathActionBuilder();
290         LoopbackActionBuilder loopbackActionBuilder = new LoopbackActionBuilder();
291         OutputActionBuilder outputActionBuilder = new OutputActionBuilder();
292         PopMplsActionBuilder popMplsActionBuilder = new PopMplsActionBuilder();
293         PopVlanActionBuilder popVlanActionBuilder = new PopVlanActionBuilder();
294         PushMplsActionBuilder pushMplsActionBuilder = new PushMplsActionBuilder();
295         PushPbbActionBuilder pushPbbActionBuilder = new PushPbbActionBuilder();
296         PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
297         SetDlDstActionBuilder setDlDstActionBuilder = new SetDlDstActionBuilder();
298         SetDlSrcActionBuilder setDlSrcActionBuilder = new SetDlSrcActionBuilder();
299         SetDlTypeActionBuilder setDlTypeActionBuilder = new SetDlTypeActionBuilder();
300         SetMplsTtlActionBuilder setMplsTtlActionBuilder = new SetMplsTtlActionBuilder();
301         SetNwTosActionBuilder setNwTosActionBuilder = new SetNwTosActionBuilder();
302         SetNwTtlActionBuilder setNwTtlActionBuilder = new SetNwTtlActionBuilder();
303         SetQueueActionBuilder setQueueActionBuilder = new SetQueueActionBuilder();
304         SetTpDstActionBuilder setTpDstActionBuilder = new SetTpDstActionBuilder();
305         SetTpSrcActionBuilder setTpSrcActionBuilder = new SetTpSrcActionBuilder();
306         SetVlanCfiActionBuilder setVlanCfiActionBuilder = new SetVlanCfiActionBuilder();
307         SetVlanIdActionBuilder setVlanIdActionBuilder = new SetVlanIdActionBuilder();
308         SetVlanPcpActionBuilder setVlanPcpActionBuilder = new SetVlanPcpActionBuilder();
309         SwPathActionBuilder swPathActionBuilder = new SwPathActionBuilder();
310
311         prepareActionOutput(outputActionBuilder);
312         prepareActionPushVlan(pushVlanActionBuilder);
313         prepareActionSetDlDst(setDlDstActionBuilder);
314         prepareActionSetDlSrc(setDlSrcActionBuilder);
315         prepareActionSetDlType(setDlTypeActionBuilder);
316         prepareActionNextHop(odActions);
317         prepareActionSetNwDst(odActions);
318         prepareActionSetNwSrc(odActions);
319         prepareActionSetNwTos(setNwTosActionBuilder);
320         prepareActionSetTpDst(setTpDstActionBuilder);
321         prepareActionSetTpSrc(setTpSrcActionBuilder);
322         prepareActionSetVlanCfi(setVlanCfiActionBuilder);
323         prepareActionSetVladId(setVlanIdActionBuilder);
324         prepareActionSetVlanPcp(setVlanPcpActionBuilder);
325
326         odActions.add(new ActionBuilder().setAction(controllerActionBuilder.build()).build());
327         odActions.add(new ActionBuilder().setAction(dropActionBuilder.build()).build());
328         odActions.add(new ActionBuilder().setAction(floodActionBuilder.build()).build());
329         odActions.add(new ActionBuilder().setAction(floodAllActionBuilder.build()).build());
330         odActions.add(new ActionBuilder().setAction(hwPathActionBuilder.build()).build());
331         odActions.add(new ActionBuilder().setAction(loopbackActionBuilder.build()).build());
332         odActions.add(new ActionBuilder().setAction(outputActionBuilder.build()).build());
333         odActions.add(new ActionBuilder().setAction(popMplsActionBuilder.build()).build());
334         odActions.add(new ActionBuilder().setAction(popVlanActionBuilder.build()).build());
335         odActions.add(new ActionBuilder().setAction(pushMplsActionBuilder.build()).build());
336         odActions.add(new ActionBuilder().setAction(pushPbbActionBuilder.build()).build());
337         odActions.add(new ActionBuilder().setAction(pushVlanActionBuilder.build()).build());
338         odActions.add(new ActionBuilder().setAction(setDlDstActionBuilder.build()).build());
339         odActions.add(new ActionBuilder().setAction(setDlSrcActionBuilder.build()).build());
340         odActions.add(new ActionBuilder().setAction(setDlTypeActionBuilder.build()).build());
341         odActions.add(new ActionBuilder().setAction(setMplsTtlActionBuilder.build()).build());
342         odActions.add(new ActionBuilder().setAction(setNwTosActionBuilder.build()).build());
343         odActions.add(new ActionBuilder().setAction(setNwTtlActionBuilder.build()).build());
344         odActions.add(new ActionBuilder().setAction(setQueueActionBuilder.build()).build());
345         odActions.add(new ActionBuilder().setAction(setTpDstActionBuilder.build()).build());
346         odActions.add(new ActionBuilder().setAction(setTpSrcActionBuilder.build()).build());
347         odActions.add(new ActionBuilder().setAction(setVlanCfiActionBuilder.build()).build());
348         odActions.add(new ActionBuilder().setAction(setVlanIdActionBuilder.build()).build());
349         odActions.add(new ActionBuilder().setAction(setVlanPcpActionBuilder.build()).build());
350         odActions.add(new ActionBuilder().setAction(swPathActionBuilder.build()).build());
351
352         
353         ApplyActions innerInst = new ApplyActionsBuilder().setAction(odActions).build();
354         Instruction applyActions = new InstructionBuilder().setInstruction(innerInst).build();
355         List<Instruction> instructions = Collections.singletonList(applyActions );
356         InstructionsBuilder instBuilder = new InstructionsBuilder();
357         
358         instBuilder.setInstruction(instructions);
359         
360         return instBuilder.build();
361     }
362
363     private void prepareActionSetVlanPcp(SetVlanPcpActionBuilder setVlanPcpActionBuilder) {
364         setVlanPcpActionBuilder.setVlanPcp(new VlanPcp((short) 7));
365     }
366
367     private void prepareActionSetVladId(SetVlanIdActionBuilder setVlanIdActionBuilder) {
368         setVlanIdActionBuilder.setVlanId(new VlanId(4095));
369     }
370
371     private void prepareActionSetVlanCfi(SetVlanCfiActionBuilder setVlanCfiActionBuilder) {
372         setVlanCfiActionBuilder.setVlanCfi(new VlanCfi(1));
373     }
374
375     private void prepareActionSetTpDst(SetTpDstActionBuilder setTpDstActionBuilder) {
376         setTpDstActionBuilder.setPort(new PortNumber(65535));
377     }
378
379     private void prepareActionSetTpSrc(SetTpSrcActionBuilder setTpSrcActionBuilder) {
380         setTpSrcActionBuilder.setPort(new PortNumber(65535));
381     }
382
383     private void prepareActionSetNwTos(SetNwTosActionBuilder setNwTosActionBuilder) {
384         setNwTosActionBuilder.setTos(63);
385     }
386
387     private void prepareActionSetNwSrc(List<Action> odActions) {
388         // test case for IPv4
389         SetNwSrcActionBuilder setNwSrcActionBuilderIpv4 = new SetNwSrcActionBuilder();
390         setNwSrcActionBuilderIpv4.setAddress(prapareIpv4Address("192.168.100.102"));
391         odActions.add(new ActionBuilder().setAction(setNwSrcActionBuilderIpv4.build()).build());
392
393         // test case for IPv6
394         SetNwSrcActionBuilder setNwSrcActionBuilderIpv6 = new SetNwSrcActionBuilder();
395         setNwSrcActionBuilderIpv6.setAddress(prapareIpv6Address("2001:0db8:85a3:0000:0000:8a2e:0370:7336"));
396         odActions.add(new ActionBuilder().setAction(setNwSrcActionBuilderIpv6.build()).build());
397     }
398
399     private void prepareActionSetNwDst(List<Action> odActions) {
400         // test case for IPv4
401         SetNwDstActionBuilder setNwDstActionBuilderIpv4 = new SetNwDstActionBuilder();
402         setNwDstActionBuilderIpv4.setAddress(prapareIpv4Address("192.168.100.101"));
403         odActions.add(new ActionBuilder().setAction(setNwDstActionBuilderIpv4.build()).build());
404
405         // test case for IPv6
406         SetNwDstActionBuilder setNwDstActionBuilderIpv6 = new SetNwDstActionBuilder();
407         setNwDstActionBuilderIpv6.setAddress(prapareIpv6Address("2001:0db8:85a3:0000:0000:8a2e:0370:7335"));
408         odActions.add(new ActionBuilder().setAction(setNwDstActionBuilderIpv6.build()).build());
409     }
410
411     private void prepareActionNextHop(List<Action> odActions) {
412         // test case for IPv4
413         SetNextHopActionBuilder setNextHopActionBuilderIpv4 = new SetNextHopActionBuilder();
414         setNextHopActionBuilderIpv4.setAddress(prapareIpv4Address("192.168.100.100"));
415         odActions.add(new ActionBuilder().setAction(setNextHopActionBuilderIpv4.build()).build());
416
417         // test case for IPv6
418         SetNextHopActionBuilder setNextHopActionBuilderIpv6 = new SetNextHopActionBuilder();
419         setNextHopActionBuilderIpv6.setAddress(prapareIpv6Address("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));
420         odActions.add(new ActionBuilder().setAction(setNextHopActionBuilderIpv6.build()).build());
421     }
422
423     private Address prapareIpv4Address(String ipv4Address) {
424         Ipv4Builder ipv4Builder = new Ipv4Builder();
425         ipv4Builder.setIpv4Address(new Ipv4Prefix(ipv4Address));
426         return ipv4Builder.build();
427     }
428
429     private Address prapareIpv6Address(String ipv6Address) {
430         Ipv6Builder ipv6Builder = new Ipv6Builder();
431         ipv6Builder.setIpv6Address(new Ipv6Prefix(ipv6Address));
432         return ipv6Builder.build();
433     }
434
435     private void prepareActionSetDlType(SetDlTypeActionBuilder setDlTypeActionBuilder) {
436         setDlTypeActionBuilder.setDlType(new EtherType(513l));
437     }
438
439     private void prepareActionSetDlSrc(SetDlSrcActionBuilder setDlSrcActionBuilder) {
440         setDlSrcActionBuilder.setAddress(new MacAddress("24:77:03:7C:C5:F1"));
441     }
442
443     private void prepareActionSetDlDst(SetDlDstActionBuilder setDlDstActionBuilder) {
444         setDlDstActionBuilder.setAddress(new MacAddress("3C:A9:F4:00:E0:C8"));
445     }
446
447     private void prepareActionPushVlan(PushVlanActionBuilder pushVlanActionBuilder) {
448         pushVlanActionBuilder.setPcp(7); // 3 bits
449         pushVlanActionBuilder.setCfi(new VlanCfi(1)); // 1 bit
450         pushVlanActionBuilder.setVlanId(new VlanId(4095));
451         pushVlanActionBuilder.setTag(0x8100); // 12 bit
452     }
453
454     private void prepareActionOutput(OutputActionBuilder outputActionBuilder) {
455         List<Uri> uris = new ArrayList<>();
456         uris.add(new Uri("uri1"));
457         uris.add(new Uri("uri2"));
458         outputActionBuilder.setOutputNodeConnector(uris);
459     }
460
461     private Match prepOdMatch(MtchType mt) {
462         MatchBuilder odMatchBuilder = new MatchBuilder();
463         switch (mt) {
464         case other:
465             odMatchBuilder.setEthernetMatch(prepEthernetMatch());
466             odMatchBuilder.setIpMatch(prepIpMatch());
467             odMatchBuilder.setVlanMatch(prepVlanMatch());
468             break;
469         case ipv4:
470             odMatchBuilder.setLayer3Match(prepLayer3MatchIpv4());
471             break;
472         case ipv6:
473             odMatchBuilder.setLayer3Match(prepLayer3MatchIpv6());
474             break;
475         case arp:
476             odMatchBuilder.setLayer3Match(prepLayer3MatchArp());
477             break;
478         case sctp:
479             odMatchBuilder.setLayer4Match(prepLayer4MatchSctp());
480             break;
481         case tcp:
482             odMatchBuilder.setLayer4Match(prepLayer4MatchTcp());
483             break;
484         case udp:
485             odMatchBuilder.setLayer4Match(prepLayer4MatchUdp());
486             break;
487         }
488         return odMatchBuilder.build();
489     }
490
491     private Layer4Match prepLayer4MatchUdp() {
492         UdpMatchBuilder udpMatchBuilder = new UdpMatchBuilder();
493
494         udpMatchBuilder.setUdpSourcePort(new PortNumber(11));
495         udpMatchBuilder.setUdpDestinationPort(new PortNumber(12));
496
497         return udpMatchBuilder.build();
498     }
499
500     private Layer4Match prepLayer4MatchTcp() {
501         TcpMatchBuilder tcpMatchBuilder = new TcpMatchBuilder();
502
503         tcpMatchBuilder.setTcpSourcePort(new PortNumber(21));
504         tcpMatchBuilder.setTcpDestinationPort(new PortNumber(22));
505
506         return tcpMatchBuilder.build();
507     }
508
509     private Layer4Match prepLayer4MatchSctp() {
510         SctpMatchBuilder sctpMatchBuilder = new SctpMatchBuilder();
511
512         sctpMatchBuilder.setSctpSourcePort(new PortNumber(31));
513         sctpMatchBuilder.setSctpDestinationPort(new PortNumber(32));
514
515         return sctpMatchBuilder.build();
516     }
517
518     private Layer3Match prepLayer3MatchIpv4() {
519         Ipv4MatchBuilder ipv4MatchBuilder = new Ipv4MatchBuilder();
520         ipv4MatchBuilder.setIpv4Source(new Ipv4Prefix("192.168.1.104"));
521         ipv4MatchBuilder.setIpv4Destination(new Ipv4Prefix("192.168.1.105"));
522         return ipv4MatchBuilder.build();
523     }
524
525     private Layer3Match prepLayer3MatchIpv6() {
526         Ipv6MatchBuilder ipv6MatchBuilder = new Ipv6MatchBuilder();
527         ipv6MatchBuilder.setIpv6Source(new Ipv6Prefix("3001:0db8:85a3:0000:0000:8a2e:0370:7334"));
528         ipv6MatchBuilder.setIpv6Destination(new Ipv6Prefix("3001:0db8:85a3:0000:0000:8a2e:0370:7335"));
529         return ipv6MatchBuilder.build();
530     }
531
532     private Layer3Match prepLayer3MatchArp() {
533         ArpMatchBuilder arpMatchBuilder = new ArpMatchBuilder();
534         arpMatchBuilder.setArpSourceTransportAddress(new Ipv4Prefix("192.168.1.101"));
535         arpMatchBuilder.setArpTargetTransportAddress(new Ipv4Prefix("192.168.1.102"));
536
537         ArpSourceHardwareAddressBuilder arpSourAddressBuild = new ArpSourceHardwareAddressBuilder();
538         arpSourAddressBuild.setAddress(new MacAddress("22:44:66:88:AA:CC"));
539         arpMatchBuilder.setArpSourceHardwareAddress(arpSourAddressBuild.build());
540
541         ArpTargetHardwareAddressBuilder arpTarAddressBuild = new ArpTargetHardwareAddressBuilder();
542         arpTarAddressBuild.setAddress(new MacAddress("11:33:55:77:BB:DD"));
543         arpMatchBuilder.setArpTargetHardwareAddress(arpTarAddressBuild.build());
544         return arpMatchBuilder.build();
545     }
546
547     private VlanMatch prepVlanMatch() {
548         VlanMatchBuilder vlanMatchBuilder = new VlanMatchBuilder();
549
550         VlanIdBuilder vlanIdBuilder = new VlanIdBuilder().setVlanId(new VlanId(0xfff));
551         vlanMatchBuilder.setVlanId(vlanIdBuilder.build());
552         vlanMatchBuilder.setVlanPcp(new VlanPcp((short) 0x7));
553
554         return vlanMatchBuilder.build();
555
556     }
557
558     private IpMatch prepIpMatch() {
559         IpMatchBuilder ipMatchBuilder = new IpMatchBuilder();
560         ipMatchBuilder.setIpDscp(new Dscp((short) 0x33));
561         ipMatchBuilder.setIpProtocol((short) 0x3f);
562         return ipMatchBuilder.build();
563     }
564
565     private EthernetMatch prepEthernetMatch() {
566         EthernetMatchBuilder odEthernetMatchBuilder = new EthernetMatchBuilder();
567         odEthernetMatchBuilder.setEthernetDestination(prepEthDest());
568         odEthernetMatchBuilder.setEthernetSource(prepEthSour());
569         odEthernetMatchBuilder.setEthernetType(prepEthType());
570         return odEthernetMatchBuilder.build();
571     }
572
573     private EthernetType prepEthType() {
574         EthernetTypeBuilder ethTypeBuild = new EthernetTypeBuilder();
575         ethTypeBuild.setType(new EtherType(0xffffl));
576         return ethTypeBuild.build();
577     }
578
579     private EthernetSource prepEthSour() {
580         EthernetSourceBuilder ethSourBuild = new EthernetSourceBuilder();
581         ethSourBuild.setAddress(new MacAddress("24:77:03:7C:C5:F1"));
582         return ethSourBuild.build();
583     }
584
585     private EthernetDestination prepEthDest() {
586         EthernetDestinationBuilder ethDestBuild = new EthernetDestinationBuilder();
587         ethDestBuild.setAddress(new MacAddress("3C:A9:F4:00:E0:C8"));
588         return ethDestBuild.build();
589     }
590 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.