Updated extension registration keys
[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.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.extensibility.keys.MatchEntrySerializerKey;
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.augments.rev131002.ExperimenterMatchEntry;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.StandardMatchType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.ExperimenterClass;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.OxmMatchType;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.match.grouping.Match;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * Serializes ofp_match (OpenFlow v1.3)
32  * @author michal.polkorab
33  * @author timotej.kubas
34  */
35 public class OF13MatchSerializer implements OFSerializer<Match>, SerializerRegistryInjector {
36     private static final Logger LOGGER = LoggerFactory.getLogger(OF13MatchSerializer.class);
37     private static final byte STANDARD_MATCH_TYPE_CODE = 0;
38     private static final byte OXM_MATCH_TYPE_CODE = 1;
39     private SerializerRegistry registry;
40
41     @Override
42     public void serialize(Match match, ByteBuf outBuffer) {
43         if (match == null) {
44             LOGGER.debug("Match is null");
45             return;
46         }
47         int matchStartIndex = outBuffer.writerIndex();
48         serializeType(match, outBuffer);
49         int matchLengthIndex = outBuffer.writerIndex();
50         outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
51         serializeMatchEntries(match.getMatchEntries(), outBuffer);
52         // Length of ofp_match (excluding padding)
53         int matchLength = outBuffer.writerIndex() - matchStartIndex;
54         outBuffer.setShort(matchLengthIndex, matchLength);
55         int paddingRemainder = matchLength % EncodeConstants.PADDING;
56         if (paddingRemainder != 0) {
57             ByteBufUtils.padBuffer(EncodeConstants.PADDING - paddingRemainder, outBuffer);
58         }
59     }
60
61     private static void serializeType(Match match, ByteBuf out) {
62         if (match.getType().isAssignableFrom(StandardMatchType.class)) {
63             out.writeShort(STANDARD_MATCH_TYPE_CODE);
64         } else if (match.getType().isAssignableFrom(OxmMatchType.class)) {
65             out.writeShort(OXM_MATCH_TYPE_CODE);
66         }
67     }
68
69     /**
70      * Serializes MatchEntries
71      * @param matchEntries list of match entries (oxm_fields)
72      * @param out output ByteBuf
73      */
74     public void serializeMatchEntries(List<MatchEntries> matchEntries, ByteBuf out) {
75         if (matchEntries == null) {
76             LOGGER.debug("Match entries are null");
77             return;
78         }
79         for (MatchEntries entry : matchEntries) {
80             
81             MatchEntrySerializerKey<?, ?> key = new MatchEntrySerializerKey<>(
82                     EncodeConstants.OF13_VERSION_ID, entry.getOxmClass(), entry.getOxmMatchField());
83             if (entry.getOxmClass().equals(ExperimenterClass.class)) {
84                 key.setExperimenterId(entry.getAugmentation(ExperimenterMatchEntry.class).getExperimenter());
85             } else {
86                 key.setExperimenterId(null);
87             }
88             OFSerializer<MatchEntries> entrySerializer = registry.getSerializer(key);
89             entrySerializer.serialize(entry, out);
90         }
91     }
92
93     @Override
94     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
95         this.registry = serializerRegistry;
96     }
97
98 }