Javadoc update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PortModInputMessageFactory.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.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;\r
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;\r
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;\r
14 \r
15 /**\r
16  * Translates PortMod messages\r
17  * @author timotej.kubas\r
18  * @author michal.polkorab\r
19  */\r
20 public class PortModInputMessageFactory implements OFSerializer<PortModInput> {\r
21     private static final byte MESSAGE_TYPE = 16;\r
22     private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;\r
23     private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;\r
24     private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;\r
25     private static final int MESSAGE_LENGTH = 40;\r
26     private static PortModInputMessageFactory instance;\r
27     \r
28     private PortModInputMessageFactory() {\r
29         // singleton\r
30     }\r
31     \r
32     /**\r
33      * @return singleton factory\r
34      */\r
35     public static synchronized PortModInputMessageFactory getInstance() {\r
36         if (instance == null) {\r
37             instance = new PortModInputMessageFactory();\r
38         }\r
39         return instance;\r
40     }\r
41     \r
42     @Override\r
43     public void messageToBuffer(short version, ByteBuf out, PortModInput message) {\r
44         ByteBufUtils.writeOFHeader(instance, message, out);\r
45         out.writeInt(message.getPortNo().getValue().intValue());\r
46         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_01, out);\r
47         out.writeBytes(ByteBufUtils.hexStringToBytes(message.getHwAddress().getValue(), false));\r
48         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_02, out);\r
49         out.writeInt(createPortConfigBitmask(message.getConfig()));\r
50         out.writeInt(createPortConfigBitmask(message.getMask()));\r
51         out.writeInt(createPortFeaturesBitmask(message.getAdvertise()));\r
52         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_03, out);\r
53     }\r
54 \r
55     @Override\r
56     public int computeLength(PortModInput message) {\r
57         return MESSAGE_LENGTH;\r
58     }\r
59 \r
60     @Override\r
61     public byte getMessageType() {\r
62         return MESSAGE_TYPE;\r
63     }\r
64    \r
65     \r
66     /**\r
67      * @param config\r
68      * @return port config bitmask \r
69      */\r
70     private static int createPortConfigBitmask(PortConfig config) {\r
71         int configBitmask = 0;\r
72         Map<Integer, Boolean> portConfigMap = new HashMap<>();\r
73         portConfigMap.put(0, config.isPortDown());\r
74         portConfigMap.put(2, config.isNoRecv());\r
75         portConfigMap.put(5, config.isNoFwd());\r
76         portConfigMap.put(6, config.isNoPacketIn());\r
77         \r
78         configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);\r
79         return configBitmask;\r
80     }\r
81     \r
82     private static int createPortFeaturesBitmask(PortFeatures feature) {\r
83         int configBitmask = 0;\r
84         Map<Integer, Boolean> portFeaturesMap = new HashMap<>();\r
85         portFeaturesMap.put(0, feature.is_10mbHd());\r
86         portFeaturesMap.put(1, feature.is_10mbFd());\r
87         portFeaturesMap.put(2, feature.is_100mbHd());\r
88         portFeaturesMap.put(3, feature.is_100mbFd());\r
89         portFeaturesMap.put(4, feature.is_1gbHd());\r
90         portFeaturesMap.put(5, feature.is_1gbFd());\r
91         portFeaturesMap.put(6, feature.is_10gbFd());\r
92         portFeaturesMap.put(7, feature.is_40gbFd());\r
93         portFeaturesMap.put(8, feature.is_100gbFd());\r
94         portFeaturesMap.put(9, feature.is_1tbFd());\r
95         portFeaturesMap.put(10, feature.isOther());\r
96         portFeaturesMap.put(11, feature.isCopper());\r
97         portFeaturesMap.put(12, feature.isFiber());\r
98         portFeaturesMap.put(13, feature.isAutoneg());\r
99         portFeaturesMap.put(14, feature.isPause());\r
100         portFeaturesMap.put(15, feature.isPauseAsym());\r
101         \r
102         configBitmask = ByteBufUtils.fillBitMaskFromMap(portFeaturesMap);\r
103         return configBitmask;\r
104     }\r
105 }\r