Bump upstreams
[openflowplugin.git] / openflowjava / 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 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
9
10 import io.netty.buffer.ByteBuf;
11 import java.util.HashMap;
12 import java.util.Map;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
15 import org.opendaylight.openflowjava.util.ByteBufUtils;
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.openflow.common.types.rev130731.PortConfig;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeatures;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortMod$G;
20
21 /**
22  * Translates PortMod messages. OF protocol versions: 1.3.
23  *
24  * @author timotej.kubas
25  * @author michal.polkorab
26  */
27 public class PortModInputMessageFactory implements OFSerializer<PortMod$G> {
28     private static final byte MESSAGE_TYPE = 16;
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
33     @Override
34     public void serialize(final PortMod$G message, final ByteBuf outBuffer) {
35         final int index = outBuffer.writerIndex();
36         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
37         outBuffer.writeInt(message.getPortNo().getValue().intValue());
38         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_01);
39         outBuffer.writeBytes(IetfYangUtil.macAddressBytes(message.getHwAddress()));
40         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_02);
41         outBuffer.writeInt(createPortConfigBitmask(message.getConfig()));
42         outBuffer.writeInt(createPortConfigBitmask(message.getMask()));
43         outBuffer.writeInt(createPortFeaturesBitmask(message.getAdvertise()));
44         outBuffer.writeZero(PADDING_IN_PORT_MOD_MESSAGE_03);
45         ByteBufUtils.updateOFHeaderLength(outBuffer, index);
46     }
47
48     private static int createPortConfigBitmask(final PortConfig config) {
49         Map<Integer, Boolean> portConfigMap = new HashMap<>();
50         portConfigMap.put(0, config.getPortDown());
51         portConfigMap.put(2, config.getNoRecv());
52         portConfigMap.put(5, config.getNoFwd());
53         portConfigMap.put(6, config.getNoPacketIn());
54
55         return ByteBufUtils.fillBitMaskFromMap(portConfigMap);
56     }
57
58     private static int createPortFeaturesBitmask(final PortFeatures feature) {
59         return ByteBufUtils.fillBitMask(0, feature.get_10mbHd(),
60                 feature.get_10mbFd(),
61                 feature.get_100mbHd(),
62                 feature.get_100mbFd(),
63                 feature.get_1gbHd(),
64                 feature.get_1gbFd(),
65                 feature.get_10gbFd(),
66                 feature.get_40gbFd(),
67                 feature.get_100gbFd(),
68                 feature.get_1tbFd(),
69                 feature.getOther(),
70                 feature.getCopper(),
71                 feature.getFiber(),
72                 feature.getAutoneg(),
73                 feature.getPause(),
74                 feature.getPauseAsym());
75     }
76
77 }