Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / OF10ActionsSerializer.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.util;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.List;
14
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.DlAddressAction;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterAction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.IpAddressAction;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaxLengthAction;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.NwTosAction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.PortAction;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.QueueIdAction;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanPcpAction;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.VlanVidAction;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Enqueue;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Experimenter;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.Output;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetDlDst;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetDlSrc;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwDst;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwSrc;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetNwTos;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetTpDst;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetTpSrc;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetVlanPcp;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.SetVlanVid;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.StripVlan;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.actions.list.Action;
39
40 /**
41  * Serializes ofp_action (OpenFlow v1.0) structures
42  * @author michal.polkorab
43  */
44 public abstract class OF10ActionsSerializer {
45     
46     private static final byte OUTPUT_CODE = 0;
47     private static final byte SET_VLAN_VID_CODE = 1;
48     private static final byte SET_VLAN_PCP_CODE = 2;
49     private static final byte STRIP_VLAN_CODE = 3;
50     private static final byte SET_DL_SRC_CODE = 4;
51     private static final byte SET_DL_DST_CODE = 5;
52     private static final byte SET_NW_SRC_CODE = 6;
53     private static final byte SET_NW_DST_CODE = 7;
54     private static final byte SET_NW_TOS_CODE = 8;
55     private static final byte SET_TP_SRC_CODE = 9;
56     private static final byte SET_TP_DST_CODE = 10;
57     private static final byte ENQUEUE_CODE = 11;
58     private static final int EXPERIMENTER_CODE = 65535; // 0xFFFF
59     private static final byte GENERIC_ACTION_LENGTH = 8;
60     private static final byte PADDING_IN_GENERIC_ACTION = 4;
61     private static final byte OUTPUT_LENGTH = 8;
62     private static final byte SET_VLAN_VID_LENGTH = 8;
63     private static final byte PADDING_IN_SET_VLAN_VID_ACTION = 2;
64     private static final byte SET_VLAN_PCP_LENGTH = 8;
65     private static final byte PADDING_IN_SET_VLAN_PCP_ACTION = 3;
66     private static final byte DL_ADDRESS_ACTION_LENGTH = 16;
67     private static final byte PADDING_IN_DL_ADDRESS_ACTION = 6;
68     private static final byte SET_NW_TOS_LENGTH = 8;
69     private static final byte PADDING_IN_SET_NW_TOS_ACTION = 3;
70     private static final byte IP_ADDRESS_ACTION_LENGTH = 8;
71     private static final byte TP_PORT_ACTION_LENGTH = 8;
72     private static final byte PADDING_IN_TP_PORT_ACTION = 2;
73     private static final byte ENQUEUE_LENGTH = 16;
74     private static final byte PADDING_IN_ENQUEUE_ACTION = 6;
75     private static final byte EXPERIMENTER_LENGTH = 8;
76
77     
78     /**
79      * Encodes ofp_action (OpenFlow v1.0) structures
80      * @param out output ByteBuf that actions will be written into
81      * @param actionsList actions to be encoded
82      */
83     public static void encodeActionsV10(ByteBuf out, List<ActionsList> actionsList) {
84         if (actionsList == null) {
85             return;
86         }
87         for (ActionsList list : actionsList) {
88             Action action = list.getAction();
89             if (action.getType().equals(Output.class)) {
90                 encodeOutputAction(action, out);
91             } else if (action.getType().equals(SetVlanVid.class)) {
92                 encodeSetVlanVidAction(action, out);
93             } else if (action.getType().equals(SetVlanPcp.class)) {
94                 encodeSetVlanPcpAction(action, out);
95             } else if (action.getType().equals(StripVlan.class)) {
96                 encodeGenericAction(STRIP_VLAN_CODE, out);
97             } else if (action.getType().equals(SetDlSrc.class)) {
98                 encodeDlAddressAction(action, out, SET_DL_SRC_CODE);
99             } else if (action.getType().equals(SetDlDst.class)) {
100                 encodeDlAddressAction(action, out, SET_DL_DST_CODE);
101             } else if (action.getType().equals(SetNwSrc.class)) {
102                 encodeIpAddressAction(action, out, SET_NW_SRC_CODE);
103             } else if (action.getType().equals(SetNwDst.class)) {
104                 encodeIpAddressAction(action, out, SET_NW_DST_CODE);
105             } else if (action.getType().equals(SetNwTos.class)) {
106                 encodeNwTosAction(action, out);
107             } else if (action.getType().equals(SetTpSrc.class)) {
108                 encodeTpPortAction(action, out, SET_TP_SRC_CODE);
109             } else if (action.getType().equals(SetTpDst.class)) {
110                 encodeTpPortAction(action, out, SET_TP_DST_CODE);
111             } else if (action.getType().equals(Enqueue.class)) {
112                 encodeEnqueueAction(action, out);
113             } else if (action.getType().equals(Experimenter.class)) {
114                 encodeExperimenterAction(action, out);
115             }
116         }
117     }
118     
119     private static void encodeGenericAction(byte code, ByteBuf out) {
120         out.writeShort(code);
121         out.writeShort(GENERIC_ACTION_LENGTH);
122         ByteBufUtils.padBuffer(PADDING_IN_GENERIC_ACTION, out);
123     }
124     
125     private static void encodeOutputAction(Action action, ByteBuf out) {
126         out.writeShort(OUTPUT_CODE);
127         out.writeShort(OUTPUT_LENGTH);
128         PortAction port = action.getAugmentation(PortAction.class);
129         out.writeShort(port.getPort().getValue().intValue());
130         MaxLengthAction maxlength = action.getAugmentation(MaxLengthAction.class);
131         out.writeShort(maxlength.getMaxLength());
132     }
133     
134     private static void encodeSetVlanVidAction(Action action, ByteBuf out) {
135         out.writeShort(SET_VLAN_VID_CODE);
136         out.writeShort(SET_VLAN_VID_LENGTH);
137         out.writeShort(action.getAugmentation(VlanVidAction.class).getVlanVid());
138         ByteBufUtils.padBuffer(PADDING_IN_SET_VLAN_VID_ACTION, out);
139     }
140     
141     private static void encodeSetVlanPcpAction(Action action, ByteBuf out) {
142         out.writeShort(SET_VLAN_PCP_CODE);
143         out.writeShort(SET_VLAN_PCP_LENGTH);
144         out.writeByte(action.getAugmentation(VlanPcpAction.class).getVlanPcp());
145         ByteBufUtils.padBuffer(PADDING_IN_SET_VLAN_PCP_ACTION, out);
146     }
147     
148     private static void encodeDlAddressAction(Action action, ByteBuf out, byte code) {
149         out.writeShort(code);
150         out.writeShort(DL_ADDRESS_ACTION_LENGTH);
151         out.writeBytes(action.getAugmentation(DlAddressAction.class)
152                 .getDlAddress().getValue().getBytes());
153         ByteBufUtils.padBuffer(PADDING_IN_DL_ADDRESS_ACTION, out);
154     }
155     
156     private static void encodeNwTosAction(Action action, ByteBuf out) {
157         out.writeShort(SET_NW_TOS_CODE);
158         out.writeShort(SET_NW_TOS_LENGTH);
159         out.writeByte(action.getAugmentation(NwTosAction.class).getNwTos());
160         ByteBufUtils.padBuffer(PADDING_IN_SET_NW_TOS_ACTION, out);
161     }
162     
163     private static void encodeIpAddressAction(Action action, ByteBuf out, byte code) {
164         out.writeShort(code);
165         out.writeShort(IP_ADDRESS_ACTION_LENGTH);
166         String[] addressGroups = action.
167                 getAugmentation(IpAddressAction.class).getIpAddress().getValue().split("\\.");
168         for (int i = 0; i < addressGroups.length; i++) {
169             out.writeByte(Integer.parseInt(addressGroups[i]));
170         }
171     }
172     
173     private static void encodeTpPortAction(Action action, ByteBuf out, byte code) {
174         out.writeShort(code);
175         out.writeShort(TP_PORT_ACTION_LENGTH);
176         PortAction port = action.getAugmentation(PortAction.class);
177         out.writeShort(port.getPort().getValue().intValue());
178         ByteBufUtils.padBuffer(PADDING_IN_TP_PORT_ACTION, out);
179     }
180     
181     private static void encodeEnqueueAction(Action action, ByteBuf out) {
182         out.writeShort(ENQUEUE_CODE);
183         out.writeShort(ENQUEUE_LENGTH);
184         PortAction port = action.getAugmentation(PortAction.class);
185         out.writeShort(port.getPort().getValue().intValue());
186         ByteBufUtils.padBuffer(PADDING_IN_ENQUEUE_ACTION, out);
187         QueueIdAction queueId = action.getAugmentation(QueueIdAction.class);
188         out.writeShort(queueId.getQueueId().intValue());
189     }
190     
191     private static void encodeExperimenterAction(Action action, ByteBuf outBuffer) {
192         outBuffer.writeShort(EXPERIMENTER_CODE);
193         outBuffer.writeShort(EXPERIMENTER_LENGTH);
194         ExperimenterAction experimenter = action.getAugmentation(ExperimenterAction.class);
195         outBuffer.writeInt(experimenter.getExperimenter().intValue());
196     }
197     
198     /**
199      * Computes length of actions
200      * @param actionsList
201      * @return length of actions (OpenFlow v1.0)
202      */
203     public static int computeActionsLength(List<ActionsList> actionsList) {
204         int length = 0;
205         if (actionsList != null) {
206             for (ActionsList list : actionsList) {
207                 Action action = list.getAction();
208                 if (action.getType().equals(Output.class)) {
209                     length += 8;
210                 } else if (action.getType().equals(SetVlanVid.class)) {
211                     length += 8;
212                 } else if (action.getType().equals(SetVlanPcp.class)) {
213                     length += 8;
214                 } else if (action.getType().equals(StripVlan.class)) {
215                     length += 8;
216                 } else if (action.getType().equals(SetDlSrc.class)) {
217                     length += 16;
218                 } else if (action.getType().equals(SetDlDst.class)) {
219                     length += 16;
220                 } else if (action.getType().equals(SetNwSrc.class)) {
221                     length += 8;
222                 } else if (action.getType().equals(SetNwDst.class)) {
223                     length += 8;
224                 } else if (action.getType().equals(SetNwTos.class)) {
225                     length += 8;
226                 } else if (action.getType().equals(SetTpSrc.class)) {
227                     length += 8;
228                 } else if (action.getType().equals(SetTpDst.class)) {
229                     length += 8;
230                 } else if (action.getType().equals(Enqueue.class)) {
231                     length += 16;
232                 } else if (action.getType().equals(Experimenter.class)) {
233                     length += 8;
234                 }
235             }
236         }
237         return length;
238     }
239
240 }