Update OF header lenght
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / FlowModInputMessageFactory.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.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
15 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
18 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMaker;
19 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory;
20 import org.opendaylight.openflowjava.util.ByteBufUtils;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.instruction.rev130731.instructions.grouping.Instruction;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowMod;
25
26 /**
27  * Translates FlowMod messages.
28  * OF protocol versions: 1.3.
29  * @author timotej.kubas
30  * @author michal.polkorab
31  */
32 public class FlowModInputMessageFactory implements OFSerializer<FlowMod>, SerializerRegistryInjector {
33     private static final byte MESSAGE_TYPE = 14;
34     private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
35     private static final TypeKeyMaker<Instruction> INSTRUCTION_KEY_MAKER =
36             TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
37     private SerializerRegistry registry;
38
39     @Override
40     public void serialize(final FlowMod message, final ByteBuf outBuffer) {
41         int index = outBuffer.writerIndex();
42         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
43         outBuffer.writeLong(message.getCookie().longValue());
44         outBuffer.writeLong(message.getCookieMask().longValue());
45         outBuffer.writeByte(message.getTableId().getValue().byteValue());
46         outBuffer.writeByte(message.getCommand().getIntValue());
47         outBuffer.writeShort(message.getIdleTimeout());
48         outBuffer.writeShort(message.getHardTimeout());
49         outBuffer.writeShort(message.getPriority());
50         outBuffer.writeInt(message.getBufferId().intValue());
51         outBuffer.writeInt(message.getOutPort().getValue().intValue());
52         outBuffer.writeInt(message.getOutGroup().intValue());
53         outBuffer.writeShort(createFlowModFlagsBitmask(message.getFlags()));
54         outBuffer.writeZero(PADDING_IN_FLOW_MOD_MESSAGE);
55         registry.<Match, OFSerializer<Match>>getSerializer(new MessageTypeKey<>(message.getVersion(), Match.class))
56             .serialize(message.getMatch(), outBuffer);
57         ListSerializer.serializeList(message.getInstruction(), INSTRUCTION_KEY_MAKER, registry, outBuffer);
58         ByteBufUtils.updateOFHeaderLength(outBuffer, index);
59     }
60
61     @Override
62     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
63         this.registry = serializerRegistry;
64     }
65
66     private static int createFlowModFlagsBitmask(final FlowModFlags flags) {
67         return ByteBufUtils.fillBitMask(0,
68                 flags.isOFPFFSENDFLOWREM(),
69                 flags.isOFPFFCHECKOVERLAP(),
70                 flags.isOFPFFRESETCOUNTS(),
71                 flags.isOFPFFNOPKTCOUNTS(),
72                 flags.isOFPFFNOBYTCOUNTS());
73     }
74
75 }