2 * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.util;
11 import io.netty.buffer.ByteBuf;
13 import java.util.List;
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;
41 * Serializes ofp_action (OpenFlow v1.0) structures
42 * @author michal.polkorab
44 public abstract class OF10ActionsSerializer {
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;
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
83 public static void encodeActionsV10(ByteBuf out, List<ActionsList> actionsList) {
84 if (actionsList == null) {
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);
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);
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());
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);
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);
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);
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);
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]));
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);
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());
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());
199 * Computes length of actions
201 * @return length of actions (OpenFlow v1.0)
203 public static int computeActionsLength(List<ActionsList> actionsList) {
205 if (actionsList != null) {
206 for (ActionsList list : actionsList) {
207 Action action = list.getAction();
208 if (action.getType().equals(Output.class)) {
210 } else if (action.getType().equals(SetVlanVid.class)) {
212 } else if (action.getType().equals(SetVlanPcp.class)) {
214 } else if (action.getType().equals(StripVlan.class)) {
216 } else if (action.getType().equals(SetDlSrc.class)) {
218 } else if (action.getType().equals(SetDlDst.class)) {
220 } else if (action.getType().equals(SetNwSrc.class)) {
222 } else if (action.getType().equals(SetNwDst.class)) {
224 } else if (action.getType().equals(SetNwTos.class)) {
226 } else if (action.getType().equals(SetTpSrc.class)) {
228 } else if (action.getType().equals(SetTpDst.class)) {
230 } else if (action.getType().equals(Enqueue.class)) {
232 } else if (action.getType().equals(Experimenter.class)) {