Fix port update
[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
9 package org.opendaylight.openflowplugin.impl.protocol.serialization.messages;
10
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;
22
23 /**
24  * Translates PortMod messages.
25  * OF protocol versions: 1.3.
26  */
27 public class PortMessageSerializer extends AbstractMessageSerializer<PortMessage> {
28
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));
34
35     @Override
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);
48     }
49
50     @Override
51     protected byte getMessageType() {
52         return 16;
53     }
54
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())
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 }