2 * Copyright (c) 2013 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowjava.protocol.impl.util;
11 import io.netty.buffer.ByteBuf;
13 import java.util.List;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.EnhancedMessageTypeKey;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
18 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.StandardMatchType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 * Serializes ofp_match (OpenFlow v1.3)
29 * @author michal.polkorab
30 * @author timotej.kubas
32 public class OF13MatchSerializer implements OFSerializer<Match>, SerializerRegistryInjector {
33 private static final Logger LOGGER = LoggerFactory.getLogger(OF13MatchSerializer.class);
34 private static final byte STANDARD_MATCH_TYPE_CODE = 0;
35 private static final byte OXM_MATCH_TYPE_CODE = 1;
36 private SerializerRegistry registry;
39 public void serialize(Match match, ByteBuf outBuffer) {
41 LOGGER.debug("Match is null");
44 int matchStartIndex = outBuffer.writerIndex();
45 serializeType(match, outBuffer);
46 int matchLengthIndex = outBuffer.writerIndex();
47 outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
48 serializeMatchEntries(match.getMatchEntries(), outBuffer);
49 // Length of ofp_match (excluding padding)
50 int matchLength = outBuffer.writerIndex() - matchStartIndex;
51 outBuffer.setShort(matchLengthIndex, matchLength);
52 int paddingRemainder = matchLength % EncodeConstants.PADDING;
53 if (paddingRemainder != 0) {
54 ByteBufUtils.padBuffer(EncodeConstants.PADDING - paddingRemainder, outBuffer);
58 private static void serializeType(Match match, ByteBuf out) {
59 if (match.getType().isAssignableFrom(StandardMatchType.class)) {
60 out.writeShort(STANDARD_MATCH_TYPE_CODE);
61 } else if (match.getType().isAssignableFrom(OxmMatchType.class)) {
62 out.writeShort(OXM_MATCH_TYPE_CODE);
67 * Serializes MatchEntries
68 * @param matchEntries list of match entries (oxm_fields)
69 * @param out output ByteBuf
71 public void serializeMatchEntries(List<MatchEntries> matchEntries, ByteBuf out) {
72 if (matchEntries == null) {
73 LOGGER.debug("Match entries are null");
76 for (MatchEntries entry : matchEntries) {
77 OFSerializer<MatchEntries> entrySerializer = registry.getSerializer(
78 new EnhancedMessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, entry.getOxmClass(),
79 entry.getOxmMatchField()));
80 entrySerializer.serialize(entry, out);
85 public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
86 this.registry = serializerRegistry;