To SAL conversion test.
[controller.git] / opendaylight / md-sal / sal-compability / src / test / java / org / opendaylight / controller / sal / compability / 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.List;
12
13 import org.junit.Test;
14 import org.opendaylight.controller.sal.action.*;
15 import org.opendaylight.controller.sal.flowprogrammer.Flow;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv6Prefix;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
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.rev130819.VlanCfi;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.action.action.*;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.Address;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv4Builder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.address.address.Ipv6Builder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Action;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.ActionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.Match;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev130819.flow.MatchBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanPcp;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev130819.match.EthernetMatchBuilder;
36
37 import com.google.common.net.InetAddresses;
38
39 public class TestToSalConversionsUtils {
40     // prefix:
41     // od|Od = Open Daylight
42
43     @Test
44     public void testToSalConversion() {
45         Flow salFlow = ToSalConversionsUtils.flowFrom(prepareOdFlow());
46         checkSalFlow(salFlow);
47     }
48
49     private void checkSalFlow(Flow salFlow) {
50         assertTrue("Id value is incorrect.", salFlow.getId() == 9223372036854775807L);
51         assertTrue("Hard timeout is incorrect.", salFlow.getHardTimeout() == 32767);
52         assertTrue("Iddle timeout is incorrect.", salFlow.getIdleTimeout() == 32767);
53         assertTrue("Priority value is incorrect.", salFlow.getPriority() == 32767);
54
55         checkSalActions(salFlow.getActions());
56     }
57
58     private void checkSalActions(List<org.opendaylight.controller.sal.action.Action> actions) {
59         checkSalAction(actions, Flood.class, 1);
60         checkSalAction(actions, FloodAll.class, 1);
61         checkSalAction(actions, HwPath.class, 1);
62         checkSalAction(actions, Loopback.class, 1);
63         checkSalAction(actions, Output.class, 2, true);
64         checkSalAction(actions, PopVlan.class, 1);
65         checkSalAction(actions, PushVlan.class, 1, true);
66         checkSalAction(actions, SetDlDst.class, 1, true);
67         checkSalAction(actions, SetDlSrc.class, 1, true);
68         checkSalAction(actions, SetDlType.class, 1, true);
69         checkSalAction(actions, SetNextHop.class, 2, true);
70         checkSalAction(actions, SetNwDst.class, 2, true);
71         checkSalAction(actions, SetNwSrc.class, 2, true);
72         checkSalAction(actions, SetNwTos.class, 1, true);
73         checkSalAction(actions, SetTpDst.class, 1, true);
74         checkSalAction(actions, SetTpSrc.class, 1, true);
75         checkSalAction(actions, SetVlanCfi.class, 1, true);
76         checkSalAction(actions, SetVlanId.class, 1, true);
77         checkSalAction(actions, SetVlanPcp.class, 1, true);
78         checkSalAction(actions, SwPath.class, 1);
79     }
80
81     private void checkSalAction(List<org.opendaylight.controller.sal.action.Action> actions, Class<?> cls,
82             int numOfActions) {
83         checkSalAction(actions, cls, numOfActions, false);
84     }
85
86     private void checkSalAction(List<org.opendaylight.controller.sal.action.Action> actions, Class<?> cls,
87             int numOfActions, boolean additionalCheck) {
88         int numOfEqualClass = 0;
89         for (org.opendaylight.controller.sal.action.Action action : actions) {
90             if (action.getClass().equals(cls)) {
91                 if (additionalCheck) {
92                     additionalActionCheck(action);
93                 }
94                 numOfEqualClass++;
95             }
96         }
97         assertEquals("Incorrect number of actions of type " + cls.getName() + " was found.", numOfActions,
98                 numOfEqualClass);
99     }
100
101     // implement special checks
102     private void additionalActionCheck(org.opendaylight.controller.sal.action.Action action) {
103         if (action instanceof Output) {
104             // ((Output)action).getPort() //TODO finish check when mapping will
105             // be defined
106         } else if (action instanceof PushVlan) {
107             assertEquals("Wrong value for action PushVlan for tag.", 0x8100, ((PushVlan) action).getTag());
108             assertEquals("Wrong value for action PushVlan for pcp.", 7, ((PushVlan) action).getPcp());
109             assertEquals("Wrong value for action PushVlan for cfi.", 1, ((PushVlan) action).getCfi());
110             assertEquals("Wrong value for action PushVlan for vlanID.", 4095, ((PushVlan) action).getVlanId());
111         } else if (action instanceof SetDlDst) {
112             assertEquals("Wrong value for action SetDlDst for MAC address.", "3C:A9:F4:00:E0:C8", new String(
113                     ((SetDlDst) action).getDlAddress()));
114         } else if (action instanceof SetDlSrc) {
115             assertEquals("Wrong value for action SetDlSrc for MAC address.", "24:77:03:7C:C5:F1", new String(
116                     ((SetDlSrc) action).getDlAddress()));
117         } else if (action instanceof SetDlType) {
118             assertEquals("Wrong value for action SetDlType for.", 513l, ((SetDlType) action).getDlType());
119         } else if (action instanceof SetNextHop) {
120             InetAddress inetAddress = ((SetNextHop) action).getAddress();
121             checkIpAddresses(inetAddress, "192.168.100.100", "2001:db8:85a3::8a2e:370:7334");
122         } else if (action instanceof SetNwDst) {
123             InetAddress inetAddress = ((SetNwDst) action).getAddress();
124             checkIpAddresses(inetAddress, "192.168.100.101", "2001:db8:85a3::8a2e:370:7335");
125         } else if (action instanceof SetNwSrc) {
126             InetAddress inetAddress = ((SetNwSrc) action).getAddress();
127             checkIpAddresses(inetAddress, "192.168.100.102", "2001:db8:85a3::8a2e:370:7336");
128         } else if (action instanceof SetNwTos) {
129             assertEquals("Wrong value for action SetNwTos for tos.", 63, ((SetNwTos) action).getNwTos());
130         } else if (action instanceof SetTpDst) {
131             assertEquals("Wrong value for action SetTpDst for port.", 65535, ((SetTpDst) action).getPort());
132         } else if (action instanceof SetTpSrc) {
133             assertEquals("Wrong value for action SetTpSrc for port.", 65535, ((SetTpSrc) action).getPort());
134         } else if (action instanceof SetVlanCfi) {
135             assertEquals("Wrong value for action SetVlanCfi for port.", 1, ((SetVlanCfi) action).getCfi());
136         } else if (action instanceof SetVlanId) {
137             assertEquals("Wrong value for action SetVlanId for vlan ID.", 4095, ((SetVlanId) action).getVlanId());
138         } else if (action instanceof SetVlanPcp) {
139             assertEquals("Wrong value for action SetVlanPcp for vlan ID.", 7, ((SetVlanPcp) action).getPcp());
140         }
141     }
142
143     private void checkIpAddresses(InetAddress inetAddress, String ipv4, String ipv6) {
144         if (inetAddress instanceof Inet4Address) {
145             assertEquals("Wrong value for IP address.", ipv4, InetAddresses.toAddrString(inetAddress));
146         } else if (inetAddress instanceof Inet6Address) {
147             assertEquals("Wrong value for IP address.", ipv6, InetAddresses.toAddrString(inetAddress));
148         }
149     }
150
151     private NodeFlow prepareOdFlow() {
152         FlowAddedBuilder odNodeFlowBuilder = new FlowAddedBuilder();
153         odNodeFlowBuilder.setCookie(new BigInteger("9223372036854775807"));
154         odNodeFlowBuilder.setHardTimeout(32767);
155         odNodeFlowBuilder.setIdleTimeout(32767);
156         odNodeFlowBuilder.setPriority(32767);
157         odNodeFlowBuilder.setAction(prepareOdActions());
158         odNodeFlowBuilder.setMatch(prepareOdMatch());
159
160         return odNodeFlowBuilder.build();
161     }
162
163     private List<Action> prepareOdActions() {
164         List<Action> odActions = new ArrayList<>();
165
166         ControllerActionBuilder controllerActionBuilder = new ControllerActionBuilder();
167         DropActionBuilder dropActionBuilder = new DropActionBuilder();
168         FloodActionBuilder floodActionBuilder = new FloodActionBuilder();
169         FloodAllActionBuilder floodAllActionBuilder = new FloodAllActionBuilder();
170         HwPathActionBuilder hwPathActionBuilder = new HwPathActionBuilder();
171         LoopbackActionBuilder loopbackActionBuilder = new LoopbackActionBuilder();
172         OutputActionBuilder outputActionBuilder = new OutputActionBuilder();
173         PopMplsActionBuilder popMplsActionBuilder = new PopMplsActionBuilder();
174         PopVlanActionBuilder popVlanActionBuilder = new PopVlanActionBuilder();
175         PushMplsActionBuilder pushMplsActionBuilder = new PushMplsActionBuilder();
176         PushPbbActionBuilder pushPbbActionBuilder = new PushPbbActionBuilder();
177         PushVlanActionBuilder pushVlanActionBuilder = new PushVlanActionBuilder();
178         SetDlDstActionBuilder setDlDstActionBuilder = new SetDlDstActionBuilder();
179         SetDlSrcActionBuilder setDlSrcActionBuilder = new SetDlSrcActionBuilder();
180         SetDlTypeActionBuilder setDlTypeActionBuilder = new SetDlTypeActionBuilder();
181         SetMplsTtlActionBuilder setMplsTtlActionBuilder = new SetMplsTtlActionBuilder();
182         SetNwTosActionBuilder setNwTosActionBuilder = new SetNwTosActionBuilder();
183         SetNwTtlActionBuilder setNwTtlActionBuilder = new SetNwTtlActionBuilder();
184         SetQueueActionBuilder setQueueActionBuilder = new SetQueueActionBuilder();
185         SetTpDstActionBuilder setTpDstActionBuilder = new SetTpDstActionBuilder();
186         SetTpSrcActionBuilder setTpSrcActionBuilder = new SetTpSrcActionBuilder();
187         SetVlanCfiActionBuilder setVlanCfiActionBuilder = new SetVlanCfiActionBuilder();
188         SetVlanIdActionBuilder setVlanIdActionBuilder = new SetVlanIdActionBuilder();
189         SetVlanPcpActionBuilder setVlanPcpActionBuilder = new SetVlanPcpActionBuilder();
190         SwPathActionBuilder swPathActionBuilder = new SwPathActionBuilder();
191
192         prepareActionOutput(outputActionBuilder);
193         prepareActionPushVlan(pushVlanActionBuilder);
194         prepareActionSetDlDst(setDlDstActionBuilder);
195         prepareActionSetDlSrc(setDlSrcActionBuilder);
196         prepareActionSetDlType(setDlTypeActionBuilder);
197         prepareActionNextHop(odActions);
198         prepareActionSetNwDst(odActions);
199         prepareActionSetNwSrc(odActions);
200         prepareActionSetNwTos(setNwTosActionBuilder);
201         prepareActionSetTpDst(setTpDstActionBuilder);
202         prepareActionSetTpSrc(setTpSrcActionBuilder);
203         prepareActionSetVlanCfi(setVlanCfiActionBuilder);
204         prepareActionSetVladId(setVlanIdActionBuilder);
205         prepareActionSetVlanPcp(setVlanPcpActionBuilder);
206
207         odActions.add(new ActionBuilder().setAction(controllerActionBuilder.build()).build());
208         odActions.add(new ActionBuilder().setAction(dropActionBuilder.build()).build());
209         odActions.add(new ActionBuilder().setAction(floodActionBuilder.build()).build());
210         odActions.add(new ActionBuilder().setAction(floodAllActionBuilder.build()).build());
211         odActions.add(new ActionBuilder().setAction(hwPathActionBuilder.build()).build());
212         odActions.add(new ActionBuilder().setAction(loopbackActionBuilder.build()).build());
213         odActions.add(new ActionBuilder().setAction(outputActionBuilder.build()).build());
214         odActions.add(new ActionBuilder().setAction(popMplsActionBuilder.build()).build());
215         odActions.add(new ActionBuilder().setAction(popVlanActionBuilder.build()).build());
216         odActions.add(new ActionBuilder().setAction(pushMplsActionBuilder.build()).build());
217         odActions.add(new ActionBuilder().setAction(pushPbbActionBuilder.build()).build());
218         odActions.add(new ActionBuilder().setAction(pushVlanActionBuilder.build()).build());
219         odActions.add(new ActionBuilder().setAction(setDlDstActionBuilder.build()).build());
220         odActions.add(new ActionBuilder().setAction(setDlSrcActionBuilder.build()).build());
221         odActions.add(new ActionBuilder().setAction(setDlTypeActionBuilder.build()).build());
222         odActions.add(new ActionBuilder().setAction(setMplsTtlActionBuilder.build()).build());
223         odActions.add(new ActionBuilder().setAction(setNwTosActionBuilder.build()).build());
224         odActions.add(new ActionBuilder().setAction(setNwTtlActionBuilder.build()).build());
225         odActions.add(new ActionBuilder().setAction(setQueueActionBuilder.build()).build());
226         odActions.add(new ActionBuilder().setAction(setTpDstActionBuilder.build()).build());
227         odActions.add(new ActionBuilder().setAction(setTpSrcActionBuilder.build()).build());
228         odActions.add(new ActionBuilder().setAction(setVlanCfiActionBuilder.build()).build());
229         odActions.add(new ActionBuilder().setAction(setVlanIdActionBuilder.build()).build());
230         odActions.add(new ActionBuilder().setAction(setVlanPcpActionBuilder.build()).build());
231         odActions.add(new ActionBuilder().setAction(swPathActionBuilder.build()).build());
232
233         return odActions;
234     }
235
236     private void prepareActionSetVlanPcp(SetVlanPcpActionBuilder setVlanPcpActionBuilder) {
237         setVlanPcpActionBuilder.setVlanPcp(new VlanPcp((short) 7));
238     }
239
240     private void prepareActionSetVladId(SetVlanIdActionBuilder setVlanIdActionBuilder) {
241         setVlanIdActionBuilder.setVlanId(new VlanId(4095));
242     }
243
244     private void prepareActionSetVlanCfi(SetVlanCfiActionBuilder setVlanCfiActionBuilder) {
245         setVlanCfiActionBuilder.setVlanCfi(new VlanCfi(1));
246     }
247
248     private void prepareActionSetTpDst(SetTpDstActionBuilder setTpDstActionBuilder) {
249         setTpDstActionBuilder.setPort(new PortNumber(65535));
250     }
251
252     private void prepareActionSetTpSrc(SetTpSrcActionBuilder setTpSrcActionBuilder) {
253         setTpSrcActionBuilder.setPort(new PortNumber(65535));
254     }
255
256     private void prepareActionSetNwTos(SetNwTosActionBuilder setNwTosActionBuilder) {
257         setNwTosActionBuilder.setTos(63);
258     }
259
260     private void prepareActionSetNwSrc(List<Action> odActions) {
261         // test case for IPv4
262         SetNwSrcActionBuilder setNwSrcActionBuilderIpv4 = new SetNwSrcActionBuilder();
263         setNwSrcActionBuilderIpv4.setAddress(prapareIpv4Address("192.168.100.102"));
264         odActions.add(new ActionBuilder().setAction(setNwSrcActionBuilderIpv4.build()).build());
265
266         // test case for IPv6
267         SetNwSrcActionBuilder setNwSrcActionBuilderIpv6 = new SetNwSrcActionBuilder();
268         setNwSrcActionBuilderIpv6.setAddress(prapareIpv6Address("2001:0db8:85a3:0000:0000:8a2e:0370:7336"));
269         odActions.add(new ActionBuilder().setAction(setNwSrcActionBuilderIpv6.build()).build());
270     }
271
272     private void prepareActionSetNwDst(List<Action> odActions) {
273         // test case for IPv4
274         SetNwDstActionBuilder setNwDstActionBuilderIpv4 = new SetNwDstActionBuilder();
275         setNwDstActionBuilderIpv4.setAddress(prapareIpv4Address("192.168.100.101"));
276         odActions.add(new ActionBuilder().setAction(setNwDstActionBuilderIpv4.build()).build());
277
278         // test case for IPv6
279         SetNwDstActionBuilder setNwDstActionBuilderIpv6 = new SetNwDstActionBuilder();
280         setNwDstActionBuilderIpv6.setAddress(prapareIpv6Address("2001:0db8:85a3:0000:0000:8a2e:0370:7335"));
281         odActions.add(new ActionBuilder().setAction(setNwDstActionBuilderIpv6.build()).build());
282     }
283
284     private void prepareActionNextHop(List<Action> odActions) {
285         // test case for IPv4
286         SetNextHopActionBuilder setNextHopActionBuilderIpv4 = new SetNextHopActionBuilder();
287         setNextHopActionBuilderIpv4.setAddress(prapareIpv4Address("192.168.100.100"));
288         odActions.add(new ActionBuilder().setAction(setNextHopActionBuilderIpv4.build()).build());
289
290         // test case for IPv6
291         SetNextHopActionBuilder setNextHopActionBuilderIpv6 = new SetNextHopActionBuilder();
292         setNextHopActionBuilderIpv6.setAddress(prapareIpv6Address("2001:0db8:85a3:0000:0000:8a2e:0370:7334"));
293         odActions.add(new ActionBuilder().setAction(setNextHopActionBuilderIpv6.build()).build());
294     }
295
296     private Address prapareIpv4Address(String ipv4Address) {
297         Ipv4Builder ipv4Builder = new Ipv4Builder();
298         ipv4Builder.setIpv4Address(new Ipv4Prefix(ipv4Address));
299         return ipv4Builder.build();
300     }
301
302     private Address prapareIpv6Address(String ipv6Address) {
303         Ipv6Builder ipv6Builder = new Ipv6Builder();
304         ipv6Builder.setIpv6Address(new Ipv6Prefix(ipv6Address));
305         return ipv6Builder.build();
306     }
307
308     private void prepareActionSetDlType(SetDlTypeActionBuilder setDlTypeActionBuilder) {
309         setDlTypeActionBuilder.setDlType(new EtherType(513l));
310     }
311
312     private void prepareActionSetDlSrc(SetDlSrcActionBuilder setDlSrcActionBuilder) {
313         setDlSrcActionBuilder.setAddress(new MacAddress("24:77:03:7C:C5:F1"));
314     }
315
316     private void prepareActionSetDlDst(SetDlDstActionBuilder setDlDstActionBuilder) {
317         setDlDstActionBuilder.setAddress(new MacAddress("3C:A9:F4:00:E0:C8"));
318     }
319
320     private void prepareActionPushVlan(PushVlanActionBuilder pushVlanActionBuilder) {
321         pushVlanActionBuilder.setPcp(7); // 3 bits
322         pushVlanActionBuilder.setCfi(new VlanCfi(1)); // 1 bit
323         pushVlanActionBuilder.setVlanId(new VlanId(4095));
324         pushVlanActionBuilder.setTag(0x8100); // 12 bit
325     }
326
327     private void prepareActionOutput(OutputActionBuilder outputActionBuilder) {
328         List<Uri> uris = new ArrayList<>();
329         uris.add(new Uri("uri1"));
330         uris.add(new Uri("uri2"));
331         outputActionBuilder.setOutputNodeConnector(uris);
332     }
333
334     private Match prepareOdMatch() {
335         MatchBuilder odMatchBuilder = new MatchBuilder();
336         EthernetMatchBuilder odEthernetMatchBuilder = new EthernetMatchBuilder();
337         odMatchBuilder.setEthernetMatch(odEthernetMatchBuilder.build());
338
339         return odMatchBuilder.build();
340     }
341 }