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