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