BUG:5021 ELan datapath code-changes and cleanUp some code.
[vpnservice.git] / mdsalutil / mdsalutil-api / src / main / java / org / opendaylight / vpnservice / mdsalutil / ActionType.java
1 /*
2  * Copyright (c) 2015 Ericsson India Global Services Pvt Ltd. 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.vpnservice.mdsalutil;
9
10 import java.math.BigInteger;
11 import java.net.InetAddress;
12
13 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
14 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.GroupActionCaseBuilder;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopMplsActionCaseBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopPbbActionCaseBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PopVlanActionCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushMplsActionCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushPbbActionCaseBuilder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.PushVlanActionCaseBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.SetFieldCaseBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.group.action._case.GroupActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.mpls.action._case.PopMplsActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.pbb.action._case.PopPbbActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.pop.vlan.action._case.PopVlanActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.mpls.action._case.PushMplsActionBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.pbb.action._case.PushPbbActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.push.vlan.action._case.PushVlanActionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.set.field._case.SetFieldBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetDestinationBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.EthernetMatchBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.ProtocolMatchFieldsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.TunnelBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.protocol.match.fields.PbbBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
45 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ethernet.match.fields.EthernetSourceBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.IpMatchBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv4MatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._4.match.UdpMatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.DropActionCaseBuilder;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropAction;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.drop.action._case.DropActionBuilder;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.add.group.input.buckets.bucket.action.action.NxActionResubmitRpcAddGroupCaseBuilder;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.nx.action.resubmit.grouping.NxResubmitBuilder;
56
57 public enum ActionType {
58     group {
59         @Override
60         public Action buildAction(ActionInfo actionInfo) {
61             long groupId = Long.parseLong(actionInfo.getActionValues()[0]);
62
63             return new ActionBuilder().setAction(
64                             new GroupActionCaseBuilder().setGroupAction(
65                                     new GroupActionBuilder().setGroupId(groupId).build()).build())
66                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
67         }
68     },
69
70     output {
71         @Override
72         public Action buildAction(ActionInfo actionInfo) {
73             String[] actionValues = actionInfo.getActionValues();
74             String port = actionValues[0];
75             int maxLength = 0;
76
77             if (actionValues.length == 2) {
78                 maxLength = Integer.valueOf(actionValues[1]);
79             }
80
81             return new ActionBuilder().setAction(
82                     new OutputActionCaseBuilder().setOutputAction(
83                             new OutputActionBuilder().setMaxLength(Integer.valueOf(maxLength))
84                                             .setOutputNodeConnector(new Uri(port)).build()).build())
85                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
86         }
87     },
88
89     pop_mpls {
90         @Override
91         public Action buildAction(ActionInfo actionInfo) {
92             return new ActionBuilder().setAction(
93                     new PopMplsActionCaseBuilder().setPopMplsAction(
94                             new PopMplsActionBuilder().setEthernetType(
95                                     Integer.valueOf(NwConstants.ETHTYPE_IPV4)).build()).build())
96
97                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
98         }
99     },
100
101     pop_pbb {
102         @Override
103         public Action buildAction(ActionInfo actionInfo) {
104             return new ActionBuilder()
105                     .setAction(new PopPbbActionCaseBuilder().setPopPbbAction(new PopPbbActionBuilder().build()).build())
106                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
107         }
108     },
109
110     pop_vlan {
111         @Override
112         public Action buildAction(ActionInfo actionInfo) {
113             return new ActionBuilder().setAction(
114                     new PopVlanActionCaseBuilder().setPopVlanAction(new PopVlanActionBuilder().build()).build())
115                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
116         }
117     },
118
119     push_mpls {
120         @Override
121         public Action buildAction(ActionInfo actionInfo) {
122             return new ActionBuilder().setAction(new PushMplsActionCaseBuilder().setPushMplsAction(
123                                     new PushMplsActionBuilder().setEthernetType(
124                                             Integer.valueOf(NwConstants.ETHTYPE_MPLS_UC)).build()).build())
125                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
126         }
127     },
128
129     push_pbb {
130         @Override
131         public Action buildAction(ActionInfo actionInfo) {
132             return new ActionBuilder().setAction(
133                     new PushPbbActionCaseBuilder().setPushPbbAction(
134                                     new PushPbbActionBuilder()
135                                             .setEthernetType(Integer.valueOf(NwConstants.ETHTYPE_PBB)).build()).build())
136                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
137         }
138     },
139
140     push_vlan {
141         @Override
142         public Action buildAction(ActionInfo actionInfo) {
143             return new ActionBuilder().setAction(
144                     new PushVlanActionCaseBuilder().setPushVlanAction(
145                                     new PushVlanActionBuilder().setEthernetType(
146                                             Integer.valueOf(NwConstants.ETHTYPE_802_1Q)).build()).build())
147                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
148         }
149     },
150
151     set_field_mpls_label {
152         @Override
153         public Action buildAction(ActionInfo actionInfo) {
154             String[] actionValues = actionInfo.getActionValues();
155             long label = Long.valueOf(actionValues[0]);
156
157             return new ActionBuilder().setAction(
158                     new SetFieldCaseBuilder().setSetField(new SetFieldBuilder().setProtocolMatchFields(
159                                             new ProtocolMatchFieldsBuilder().setMplsLabel(label).build()).build())
160                                     .build()).setKey(new ActionKey(actionInfo.getActionKey())).build();
161         }
162     },
163
164     set_field_pbb_isid {
165         @Override
166         public Action buildAction(ActionInfo actionInfo) {
167             String[] actionValues = actionInfo.getActionValues();
168             long label = Long.valueOf(actionValues[0]);
169
170             return new ActionBuilder().setAction(
171                     new SetFieldCaseBuilder().setSetField(
172                             new SetFieldBuilder().setProtocolMatchFields(
173                                             new ProtocolMatchFieldsBuilder().setPbb(
174                                                     new PbbBuilder().setPbbIsid(label).build()).build()).build())
175                                     .build()).setKey(new ActionKey(actionInfo.getActionKey())).build();
176         }
177     },
178
179     set_field_vlan_vid {
180         @Override
181         public Action buildAction(ActionInfo actionInfo) {
182             String[] actionValues = actionInfo.getActionValues();
183             int vlanId = Integer.valueOf(actionValues[0]);
184
185             return new ActionBuilder().setAction(
186                     new SetFieldCaseBuilder().setSetField(
187                             new SetFieldBuilder().setVlanMatch(
188                                     new VlanMatchBuilder().setVlanId(
189                                                     new VlanIdBuilder().setVlanId(new VlanId(vlanId))
190                                                             .setVlanIdPresent(true).build()).build()).build()).build())
191                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
192         }
193     },
194
195     set_field_tunnel_id {
196         @Override
197         public Action buildAction(ActionInfo actionInfo) {
198             BigInteger [] actionValues = actionInfo.getBigActionValues();
199             if (actionValues.length == 2) {
200                 return new ActionBuilder().setAction(
201                     new SetFieldCaseBuilder().setSetField(
202                         new SetFieldBuilder()
203                             .setTunnel(new TunnelBuilder().setTunnelId(actionValues[0])
204                                            .setTunnelMask(actionValues[1]).build()).build())
205                         .build())
206                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
207             } else {
208                 return new ActionBuilder().setAction(
209                     new SetFieldCaseBuilder().setSetField(
210                         new SetFieldBuilder()
211                             .setTunnel(new TunnelBuilder().setTunnelId(actionValues[0])
212                                            .build()).build())
213                         .build())
214                     .setKey(new ActionKey(actionInfo.getActionKey())).build();
215             }
216
217         }
218
219     },
220
221     set_field_eth_dest {
222
223         @Override
224         public Action buildAction(ActionInfo actionInfo) {
225             String[] actionValues = actionInfo.getActionValues();
226             MacAddress mac = new MacAddress(actionValues[0]);
227
228             return new ActionBuilder().setAction(
229                     new SetFieldCaseBuilder().setSetField(
230                             new SetFieldBuilder().setEthernetMatch(
231                                     new EthernetMatchBuilder().setEthernetDestination(
232                                                     new EthernetDestinationBuilder().setAddress(mac).build()).build())
233                                             .build()).build()).setKey(new ActionKey(actionInfo.getActionKey())).build();
234
235         }
236
237     },
238
239     set_udp_protocol {
240
241         @Override
242         public Action buildAction(ActionInfo actionInfo) {
243             return new ActionBuilder().setAction(
244                     new SetFieldCaseBuilder().setSetField(
245                             new SetFieldBuilder().setIpMatch(
246                                     new IpMatchBuilder().setIpProtocol((short) 17).build()).
247                                     build()).build()).setKey(new ActionKey(actionInfo.getActionKey())).build();
248
249         }
250
251     },
252     punt_to_controller {
253         @Override
254         public Action buildAction(ActionInfo actionInfo) {
255             ActionBuilder ab = new ActionBuilder();
256             OutputActionBuilder output = new OutputActionBuilder();
257             output.setMaxLength(0xffff);
258             Uri value = new Uri(OutputPortValues.CONTROLLER.toString());
259             output.setOutputNodeConnector(value);
260             ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
261             ab.setKey(new ActionKey(actionInfo.getActionKey()));
262             return ab.build();
263         }
264
265     },
266     set_destination_port_field {
267
268         @Override
269         public Action buildAction(ActionInfo actionInfo) {
270             String[] actionValues = actionInfo.getActionValues();
271             Integer portNumber = new Integer(actionValues[0]);
272
273             return new ActionBuilder().setAction(
274                     new SetFieldCaseBuilder().setSetField(
275                             new SetFieldBuilder().setLayer4Match(
276                                     new UdpMatchBuilder().setUdpDestinationPort(
277                                             new PortNumber(portNumber)).build())
278                             .build()).build()).setKey(new ActionKey(actionInfo.getActionKey())).build();
279
280         }
281
282     },
283     set_source_port_field {
284
285         @Override
286         public Action buildAction(ActionInfo actionInfo) {
287             String[] actionValues = actionInfo.getActionValues();
288             Integer portNumber = new Integer(actionValues[0]);
289
290             return new ActionBuilder().setAction(
291                     new SetFieldCaseBuilder().setSetField(
292                             new SetFieldBuilder().setLayer4Match(
293                                     new UdpMatchBuilder().setUdpSourcePort(
294                                             new PortNumber(portNumber)).build())
295                             .build()).build()).setKey(new ActionKey(actionInfo.getActionKey())).build();
296
297         }
298
299     },
300     set_source_ip {
301
302         @Override
303         public Action buildAction(ActionInfo actionInfo) {
304             String[] actionValues = actionInfo.getActionValues();
305             InetAddress sourceIp = null;
306             try{
307                 sourceIp = InetAddress.getByName(actionValues[0]);
308             } catch (Exception e){
309                 e.printStackTrace();
310             }
311             return new ActionBuilder().setAction(
312                     new SetFieldCaseBuilder().setSetField(
313                             new SetFieldBuilder().setLayer3Match(
314                                     new Ipv4MatchBuilder().setIpv4Source(
315                                             new Ipv4Prefix(sourceIp.getHostAddress())).build()).
316                                             build()).build()).setKey(new ActionKey(actionInfo.getActionKey())).build();
317
318         }
319
320     },
321     set_destination_ip {
322
323         @Override
324         public Action buildAction(ActionInfo actionInfo) {
325             String[] actionValues = actionInfo.getActionValues();
326             InetAddress sourceIp = null;
327             try{
328                 sourceIp = InetAddress.getByName(actionValues[0]);
329             } catch (Exception e){
330                 e.printStackTrace();
331             }
332             return new ActionBuilder().setAction(
333                     new SetFieldCaseBuilder().setSetField(
334                             new SetFieldBuilder().setLayer3Match(
335                                     new Ipv4MatchBuilder().setIpv4Destination(
336                                             new Ipv4Prefix(sourceIp.getHostAddress())).build()).
337                                             build()).build()).setKey(new ActionKey(actionInfo.getActionKey())).build();
338
339         }
340
341     },
342     set_field_eth_src {
343
344         @Override
345         public Action buildAction(ActionInfo actionInfo) {
346             String[] actionValues = actionInfo.getActionValues();
347             MacAddress mac = new MacAddress(actionValues[0]);
348
349             return new ActionBuilder().setAction(
350                     new SetFieldCaseBuilder().setSetField(
351                             new SetFieldBuilder().setEthernetMatch(
352                                     new EthernetMatchBuilder().setEthernetSource(
353                                                     new EthernetSourceBuilder().setAddress(mac).build()).build())
354                                             .build()).build()).setKey(new ActionKey(actionInfo.getActionKey())).build();
355
356         }
357     },
358
359     drop_action {
360
361         @Override
362         public Action buildAction(ActionInfo actionInfo) {
363             DropActionBuilder dab = new DropActionBuilder();
364             DropAction dropAction = dab.build();
365             ActionBuilder ab = new ActionBuilder();
366             ab.setAction(new DropActionCaseBuilder().setDropAction(dropAction).build());
367             ab.setKey(new ActionKey(actionInfo.getActionKey())).build();
368             return ab.build();
369         }
370     },
371
372     nx_resubmit {
373
374         @Override
375         public Action buildAction(ActionInfo actionInfo) {
376             NxResubmitBuilder nxarsb = new NxResubmitBuilder();
377             nxarsb.setTable((short) 55);
378             ActionBuilder ab = new ActionBuilder();
379             ab.setAction(new NxActionResubmitRpcAddGroupCaseBuilder().setNxResubmit(nxarsb.build()).build());
380             ab.setKey(new ActionKey(actionInfo.getActionKey()));
381             return ab.build();
382         }
383     },
384
385     goto_table {
386
387         @Override
388         public Action buildAction(ActionInfo actionInfo) {
389             ActionBuilder ab = new ActionBuilder();
390             return null;
391         }
392     };
393
394     private static final int RADIX_HEX = 16;
395     public abstract Action buildAction(ActionInfo actionInfo);
396 }