2 * Copyright (c) 2016 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
8 package org.opendaylight.openflowplugin.impl.protocol.serialization.messages;
10 import static java.util.Objects.requireNonNullElse;
12 import com.google.common.collect.ImmutableMap;
13 import io.netty.buffer.ByteBuf;
14 import org.opendaylight.openflowjava.util.ByteBufUtils;
15 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
16 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
17 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortMessage;
23 * Translates PortMod messages.
24 * OF protocol versions: 1.3.
26 public class PortMessageSerializer extends AbstractMessageSerializer<PortMessage> {
27 private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
28 private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
29 private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
30 private static final Integer DEFAULT_PORT_CONFIG_MASK = createPortConfigBitMask(
31 new PortConfig(true, true, true, true));
34 public void serialize(final PortMessage message, final ByteBuf outBuffer) {
35 final int index = outBuffer.writerIndex();
36 super.serialize(message, outBuffer);
37 outBuffer.writeInt(OpenflowPortsUtil
38 .getProtocolPortNumber(OpenflowVersion.OF13, message.getPortNumber()).intValue());
39 outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_01);
40 outBuffer.writeBytes(IetfYangUtil.macAddressBytes(message.getHardwareAddress()));
41 outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_02);
42 outBuffer.writeInt(createPortConfigBitMask(message.getConfiguration()));
43 outBuffer.writeInt(requireNonNullElse(createPortConfigBitMask(message.getMask()), DEFAULT_PORT_CONFIG_MASK));
44 outBuffer.writeInt(createPortFeaturesBitMask(message.getAdvertisedFeatures()));
45 outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_03);
46 outBuffer.setShort(index + 2, outBuffer.writerIndex() - index);
50 protected byte getMessageType() {
54 private static Integer createPortConfigBitMask(final PortConfig config) {
55 return config == null ? null : ByteBufUtils.fillBitMaskFromMap(ImmutableMap
56 .<Integer, Boolean>builder()
57 .put(0, config.getPORTDOWN())
58 .put(2, config.getNORECV())
59 .put(5, config.getNOFWD())
60 .put(6, config.getNOPACKETIN())
64 private static int createPortFeaturesBitMask(final PortFeatures feature) {
65 return ByteBufUtils.fillBitMask(0,
68 feature.getHundredMbHd(),
69 feature.getHundredMbFd(),
73 feature.getFortyGbFd(),
74 feature.getHundredGbFd(),
81 feature.getPauseAsym());