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.serialization.factories;
11 import io.netty.buffer.ByteBuf;
13 import java.util.HashMap;
16 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;
17 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;
18 import org.opendaylight.openflowjava.protocol.impl.util.InstructionsSerializer;
19 import org.opendaylight.openflowjava.protocol.impl.util.MatchSerializer;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
24 * Translates FlowMod messages
25 * @author timotej.kubas
26 * @author michal.polkorab
28 public class FlowModInputMessageFactory implements OFSerializer<FlowModInput> {
29 private static final byte MESSAGE_TYPE = 14;
30 private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;
31 private static final int MESSAGE_LENGTH = 48;
32 private static FlowModInputMessageFactory instance;
34 private FlowModInputMessageFactory() {
39 * @return singleton factory
41 public static synchronized FlowModInputMessageFactory getInstance() {
42 if(instance == null) {
43 instance = new FlowModInputMessageFactory();
49 public void messageToBuffer(short version, ByteBuf out, FlowModInput message) {
50 ByteBufUtils.writeOFHeader(instance, message, out);
51 out.writeLong(message.getCookie().longValue());
52 out.writeLong(message.getCookieMask().longValue());
53 out.writeByte(message.getTableId().getValue().byteValue());
54 out.writeByte(message.getCommand().getIntValue());
55 out.writeShort(message.getIdleTimeout().intValue());
56 out.writeShort(message.getHardTimeout().intValue());
57 out.writeShort(message.getPriority());
58 out.writeInt(message.getBufferId().intValue());
59 out.writeInt(message.getOutPort().getValue().intValue());
60 out.writeInt(message.getOutGroup().intValue());
61 out.writeShort(createFlowModFlagsBitmask(message.getFlags()));
62 ByteBufUtils.padBuffer(PADDING_IN_FLOW_MOD_MESSAGE, out);
63 MatchSerializer.encodeMatch(message.getMatch(), out);
64 InstructionsSerializer.encodeInstructions(message.getInstruction(), out);
68 public int computeLength(FlowModInput message) {
69 return MESSAGE_LENGTH + MatchSerializer.computeMatchLength(message.getMatch())
70 + InstructionsSerializer.computeInstructionsLength(message.getInstruction());
74 public byte getMessageType() {
78 private static int createFlowModFlagsBitmask(FlowModFlags flags) {
79 int flowModFlagBitmask = 0;
80 Map<Integer, Boolean> flowModFlagsMap = new HashMap<>();
81 flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM());
82 flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP());
83 flowModFlagsMap.put(2, flags.isOFPFFRESETCOUNTS());
84 flowModFlagsMap.put(3, flags.isOFPFFNOPKTCOUNTS());
85 flowModFlagsMap.put(4, flags.isOFPFFNOBYTCOUNTS());
87 flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap);
88 return flowModFlagBitmask;