2 * Copyright (c) 2015 NetIDE Consortium and others. All rights reserved.
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
8 package org.opendaylight.openflowjava.protocol.impl.serialization.factories;
10 import io.netty.buffer.ByteBuf;
11 import java.nio.charset.StandardCharsets;
12 import java.util.HashMap;
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.PortConfigV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
24 * Translates PortStatus messages.
26 * @author giuseppex.petralia@intel.com
28 public class OF10PortStatusMessageFactory implements OFSerializer<PortStatusMessage> {
30 private static final byte MESSAGE_TYPE = 12;
31 private static final byte PADDING = 7;
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.writeShort(message.getPortNo().intValue());
39 outBuffer.writeBytes(IetfYangUtil.INSTANCE.macAddressBytes(message.getHwAddr()));
40 writeName(message.getName(), outBuffer);
41 writePortConfig(message.getConfigV10(), outBuffer);
42 writePortState(message.getStateV10(), outBuffer);
43 writePortFeature(message.getCurrentFeaturesV10(), outBuffer);
44 writePortFeature(message.getAdvertisedFeaturesV10(), outBuffer);
45 writePortFeature(message.getSupportedFeaturesV10(), outBuffer);
46 writePortFeature(message.getPeerFeaturesV10(), outBuffer);
47 ByteBufUtils.updateOFHeaderLength(outBuffer);
50 private static void writePortFeature(final PortFeaturesV10 feature, final ByteBuf outBuffer) {
51 Map<Integer, Boolean> map = new HashMap<>();
52 map.put(0, feature.is_10mbHd());
53 map.put(1, feature.is_10mbFd());
54 map.put(2, feature.is_100mbHd());
55 map.put(3, feature.is_100mbFd());
56 map.put(4, feature.is_1gbHd());
57 map.put(5, feature.is_1gbFd());
58 map.put(6, feature.is_10gbFd());
59 map.put(7, feature.isCopper());
60 map.put(8, feature.isFiber());
61 map.put(9, feature.isAutoneg());
62 map.put(10, feature.isPause());
63 map.put(11, feature.isPauseAsym());
64 int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
65 outBuffer.writeInt(bitmap);
68 private static void writePortState(final PortStateV10 state, final ByteBuf outBuffer) {
69 Map<Integer, Boolean> map = new HashMap<>();
70 map.put(0, state.isLinkDown());
71 map.put(1, state.isBlocked());
72 map.put(2, state.isLive());
73 map.put(3, state.isStpListen());
74 map.put(4, state.isStpLearn());
75 map.put(5, state.isStpForward());
76 map.put(6, state.isStpBlock());
77 map.put(7, state.isStpMask());
78 int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
79 outBuffer.writeInt(bitmap);
82 private static void writePortConfig(final PortConfigV10 config, final ByteBuf outBuffer) {
83 Map<Integer, Boolean> map = new HashMap<>();
84 map.put(0, config.isPortDown());
85 map.put(1, config.isNoStp());
86 map.put(2, config.isNoRecv());
87 map.put(3, config.isNoRecvStp());
88 map.put(4, config.isNoFlood());
89 map.put(5, config.isNoFwd());
90 map.put(6, config.isNoPacketIn());
91 int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
92 outBuffer.writeInt(bitmap);
95 private static void writeName(final String name, final ByteBuf outBuffer) {
96 byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
97 if (nameBytes.length < 16) {
98 byte[] nameBytesPadding = new byte[16];
100 for (byte b : nameBytes) {
101 nameBytesPadding[index] = b;
104 for (; index < 16; index++) {
105 nameBytesPadding[index] = 0x0;
107 outBuffer.writeBytes(nameBytesPadding);
109 outBuffer.writeBytes(nameBytes);