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

©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.