Merge "Prepare upgrade to Netty 4.1"
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / factories / PortStatusMessageFactory.java
1 /*
2  * Copyright (c) 2015 NetIDE Consortium 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.common.types.rev130731.PortState;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
21
22 /**
23  * @author giuseppex.petralia@intel.com
24  *
25  */
26 public class PortStatusMessageFactory implements OFSerializer<PortStatusMessage> {
27
28     private static final byte MESSAGE_TYPE = 12;
29     private static final byte PADDING = 7;
30     private static final byte PORT_PADDING_1 = 4;
31     private static final byte PORT_PADDING_2 = 2;
32
33     @Override
34     public void serialize(final PortStatusMessage message, final ByteBuf outBuffer) {
35         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
36         outBuffer.writeByte(message.getReason().getIntValue());
37         outBuffer.writeZero(PADDING);
38         outBuffer.writeInt(message.getPortNo().intValue());
39         outBuffer.writeZero(PORT_PADDING_1);
40         outBuffer.writeBytes(IetfYangUtil.INSTANCE.bytesFor(message.getHwAddr()));
41         outBuffer.writeZero(PORT_PADDING_2);
42         writeName(message.getName(), outBuffer);
43         writePortConfig(message.getConfig(), outBuffer);
44         writePortState(message.getState(), outBuffer);
45         writePortFeatures(message.getCurrentFeatures(), outBuffer);
46         writePortFeatures(message.getAdvertisedFeatures(), outBuffer);
47         writePortFeatures(message.getSupportedFeatures(), outBuffer);
48         writePortFeatures(message.getPeerFeatures(), outBuffer);
49         outBuffer.writeInt(message.getCurrSpeed().intValue());
50         outBuffer.writeInt(message.getMaxSpeed().intValue());
51         ByteBufUtils.updateOFHeaderLength(outBuffer);
52     }
53
54     private void writePortConfig(final PortConfig config, final ByteBuf outBuffer) {
55         Map<Integer, Boolean> map = new HashMap<>();
56         map.put(0, config.isPortDown());
57         map.put(2, config.isNoRecv());
58         map.put(5, config.isNoFwd());
59         map.put(6, config.isNoPacketIn());
60         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
61         outBuffer.writeInt(bitmap);
62     }
63
64     private void writeName(final String name, final ByteBuf outBuffer) {
65         byte[] nameBytes = name.getBytes();
66         if (nameBytes.length < 16) {
67             byte[] nameBytesPadding = new byte[16];
68             int i = 0;
69             for (byte b : nameBytes) {
70                 nameBytesPadding[i] = b;
71                 i++;
72             }
73             for (; i < 16; i++) {
74                 nameBytesPadding[i] = 0x0;
75             }
76             outBuffer.writeBytes(nameBytesPadding);
77         } else {
78             outBuffer.writeBytes(nameBytes);
79         }
80
81     }
82
83     private void writePortState(final PortState state, final ByteBuf outBuffer) {
84         Map<Integer, Boolean> map = new HashMap<>();
85         map.put(0, state.isLinkDown());
86         map.put(1, state.isBlocked());
87         map.put(2, state.isLive());
88         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
89         outBuffer.writeInt(bitmap);
90     }
91
92     private void writePortFeatures(final PortFeatures features, final ByteBuf outBuffer) {
93         Map<Integer, Boolean> map = new HashMap<>();
94         map.put(0, features.is_10mbHd());
95         map.put(1, features.is_10mbFd());
96         map.put(2, features.is_100mbHd());
97         map.put(3, features.is_100mbFd());
98         map.put(4, features.is_1gbHd());
99         map.put(5, features.is_1gbFd());
100         map.put(6, features.is_10gbFd());
101         map.put(7, features.is_40gbFd());
102         map.put(8, features.is_100gbFd());
103         map.put(9, features.is_1tbFd());
104         map.put(10, features.isOther());
105         map.put(11, features.isCopper());
106         map.put(12, features.isFiber());
107         map.put(13, features.isAutoneg());
108         map.put(14, features.isPause());
109         map.put(15, features.isPauseAsym());
110         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
111         outBuffer.writeInt(bitmap);
112     }
113 }