Merge "Bug 1277 - Move ByteBuffUtils to separate bundle"
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PortModInputMessageFactory.java
1 /*
2  * Copyright (c) 2013 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.openflowjava.protocol.impl.serialization.factories;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.HashMap;
14 import java.util.Map;
15
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.openflowjava.util.ByteBufUtils;
18 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfig;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
22
23 /**
24  * Translates PortMod messages
25  * @author timotej.kubas
26  * @author michal.polkorab
27  */
28 public class PortModInputMessageFactory implements OFSerializer<PortModInput> {
29     private static final byte MESSAGE_TYPE = 16;
30     private static final byte PADDING_IN_PORT_MOD_MESSAGE_01 = 4;
31     private static final byte PADDING_IN_PORT_MOD_MESSAGE_02 = 2;
32     private static final byte PADDING_IN_PORT_MOD_MESSAGE_03 = 4;
33
34     @Override
35     public void serialize(final PortModInput message, final ByteBuf outBuffer) {
36         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
37         outBuffer.writeInt(message.getPortNo().getValue().intValue());
38         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_01, outBuffer);
39         outBuffer.writeBytes(ByteBufUtils.macAddressToBytes(message.getHwAddress().getValue()));
40         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_02, outBuffer);
41         outBuffer.writeInt(createPortConfigBitmask(message.getConfig()));
42         outBuffer.writeInt(createPortConfigBitmask(message.getMask()));
43         outBuffer.writeInt(createPortFeaturesBitmask(message.getAdvertise()));
44         ByteBufUtils.padBuffer(PADDING_IN_PORT_MOD_MESSAGE_03, outBuffer);
45         ByteBufUtils.updateOFHeaderLength(outBuffer);
46     }
47
48     /**
49      * @param config
50      * @return port config bitmask
51      */
52     private static int createPortConfigBitmask(final PortConfig config) {
53         int configBitmask = 0;
54         Map<Integer, Boolean> portConfigMap = new HashMap<>();
55         portConfigMap.put(0, config.isPortDown());
56         portConfigMap.put(2, config.isNoRecv());
57         portConfigMap.put(5, config.isNoFwd());
58         portConfigMap.put(6, config.isNoPacketIn());
59
60         configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);
61         return configBitmask;
62     }
63
64     private static int createPortFeaturesBitmask(final PortFeatures feature) {
65         return ByteBufUtils.fillBitMask(0, feature.is_10mbHd(),
66                 feature.is_10mbFd(),
67                 feature.is_100mbHd(),
68                 feature.is_100mbFd(),
69                 feature.is_1gbHd(),
70                 feature.is_1gbFd(),
71                 feature.is_10gbFd(),
72                 feature.is_40gbFd(),
73                 feature.is_100gbFd(),
74                 feature.is_1tbFd(),
75                 feature.isOther(),
76                 feature.isCopper(),
77                 feature.isFiber(),
78                 feature.isAutoneg(),
79                 feature.isPause(),
80                 feature.isPauseAsym());
81     }
82
83 }