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
8 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
10 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
11 import io.netty.buffer.ByteBuf;
12 import java.util.Objects;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.openflowjava.protocol.impl.util.ListSerializer;
19 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMaker;
20 import org.opendaylight.openflowjava.protocol.impl.util.TypeKeyMakerFactory;
21 import org.opendaylight.openflowjava.util.ByteBufUtils;
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.rev150225.match.grouping.Match;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowMod;
28 * Translates FlowMod messages. OF protocol versions: 1.3.
30 * @author timotej.kubas
31 * @author michal.polkorab
33 public class FlowModInputMessageFactory implements OFSerializer<FlowMod>, SerializerRegistryInjector {
34 private static final byte MESSAGE_TYPE = 14;
35 private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
36 private static final TypeKeyMaker<Instruction> INSTRUCTION_KEY_MAKER =
37 TypeKeyMakerFactory.createInstructionKeyMaker(EncodeConstants.OF13_VERSION_ID);
38 private SerializerRegistry registry;
41 @SuppressFBWarnings("UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR") // FB doesn't recognize Objects.requireNonNull
42 public void serialize(final FlowMod message, final ByteBuf outBuffer) {
43 Objects.requireNonNull(registry);
45 final int index = outBuffer.writerIndex();
46 ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
47 outBuffer.writeLong(message.getCookie().longValue());
48 outBuffer.writeLong(message.getCookieMask().longValue());
49 outBuffer.writeByte(message.getTableId().getValue().byteValue());
50 outBuffer.writeByte(message.getCommand().getIntValue());
51 outBuffer.writeShort(message.getIdleTimeout().toJava());
52 outBuffer.writeShort(message.getHardTimeout().toJava());
53 outBuffer.writeShort(message.getPriority().toJava());
54 outBuffer.writeInt(message.getBufferId().intValue());
55 outBuffer.writeInt(message.getOutPort().getValue().intValue());
56 outBuffer.writeInt(message.getOutGroup().intValue());
57 outBuffer.writeShort(createFlowModFlagsBitmask(message.getFlags()));
58 outBuffer.writeZero(PADDING_IN_FLOW_MOD_MESSAGE);
59 registry.<Match, OFSerializer<Match>>getSerializer(
60 new MessageTypeKey<>(message.getVersion().toJava(), Match.class)) .serialize(message.getMatch(), outBuffer);
61 ListSerializer.serializeList(message.getInstruction(), INSTRUCTION_KEY_MAKER, registry, outBuffer);
62 ByteBufUtils.updateOFHeaderLength(outBuffer, index);
66 public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
67 this.registry = serializerRegistry;
70 private static int createFlowModFlagsBitmask(final FlowModFlags flags) {
71 return ByteBufUtils.fillBitMask(0,
72 flags.getOFPFFSENDFLOWREM(),
73 flags.getOFPFFCHECKOVERLAP(),
74 flags.getOFPFFRESETCOUNTS(),
75 flags.getOFPFFNOPKTCOUNTS(),
76 flags.getOFPFFNOBYTCOUNTS());