Merge "Optional fields - reverted"
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / FlowModInputMessageFactory.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 \r
6 import java.util.HashMap;\r
7 import java.util.Map;\r
8 \r
9 import org.opendaylight.openflowjava.protocol.impl.serialization.OFSerializer;\r
10 import org.opendaylight.openflowjava.protocol.impl.util.ByteBufUtils;\r
11 import org.opendaylight.openflowjava.protocol.impl.util.InstructionsSerializer;\r
12 import org.opendaylight.openflowjava.protocol.impl.util.MatchSerializer;\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.FlowModFlags;\r
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;\r
15 \r
16 /**\r
17  * Translates FlowMod messages\r
18  * @author timotej.kubas\r
19  * @author michal.polkorab\r
20  */\r
21 public class FlowModInputMessageFactory implements OFSerializer<FlowModInput> {\r
22     private static final byte MESSAGE_TYPE = 14;\r
23     private static final byte PADDING_IN_FLOW_MOD_MESSAGE = 2;\r
24     private static final int MESSAGE_LENGTH = 48;\r
25     private static FlowModInputMessageFactory instance;\r
26    \r
27     private FlowModInputMessageFactory() {\r
28         // singleton\r
29     }\r
30     \r
31     /**\r
32      * @return singleton factory\r
33      */\r
34     public static synchronized FlowModInputMessageFactory getInstance() {\r
35         if(instance == null) {\r
36             instance = new FlowModInputMessageFactory();\r
37         }\r
38         return instance;\r
39     }\r
40     \r
41     @Override\r
42     public void messageToBuffer(short version, ByteBuf out, FlowModInput message) {\r
43         ByteBufUtils.writeOFHeader(instance, message, out);\r
44         out.writeLong(message.getCookie().longValue());\r
45         out.writeLong(message.getCookieMask().longValue());\r
46         out.writeByte(message.getTableId().getValue().byteValue());\r
47         out.writeByte(message.getCommand().getIntValue());\r
48         out.writeShort(message.getIdleTimeout().intValue());\r
49         out.writeShort(message.getHardTimeout().intValue());\r
50         out.writeShort(message.getPriority());\r
51         out.writeInt(message.getBufferId().intValue());\r
52         out.writeInt(message.getOutPort().getValue().intValue());\r
53         out.writeInt(message.getOutGroup().intValue());\r
54         out.writeShort(createFlowModFlagsBitmask(message.getFlags()));\r
55         ByteBufUtils.padBuffer(PADDING_IN_FLOW_MOD_MESSAGE, out);\r
56         MatchSerializer.encodeMatch(message.getMatch(), out);\r
57         InstructionsSerializer.encodeInstructions(message.getInstructions(), out);\r
58     }\r
59 \r
60     @Override\r
61     public int computeLength(FlowModInput message) {\r
62         return MESSAGE_LENGTH + MatchSerializer.computeMatchLength(message.getMatch())\r
63                 + InstructionsSerializer.computeInstructionsLength(message.getInstructions());\r
64     }\r
65 \r
66     @Override\r
67     public byte getMessageType() {\r
68         return MESSAGE_TYPE;\r
69     }\r
70 \r
71     private static int createFlowModFlagsBitmask(FlowModFlags flags) {\r
72         int flowModFlagBitmask = 0;\r
73         Map<Integer, Boolean> flowModFlagsMap = new HashMap<>();\r
74         flowModFlagsMap.put(0, flags.isOFPFFSENDFLOWREM());\r
75         flowModFlagsMap.put(1, flags.isOFPFFCHECKOVERLAP());\r
76         flowModFlagsMap.put(2, flags.isOFPFFRESETCOUNTS());\r
77         flowModFlagsMap.put(3, flags.isOFPFFNOPKTCOUNTS());\r
78         flowModFlagsMap.put(4, flags.isOFPFFNOBYTCOUNTS());\r
79         \r
80         flowModFlagBitmask = ByteBufUtils.fillBitMaskFromMap(flowModFlagsMap);\r
81         return flowModFlagBitmask;\r
82     }\r
83     \r
84     \r
85 }\r