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 int index = outBuffer.writerIndex();
38 super.serialize(message, outBuffer);
39 outBuffer.writeInt(OpenflowPortsUtil.getProtocolPortNumber(OpenflowVersion.OF13, message.getPortNumber()).intValue());
40 outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_01);
41 outBuffer.writeBytes(IetfYangUtil.INSTANCE.bytesFor(message.getHardwareAddress()));
42 outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_02);
43 outBuffer.writeInt(createPortConfigBitMask(message.getConfiguration()));
44 outBuffer.writeInt(MoreObjects.firstNonNull(createPortConfigBitMask(message.getMask()), DEFAULT_PORT_CONFIG_MASK));
45 outBuffer.writeInt(createPortFeaturesBitMask(message.getAdvertisedFeatures()));
46 outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_03);
47 outBuffer.setShort(index + 2, outBuffer.writerIndex() - index);
51 protected byte getMessageType() {
55 private static Integer createPortConfigBitMask(final PortConfig config) {
56 return Objects.isNull(config) ? null : ByteBufUtils.fillBitMaskFromMap(ImmutableMap
57 .<Integer, Boolean>builder()
58 .put(0, config.isPORTDOWN())
59 .put(2, config.isNORECV())
60 .put(5, config.isNOFWD())
61 .put(6, config.isNOPACKETIN())
65 private static int createPortFeaturesBitMask(final PortFeatures feature) {
66 return ByteBufUtils.fillBitMask(0,
69 feature.isHundredMbHd(),
70 feature.isHundredMbFd(),
74 feature.isFortyGbFd(),
75 feature.isHundredGbFd(),
82 feature.isPauseAsym());