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.ActionTypeV10;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortConfigV10;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortFeaturesV10;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortStateV10;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.features.reply.PhyPort;
27 * Translates FeaturesReply messages.
29 * @author giuseppex.petralia@intel.com
31 public class OF10FeaturesReplyMessageFactory implements OFSerializer<GetFeaturesOutput> {
33 private static final byte PADDING = 3;
34 private static final byte MESSAGE_TYPE = 6;
37 public void serialize(final GetFeaturesOutput message, final ByteBuf outBuffer) {
38 ByteBufUtils.writeOFHeader(MESSAGE_TYPE, message, outBuffer, EncodeConstants.EMPTY_LENGTH);
39 outBuffer.writeLong(message.getDatapathId().longValue());
40 outBuffer.writeInt(message.getBuffers().intValue());
41 outBuffer.writeByte(message.getTables().intValue());
42 outBuffer.writeZero(PADDING);
43 outBuffer.writeInt(createCapabilities(message.getCapabilitiesV10()));
44 outBuffer.writeInt(createActionsV10(message.getActionsV10()));
45 for (PhyPort port : message.getPhyPort()) {
46 outBuffer.writeShort(port.getPortNo().intValue());
47 outBuffer.writeBytes(IetfYangUtil.INSTANCE.macAddressBytes(port.getHwAddr()));
48 writeName(port.getName(), outBuffer);
49 writePortConfig(port.getConfigV10(), outBuffer);
50 writePortState(port.getStateV10(), outBuffer);
51 writePortFeature(port.getCurrentFeaturesV10(), outBuffer);
52 writePortFeature(port.getAdvertisedFeaturesV10(), outBuffer);
53 writePortFeature(port.getSupportedFeaturesV10(), outBuffer);
54 writePortFeature(port.getPeerFeaturesV10(), outBuffer);
56 ByteBufUtils.updateOFHeaderLength(outBuffer);
59 private static void writePortFeature(final PortFeaturesV10 feature, final ByteBuf outBuffer) {
60 Map<Integer, Boolean> map = new HashMap<>();
61 map.put(0, feature.is_10mbHd());
62 map.put(1, feature.is_10mbFd());
63 map.put(2, feature.is_100mbHd());
64 map.put(3, feature.is_100mbFd());
65 map.put(4, feature.is_1gbHd());
66 map.put(5, feature.is_1gbFd());
67 map.put(6, feature.is_10gbFd());
68 map.put(7, feature.isCopper());
69 map.put(8, feature.isFiber());
70 map.put(9, feature.isAutoneg());
71 map.put(10, feature.isPause());
72 map.put(11, feature.isPauseAsym());
73 int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
74 outBuffer.writeInt(bitmap);
77 private static void writePortState(final PortStateV10 state, final ByteBuf outBuffer) {
78 Map<Integer, Boolean> map = new HashMap<>();
79 map.put(0, state.isLinkDown());
80 map.put(1, state.isBlocked());
81 map.put(2, state.isLive());
82 map.put(3, state.isStpListen());
83 map.put(4, state.isStpLearn());
84 map.put(5, state.isStpForward());
85 map.put(6, state.isStpBlock());
86 map.put(7, state.isStpMask());
87 int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
88 outBuffer.writeInt(bitmap);
91 private static void writePortConfig(final PortConfigV10 config, final ByteBuf outBuffer) {
92 Map<Integer, Boolean> map = new HashMap<>();
93 map.put(0, config.isPortDown());
94 map.put(1, config.isNoStp());
95 map.put(2, config.isNoRecv());
96 map.put(3, config.isNoRecvStp());
97 map.put(4, config.isNoFlood());
98 map.put(5, config.isNoFwd());
99 map.put(6, config.isNoPacketIn());
100 int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
101 outBuffer.writeInt(bitmap);
104 private static int createCapabilities(final CapabilitiesV10 capabilities) {
105 Map<Integer, Boolean> map = new HashMap<>();
106 map.put(0, capabilities.isOFPCFLOWSTATS());
107 map.put(1, capabilities.isOFPCTABLESTATS());
108 map.put(2, capabilities.isOFPCPORTSTATS());
109 map.put(3, capabilities.isOFPCSTP());
110 map.put(4, capabilities.isOFPCRESERVED());
111 map.put(5, capabilities.isOFPCIPREASM());
112 map.put(6, capabilities.isOFPCQUEUESTATS());
113 map.put(7, capabilities.isOFPCARPMATCHIP());
114 int bitmap = ByteBufUtils.fillBitMaskFromMap(map);
118 private static int createActionsV10(final ActionTypeV10 action) {
119 return ByteBufUtils.fillBitMask(0, action.isOFPATOUTPUT(), action.isOFPATSETVLANVID(),
120 action.isOFPATSETVLANPCP(), action.isOFPATSTRIPVLAN(), action.isOFPATSETDLSRC(),
121 action.isOFPATSETDLDST(), action.isOFPATSETNWSRC(), action.isOFPATSETNWDST(), action.isOFPATSETNWTOS(),
122 action.isOFPATSETTPSRC(), action.isOFPATSETTPDST(), action.isOFPATENQUEUE(), action.isOFPATVENDOR());
126 private static void writeName(final String name, final ByteBuf outBuffer) {
127 byte[] nameBytes = name.getBytes(StandardCharsets.UTF_8);
128 if (nameBytes.length < 16) {
129 byte[] nameBytesPadding = new byte[16];
131 for (byte b : nameBytes) {
132 nameBytesPadding[index] = b;
135 for (; index < 16; index++) {
136 nameBytesPadding[index] = 0x0;
138 outBuffer.writeBytes(nameBytesPadding);
140 outBuffer.writeBytes(nameBytes);