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