Bump upstreams
[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 static java.util.Objects.requireNonNullElse;
11
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;
21
22 /**
23  * Translates PortMod messages.
24  * OF protocol versions: 1.3.
25  */
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));
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.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);
47     }
48
49     @Override
50     protected byte getMessageType() {
51         return 16;
52     }
53
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())
61                 .build());
62     }
63
64     private static int createPortFeaturesBitMask(final PortFeatures feature) {
65         return ByteBufUtils.fillBitMask(0,
66                 feature.getTenMbHd(),
67                 feature.getTenMbFd(),
68                 feature.getHundredMbHd(),
69                 feature.getHundredMbFd(),
70                 feature.getOneGbHd(),
71                 feature.getOneGbFd(),
72                 feature.getTenGbFd(),
73                 feature.getFortyGbFd(),
74                 feature.getHundredGbFd(),
75                 feature.getOneTbFd(),
76                 feature.getOther(),
77                 feature.getCopper(),
78                 feature.getFiber(),
79                 feature.getAutoeng(),
80                 feature.getPause(),
81                 feature.getPauseAsym());
82     }
83 }