e2196daf12377cff9621fa80720e88391c7e28ef
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / util / OF13MatchSerializer.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. and others. All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.openflowjava.protocol.impl.util;
10
11 import io.netty.buffer.ByteBuf;
12
13 import java.util.List;
14
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.openflowjava.util.ByteBufUtils;
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.OxmMatchType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Serializes ofp_match (OpenFlow v1.3)
30  * @author michal.polkorab
31  * @author timotej.kubas
32  */
33 public class OF13MatchSerializer implements OFSerializer<Match>, SerializerRegistryInjector {
34     private static final Logger LOGGER = LoggerFactory.getLogger(OF13MatchSerializer.class);
35     private static final byte STANDARD_MATCH_TYPE_CODE = 0;
36     private static final byte OXM_MATCH_TYPE_CODE = 1;
37     private SerializerRegistry registry;
38
39     @Override
40     public void serialize(Match match, ByteBuf outBuffer) {
41         if (match == null) {
42             LOGGER.debug("Match is null");
43             return;
44         }
45         int matchStartIndex = outBuffer.writerIndex();
46         serializeType(match, outBuffer);
47         int matchLengthIndex = outBuffer.writerIndex();
48         outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
49         serializeMatchEntries(match.getMatchEntries(), outBuffer);
50         // Length of ofp_match (excluding padding)
51         int matchLength = outBuffer.writerIndex() - matchStartIndex;
52         outBuffer.setShort(matchLengthIndex, matchLength);
53         int paddingRemainder = matchLength % EncodeConstants.PADDING;
54         if (paddingRemainder != 0) {
55             ByteBufUtils.padBuffer(EncodeConstants.PADDING - paddingRemainder, outBuffer);
56         }
57     }
58
59     private static void serializeType(Match match, ByteBuf out) {
60         if (match.getType().isAssignableFrom(StandardMatchType.class)) {
61             out.writeShort(STANDARD_MATCH_TYPE_CODE);
62         } else if (match.getType().isAssignableFrom(OxmMatchType.class)) {
63             out.writeShort(OXM_MATCH_TYPE_CODE);
64         }
65     }
66
67     /**
68      * Serializes MatchEntries
69      * @param matchEntries list of match entries (oxm_fields)
70      * @param out output ByteBuf
71      */
72     public void serializeMatchEntries(List<MatchEntries> matchEntries, ByteBuf out) {
73         if (matchEntries == null) {
74             LOGGER.debug("Match entries are null");
75             return;
76         }
77         for (MatchEntries entry : matchEntries) {
78             OFSerializer<MatchEntries> entrySerializer = registry.getSerializer(
79                     new EnhancedMessageTypeKey<>(EncodeConstants.OF13_VERSION_ID, entry.getOxmClass(),
80                             entry.getOxmMatchField()));
81             entrySerializer.serialize(entry, out);
82         }
83     }
84
85     @Override
86     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
87         this.registry = serializerRegistry;
88     }
89
90 }