1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
2 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
\r
4 import io.netty.buffer.ByteBuf;
\r
6 import java.util.HashMap;
\r
7 import java.util.Map;
\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
16 * @author timotej.kubas
\r
17 * @author michal.polkorab
\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
27 private PortModInputMessageFactory() {
\r
32 * @return singleton factory
\r
34 public static synchronized PortModInputMessageFactory getInstance() {
\r
35 if (instance == null) {
\r
36 instance = new PortModInputMessageFactory();
\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
55 public int computeLength(PortModInput message) {
\r
56 return MESSAGE_LENGTH;
\r
60 public byte getMessageType() {
\r
61 return MESSAGE_TYPE;
\r
67 * @return port config bitmask
\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
77 configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);
\r
78 return configBitmask;
\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
101 configBitmask = ByteBufUtils.fillBitMaskFromMap(portFeaturesMap);
\r
102 return configBitmask;
\r