Update OF header lenght
[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 import java.util.HashMap;
13 import java.util.Map;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
15 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
16 import org.opendaylight.openflowjava.util.ByteBufUtils;
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.openflow.common.types.rev130731.PortConfig;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortMod;
21
22 /**
23  * Translates PortMod messages.
24  * OF protocol versions: 1.3.
25  * @author timotej.kubas
26  * @author michal.polkorab
27  */
28 public class PortModInputMessageFactory implements OFSerializer<PortMod> {
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 PortMod message, final ByteBuf outBuffer) {
36         int index = outBuffer.writerIndex();
37         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
38         outBuffer.writeInt(message.getPortNo().getValue().intValue());
39         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_01);
40         outBuffer.writeBytes(IetfYangUtil.INSTANCE.bytesFor(message.getHwAddress()));
41         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_02);
42         outBuffer.writeInt(createPortConfigBitmask(message.getConfig()));
43         outBuffer.writeInt(createPortConfigBitmask(message.getMask()));
44         outBuffer.writeInt(createPortFeaturesBitmask(message.getAdvertise()));
45         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_03);
46         ByteBufUtils.updateOFHeaderLength(outBuffer, index);
47     }
48
49     /**
50      * @param config
51      * @return port config bitmask
52      */
53     private static int createPortConfigBitmask(final PortConfig config) {
54         int configBitmask = 0;
55         Map<Integer, Boolean> portConfigMap = new HashMap<>();
56         portConfigMap.put(0, config.isPortDown());
57         portConfigMap.put(2, config.isNoRecv());
58         portConfigMap.put(5, config.isNoFwd());
59         portConfigMap.put(6, config.isNoPacketIn());
60
61         configBitmask = ByteBufUtils.fillBitMaskFromMap(portConfigMap);
62         return configBitmask;
63     }
64
65     private static int createPortFeaturesBitmask(final PortFeatures feature) {
66         return ByteBufUtils.fillBitMask(0, feature.is_10mbHd(),
67                 feature.is_10mbFd(),
68                 feature.is_100mbHd(),
69                 feature.is_100mbFd(),
70                 feature.is_1gbHd(),
71                 feature.is_1gbFd(),
72                 feature.is_10gbFd(),
73                 feature.is_40gbFd(),
74                 feature.is_100gbFd(),
75                 feature.is_1tbFd(),
76                 feature.isOther(),
77                 feature.isCopper(),
78                 feature.isFiber(),
79                 feature.isAutoneg(),
80                 feature.isPause(),
81                 feature.isPauseAsym());
82     }
83
84 }