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.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
23 * Translates PortMod messages
24 * @author timotej.kubas
25 * @author michal.polkorab
27 public class PortModInputMessageFactory implements OFSerializer<PortModInput> {
28 private static final byte MESSAGE_TYPE = 16;
29 private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
30 private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
31 private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
32 private static final int MESSAGE_LENGTH = 40;
33 private static PortModInputMessageFactory instance;
35 private PortModInputMessageFactory() {
40 * @return singleton factory
42 public static synchronized PortModInputMessageFactory getInstance() {
43 if (instance == null) {
44 instance = new PortModInputMessageFactory();
50 public void messageToBuffer(short version, ByteBuf out, PortModInput message) {
51 ByteBufUtils.writeOFHeader(instance, message, out);
52 out.writeInt(message.getPortNo().getValue().intValue());
53 ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_01, out);
54 out.writeBytes(ByteBufUtils.macAddressToBytes(message.getHwAddress().getValue()));
55 ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_02, out);
56 out.writeInt(createPortConfigBitmask(message.getConfig()));
57 out.writeInt(createPortConfigBitmask(message.getMask()));
58 out.writeInt(createPortFeaturesBitmask(message.getAdvertise()));
59 ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_03, out);
63 public int computeLength(PortModInput message) {
64 return MESSAGE_LENGTH;
68 public byte getMessageType() {
75 * @return port config bitmask
77 private static int createPortConfigBitmask(PortConfig config) {
78 int configBitmask = 0;
79 Map<Integer, Boolean> portConfigMap = new HashMap<>();
80 portConfigMap.put(0, config.isPortDown());
81 portConfigMap.put(2, config.isNoRecv());
82 portConfigMap.put(5, config.isNoFwd());
83 portConfigMap.put(6, config.isNoPacketIn());
85 configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);
89 private static int createPortFeaturesBitmask(PortFeatures feature) {
90 int configBitmask = 0;
91 Map<Integer, Boolean> portFeaturesMap = new HashMap<>();
92 portFeaturesMap.put(0, feature.is_10mbHd());
93 portFeaturesMap.put(1, feature.is_10mbFd());
94 portFeaturesMap.put(2, feature.is_100mbHd());
95 portFeaturesMap.put(3, feature.is_100mbFd());
96 portFeaturesMap.put(4, feature.is_1gbHd());
97 portFeaturesMap.put(5, feature.is_1gbFd());
98 portFeaturesMap.put(6, feature.is_10gbFd());
99 portFeaturesMap.put(7, feature.is_40gbFd());
100 portFeaturesMap.put(8, feature.is_100gbFd());
101 portFeaturesMap.put(9, feature.is_1tbFd());
102 portFeaturesMap.put(10, feature.isOther());
103 portFeaturesMap.put(11, feature.isCopper());
104 portFeaturesMap.put(12, feature.isFiber());
105 portFeaturesMap.put(13, feature.isAutoneg());
106 portFeaturesMap.put(14, feature.isPause());
107 portFeaturesMap.put(15, feature.isPauseAsym());
109 configBitmask = ByteBufUtils.fillBitMaskFromMap(portFeaturesMap);
110 return configBitmask;