Bump upstreams
[openflowplugin.git] / openflowjava / 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.nio.charset.StandardCharsets;
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.common.types.rev130731.PortState;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
22
23 /**
24  * Translates PortStatus messages.
25  *
26  * @author giuseppex.petralia@intel.com
27  */
28 public class PortStatusMessageFactory implements OFSerializer<PortStatusMessage> {
29
30     private static final byte MESSAGE_TYPE = 12;
31     private static final byte PADDING = 7;
32     private static final byte PORT_PADDING_1 = 4;
33     private static final byte PORT_PADDING_2 = 2;
34
35     @Override
36     public void serialize(final PortStatusMessage message, final ByteBuf outBuffer) {
37         ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
38         outBuffer.writeByte(message.getReason().getIntValue());
39         outBuffer.writeZero(PADDING);
40         outBuffer.writeInt(message.getPortNo().intValue());
41         outBuffer.writeZero(PORT_PADDING_1);
42         outBuffer.writeBytes(IetfYangUtil.macAddressBytes(message.getHwAddr()));
43         outBuffer.writeZero(PORT_PADDING_2);
44         writeName(message.getName(), outBuffer);
45         writePortConfig(message.getConfig(), outBuffer);
46         writePortState(message.getState(), outBuffer);
47         writePortFeatures(message.getCurrentFeatures(), outBuffer);
48         writePortFeatures(message.getAdvertisedFeatures(), outBuffer);
49         writePortFeatures(message.getSupportedFeatures(), outBuffer);
50         writePortFeatures(message.getPeerFeatures(), outBuffer);
51         outBuffer.writeInt(message.getCurrSpeed().intValue());
52         outBuffer.writeInt(message.getMaxSpeed().intValue());
53         ByteBufUtils.updateOFHeaderLength(outBuffer);
54     }
55
56     private static void writePortConfig(final PortConfig config, final ByteBuf outBuffer) {
57         Map<Integer, Boolean> map = new HashMap<>();
58         map.put(0, config.getPortDown());
59         map.put(2, config.getNoRecv());
60         map.put(5, config.getNoFwd());
61         map.put(6, config.getNoPacketIn());
62         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
63         outBuffer.writeInt(bitmap);
64     }
65
66     private static void writeName(final String name, final ByteBuf outBuffer) {
67         byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
68         if (nameBytes.length < 16) {
69             byte[] nameBytesPadding = new byte[16];
70             int index = 0;
71             for (byte b : nameBytes) {
72                 nameBytesPadding[index] = b;
73                 index++;
74             }
75             for (; index < 16; index++) {
76                 nameBytesPadding[index] = 0x0;
77             }
78             outBuffer.writeBytes(nameBytesPadding);
79         } else {
80             outBuffer.writeBytes(nameBytes);
81         }
82     }
83
84     private static void writePortState(final PortState state, final ByteBuf outBuffer) {
85         Map<Integer, Boolean> map = new HashMap<>();
86         map.put(0, state.getLinkDown());
87         map.put(1, state.getBlocked());
88         map.put(2, state.getLive());
89         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
90         outBuffer.writeInt(bitmap);
91     }
92
93     private static void writePortFeatures(final PortFeatures features, final ByteBuf outBuffer) {
94         Map<Integer, Boolean> map = new HashMap<>();
95         map.put(0, features.get_10mbHd());
96         map.put(1, features.get_10mbFd());
97         map.put(2, features.get_100mbHd());
98         map.put(3, features.get_100mbFd());
99         map.put(4, features.get_1gbHd());
100         map.put(5, features.get_1gbFd());
101         map.put(6, features.get_10gbFd());
102         map.put(7, features.get_40gbFd());
103         map.put(8, features.get_100gbFd());
104         map.put(9, features.get_1tbFd());
105         map.put(10, features.getOther());
106         map.put(11, features.getCopper());
107         map.put(12, features.getFiber());
108         map.put(13, features.getAutoneg());
109         map.put(14, features.getPause());
110         map.put(15, features.getPauseAsym());
111         int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
112         outBuffer.writeInt(bitmap);
113     }
114 }