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.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.api.extensibility.RegistryInjector;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
18 import org.opendaylight.openflowjava.protocol.impl.deserialization.EnhancedMessageTypeKey;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.StandardMatchType;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
27 * Serializes ofp_match (OpenFlow v1.3)
28 * @author michal.polkorab
29 * @author timotej.kubas
31 public class OF13MatchSerializer implements OFSerializer<Match>, RegistryInjector {
32 private static final Logger LOGGER = LoggerFactory.getLogger(OF13MatchSerializer.class);
33 private static final byte STANDARD_MATCH_TYPE_CODE = 0;
34 private static final byte OXM_MATCH_TYPE_CODE = 1;
35 private SerializerRegistry registry;
38 public void serialize(Match match, ByteBuf outBuffer) {
40 LOGGER.debug("Match is null");
43 int matchStartIndex = outBuffer.writerIndex();
44 serializeType(match, outBuffer);
45 int matchLengthIndex = outBuffer.writerIndex();
46 outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
47 serializeMatchEntries(match.getMatchEntries(), outBuffer);
48 // Length of ofp_match (excluding padding)
49 int matchLength = outBuffer.writerIndex() - matchStartIndex;
50 outBuffer.setShort(matchLengthIndex, matchLength);
51 int paddingRemainder = matchLength % EncodeConstants.PADDING;
52 if (paddingRemainder != 0) {
53 ByteBufUtils.padBuffer(EncodeConstants.PADDING - paddingRemainder, outBuffer);
57 private static void serializeType(Match match, ByteBuf out) {
58 if (match.getType().isAssignableFrom(StandardMatchType.class)) {
59 out.writeShort(STANDARD_MATCH_TYPE_CODE);
60 } else if (match.getType().isAssignableFrom(OxmMatchType.class)) {
61 out.writeShort(OXM_MATCH_TYPE_CODE);
66 * Serializes MatchEntries
67 * @param matchEntries list of match entries (oxm_fields)
68 * @param out output ByteBuf
70 public void serializeMatchEntries(List<MatchEntries> matchEntries, ByteBuf out) {
71 if (matchEntries == null) {
72 LOGGER.debug("Match entries are null");
75 for (MatchEntries entry : matchEntries) {
76 OFSerializer<MatchEntries> entrySerializer = registry.getSerializer(
77 new EnhancedMessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, entry.getOxmClass(),
78 entry.getOxmMatchField()));
79 entrySerializer.serialize(entry, out);
84 public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
85 this.registry = serializerRegistry;