Merge "OPNFLWPLUG-1062 Include additional LLDP fields in liblldp"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / messages / PortMessageSerializer.java
1 /*
2  * Copyright (c) 2016 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.protocol.serialization.messages;
9
10 import com.google.common.base.MoreObjects;
11 import com.google.common.collect.ImmutableMap;
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.openflowjava.util.ByteBufUtils;
14 import org.opendaylight.openflowplugin.api.openflow.md.util.OpenflowVersion;
15 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.IetfYangUtil;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortConfig;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortFeatures;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.PortMessage;
20
21 /**
22  * Translates PortMod messages.
23  * OF protocol versions: 1.3.
24  */
25 public class PortMessageSerializer extends AbstractMessageSerializer<PortMessage> {
26
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));
32
33     @Override
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.INSTANCE.bytesFor(message.getHardwareAddress()));
41         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_02);
42         outBuffer.writeInt(createPortConfigBitMask(message.getConfiguration()));
43         outBuffer.writeInt(MoreObjects
44                 .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);
48     }
49
50     @Override
51     protected byte getMessageType() {
52         return 16;
53     }
54
55     private static Integer createPortConfigBitMask(final PortConfig config) {
56         return config == null ? 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())
62                 .build());
63     }
64
65     private static int createPortFeaturesBitMask(final PortFeatures feature) {
66         return ByteBufUtils.fillBitMask(0,
67                 feature.isTenMbHd(),
68                 feature.isTenMbFd(),
69                 feature.isHundredMbHd(),
70                 feature.isHundredMbFd(),
71                 feature.isOneGbHd(),
72                 feature.isOneGbFd(),
73                 feature.isTenGbFd(),
74                 feature.isFortyGbFd(),
75                 feature.isHundredGbFd(),
76                 feature.isOneTbFd(),
77                 feature.isOther(),
78                 feature.isCopper(),
79                 feature.isFiber(),
80                 feature.isAutoeng(),
81                 feature.isPause(),
82                 feature.isPauseAsym());
83     }
84
85 }