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
9 package org.opendaylight.openflowplugin.impl.protocol.serialization.messages;
11 import com.google.common.base.MoreObjects;
12 import com.google.common.collect.ImmutableMap;
13 import io.netty.buffer.ByteBuf;
14 import java.util.Objects;
15 import org.opendaylight.openflowjava.util.ByteBufUtils;
16 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
17 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortMessage;
24 * Translates PortMod messages.
25 * OF protocol versions: 1.3.
27 public class PortMessageSerializer extends AbstractMessageSerializer<PortMessage> {
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 DEFAULT_PORT_CONFIG_MASK = createPortConfigBitMask(
33 new PortConfig(true, true, true, true));
36 public void serialize(final PortMessage message, final ByteBuf outBuffer) {
37 final int index = outBuffer.writerIndex();
38 super.serialize(message, outBuffer);
39 outBuffer.writeInt(OpenflowPortsUtil
40 .getProtocolPortNumber(OpenflowVersion.OF13, message.getPortNumber()).intValue());
41 outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_01);
42 outBuffer.writeBytes(IetfYangUtil.INSTANCE.bytesFor(message.getHardwareAddress()));
43 outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_02);
44 outBuffer.writeInt(createPortConfigBitMask(message.getConfiguration()));
45 outBuffer.writeInt(MoreObjects
46 .firstNonNull(createPortConfigBitMask(message.getMask()), DEFAULT_PORT_CONFIG_MASK));
47 outBuffer.writeInt(createPortFeaturesBitMask(message.getAdvertisedFeatures()));
48 outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_03);
49 outBuffer.setShort(index + 2, outBuffer.writerIndex() - index);
53 protected byte getMessageType() {
57 private static Integer createPortConfigBitMask(final PortConfig config) {
58 return Objects.isNull(config) ? null : ByteBufUtils.fillBitMaskFromMap(ImmutableMap
59 .<Integer, Boolean>builder()
60 .put(0, config.isPORTDOWN())
61 .put(2, config.isNORECV())
62 .put(5, config.isNOFWD())
63 .put(6, config.isNOPACKETIN())
67 private static int createPortFeaturesBitMask(final PortFeatures feature) {
68 return ByteBufUtils.fillBitMask(0,
71 feature.isHundredMbHd(),
72 feature.isHundredMbFd(),
76 feature.isFortyGbFd(),
77 feature.isHundredGbFd(),
84 feature.isPauseAsym());