Copyright update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / InstructionsSerializer.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.ActionsInstruction;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterInstruction;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MetadataInstruction;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MeterIdInstruction;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.TableIdInstruction;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.action.rev130731.actions.ActionsList;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ApplyActions;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.ClearActions;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Experimenter;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.GotoTable;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.Meter;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteActions;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.WriteMetadata;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.Instructions;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Instruction;
30
31 /**
32  * Serializes ofp_instruction (OpenFlow v 1.3) structure
33  * @author michal.polkorab
34  * @author timotej.kubas
35  */
36 public abstract class InstructionsSerializer {
37
38     private static final byte GOTO_TABLE_TYPE = 1;
39     private static final byte WRITE_METADATA_TYPE = 2;
40     private static final byte WRITE_ACTIONS_TYPE = 3;
41     private static final byte APPLY_ACTIONS_TYPE = 4;
42     private static final byte CLEAR_ACTIONS_TYPE = 5;
43     private static final byte METER_TYPE = 6;
44     private static final byte EXPERIMENTER_TYPE = 7;
45     private static final byte GOTO_TABLE_LENGTH = 8;
46     private static final byte WRITE_METADATA_LENGTH = 24;
47     private static final byte WRITE_ACTIONS_LENGTH = 8;
48     private static final byte APPLY_ACTIONS_LENGTH = 8;
49     private static final byte CLEAR_ACTIONS_LENGTH = 8;
50     private static final byte METER_LENGTH = 8;
51     private static final byte EXPERIMENTER_LENGTH = 8;
52     private static final byte PADDING_IN_GOTO_TABLE = 3;
53     private static final byte PADDING_IN_WRITE_METADATA = 4;
54     private static final byte PADDING_IN_CLEAR_ACTIONS = 4;
55     
56     /**
57      * Encodes instructions
58      * @param instructions List of instructions
59      * @param out output buffer
60      */
61     public static void encodeInstructions(List<Instructions> instructions, ByteBuf out) {
62         if (instructions != null) {
63             for (Instructions instruction : instructions) {
64                 Class<? extends Instruction> type = instruction.getType();
65                 if (type.isAssignableFrom(GotoTable.class)) {
66                     writeTypeAndLength(out, GOTO_TABLE_TYPE, GOTO_TABLE_LENGTH);
67                     out.writeByte(instruction.getAugmentation(TableIdInstruction.class).getTableId());
68                     ByteBufUtils.padBuffer(PADDING_IN_GOTO_TABLE, out);
69                 } else if (type.isAssignableFrom(WriteMetadata.class)) {
70                     writeTypeAndLength(out, WRITE_METADATA_TYPE, WRITE_METADATA_LENGTH);
71                     ByteBufUtils.padBuffer(PADDING_IN_WRITE_METADATA, out);
72                     MetadataInstruction metadata = instruction.getAugmentation(MetadataInstruction.class);
73                     out.writeBytes(metadata.getMetadata());
74                     out.writeBytes(metadata.getMetadataMask());
75                 } else if (type.isAssignableFrom(WriteActions.class)) {
76                     writeActionsInstruction(out, instruction, WRITE_ACTIONS_TYPE);
77                 } else if (type.isAssignableFrom(ApplyActions.class)) {
78                     writeActionsInstruction(out, instruction, APPLY_ACTIONS_TYPE);
79                 } else if (type.isAssignableFrom(ClearActions.class)) {
80                     writeTypeAndLength(out, CLEAR_ACTIONS_TYPE, CLEAR_ACTIONS_LENGTH);
81                     ByteBufUtils.padBuffer(PADDING_IN_CLEAR_ACTIONS, out);
82                 } else if (type.isAssignableFrom(Meter.class)) {
83                     writeTypeAndLength(out, METER_TYPE, METER_LENGTH);
84                     out.writeInt(instruction.getAugmentation(MeterIdInstruction.class).getMeterId().intValue());
85                 } else if (type.isAssignableFrom(Experimenter.class)) {
86                     ExperimenterInstruction experimenter = instruction.getAugmentation(ExperimenterInstruction.class);
87                     byte[] data = experimenter.getData();
88                     writeTypeAndLength(out, EXPERIMENTER_TYPE, EXPERIMENTER_LENGTH + data.length);
89                     out.writeInt(experimenter.getExperimenter().intValue());
90                     out.writeBytes(data);
91                 }
92             }
93         }
94         
95     }
96
97     private static void writeTypeAndLength(ByteBuf out, int type, int length) {
98         out.writeShort(type);
99         out.writeShort(length);
100     }
101
102     private static void writeActionsInstruction(ByteBuf out,
103             Instructions instruction, int type) {
104         final byte ACTIONS_INSTRUCTION_LENGTH = 8;
105         final byte PADDING_IN_ACTIONS_INSTRUCTION = 4;
106         out.writeShort(type);
107         List<ActionsList> actions = instruction.getAugmentation(ActionsInstruction.class).getActionsList();
108         out.writeShort(ACTIONS_INSTRUCTION_LENGTH + ActionsSerializer.computeLengthOfActions(actions));
109         ByteBufUtils.padBuffer(PADDING_IN_ACTIONS_INSTRUCTION, out);
110         ActionsSerializer.encodeActions(actions, out);
111     }
112     
113     /**
114      * Computes length of instructions
115      * @param instructions List of instructions
116      * @return length of instructions (in bytes)
117      */
118     public static int computeInstructionsLength(List<Instructions> instructions) {
119         int length = 0;
120         if (instructions != null) {
121             for (Instructions instruction : instructions) {
122                 Class<? extends Instruction> type = instruction.getType();
123                 if (type.isAssignableFrom(GotoTable.class)) {
124                     length += GOTO_TABLE_LENGTH;
125                 } else if (type.isAssignableFrom(WriteMetadata.class)) {
126                     length += WRITE_METADATA_LENGTH;
127                 } else if (type.isAssignableFrom(WriteActions.class)) {
128                     length += WRITE_ACTIONS_LENGTH + ActionsSerializer.computeLengthOfActions(
129                             instruction.getAugmentation(ActionsInstruction.class).getActionsList());
130                 } else if (type.isAssignableFrom(ApplyActions.class)) {
131                     length += APPLY_ACTIONS_LENGTH + ActionsSerializer.computeLengthOfActions(
132                             instruction.getAugmentation(ActionsInstruction.class).getActionsList());
133                 } else if (type.isAssignableFrom(ClearActions.class)) {
134                     length += CLEAR_ACTIONS_LENGTH;
135                 } else if (type.isAssignableFrom(Meter.class)) {
136                     length += METER_LENGTH;
137                 } else if (type.isAssignableFrom(Experimenter.class)) {
138                     ExperimenterInstruction experimenter = instruction.getAugmentation(ExperimenterInstruction.class);
139                     byte[] data = experimenter.getData();
140                     length += EXPERIMENTER_LENGTH + data.length;
141                 }
142             }
143         }
144         return length;
145     }
146 }