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.SerializerRegistry;
17 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
18 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
19 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.ExperimenterIdMatchEntry;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.StandardMatchType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ExperimenterClass;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
30 * Serializes ofp_match (OpenFlow v1.3)
31 * @author michal.polkorab
32 * @author timotej.kubas
34 public class OF13MatchSerializer implements OFSerializer<Match>, SerializerRegistryInjector {
35 private static final Logger LOGGER = LoggerFactory.getLogger(OF13MatchSerializer.class);
36 private static final byte STANDARD_MATCH_TYPE_CODE = 0;
37 private static final byte OXM_MATCH_TYPE_CODE = 1;
38 private SerializerRegistry registry;
41 public void serialize(Match match, ByteBuf outBuffer) {
43 LOGGER.debug("Match is null");
46 int matchStartIndex = outBuffer.writerIndex();
47 serializeType(match, outBuffer);
48 int matchLengthIndex = outBuffer.writerIndex();
49 outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
50 serializeMatchEntries(match.getMatchEntries(), outBuffer);
51 // Length of ofp_match (excluding padding)
52 int matchLength = outBuffer.writerIndex() - matchStartIndex;
53 outBuffer.setShort(matchLengthIndex, matchLength);
54 int paddingRemainder = matchLength % EncodeConstants.PADDING;
55 if (paddingRemainder != 0) {
56 outBuffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
60 private static void serializeType(Match match, ByteBuf out) {
61 if (match.getType().isAssignableFrom(StandardMatchType.class)) {
62 out.writeShort(STANDARD_MATCH_TYPE_CODE);
63 } else if (match.getType().isAssignableFrom(OxmMatchType.class)) {
64 out.writeShort(OXM_MATCH_TYPE_CODE);
69 * Serializes MatchEntries
70 * @param matchEntries list of match entries (oxm_fields)
71 * @param out output ByteBuf
73 public void serializeMatchEntries(List<MatchEntries> matchEntries, ByteBuf out) {
74 if (matchEntries == null) {
75 LOGGER.debug("Match entries are null");
78 for (MatchEntries entry : matchEntries) {
80 MatchEntrySerializerKey<?, ?> key = new MatchEntrySerializerKey<>(
81 EncodeConstants.OF13_VERSION_ID, entry.getOxmClass(), entry.getOxmMatchField());
82 if (entry.getOxmClass().equals(ExperimenterClass.class)) {
83 key.setExperimenterId(entry.getAugmentation(ExperimenterIdMatchEntry.class)
84 .getExperimenter().getValue());
86 key.setExperimenterId(null);
88 OFSerializer<MatchEntries> entrySerializer = registry.getSerializer(key);
89 entrySerializer.serialize(entry, out);
94 public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
95 this.registry = serializerRegistry;