b208e9e449fd7a6a513f1bf4f9794fb8e751dc1f
[openflowplugin.git] / openflowjava / 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 import java.util.List;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistry;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.SerializerRegistryInjector;
16 import org.opendaylight.openflowjava.protocol.api.keys.MatchEntrySerializerKey;
17 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev150225.experimenter.id.match.entry.ExperimenterIdCase;
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.rev150225.ExperimenterClass;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmMatchType;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.grouping.Match;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Serializes ofp_match (OpenFlow v1.3).
29  *
30  * @author michal.polkorab
31  * @author timotej.kubas
32  */
33 public class OF13MatchSerializer implements OFSerializer<Match>, SerializerRegistryInjector {
34     private static final Logger LOG = 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(final Match match, final ByteBuf outBuffer) {
41         if (match == null) {
42             LOG.debug("Match is null");
43             return;
44         }
45         final int matchStartIndex = outBuffer.writerIndex();
46         serializeType(match, outBuffer);
47         int matchLengthIndex = outBuffer.writerIndex();
48         outBuffer.writeShort(EncodeConstants.EMPTY_LENGTH);
49         serializeMatchEntries(match.getMatchEntry(), 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             outBuffer.writeZero(EncodeConstants.PADDING - paddingRemainder);
56         }
57     }
58
59     private static void serializeType(final Match match, final ByteBuf out) {
60         if (match.getType() instanceof StandardMatchType) {
61             out.writeShort(STANDARD_MATCH_TYPE_CODE);
62         } else if (match.getType() instanceof OxmMatchType) {
63             out.writeShort(OXM_MATCH_TYPE_CODE);
64         }
65     }
66
67     /**
68      * Serializes MatchEntries.
69      *
70      * @param matchEntries list of match entries (oxm_fields)
71      * @param out output ByteBuf
72      */
73     public void serializeMatchEntries(final List<MatchEntry> matchEntries, final ByteBuf out) {
74         if (matchEntries == null) {
75             LOG.debug("Match entries are null");
76             return;
77         }
78         for (MatchEntry entry : matchEntries) {
79
80             MatchEntrySerializerKey<?, ?> key = new MatchEntrySerializerKey<>(
81                     EncodeConstants.OF_VERSION_1_3, entry.getOxmClass(), entry.getOxmMatchField());
82             if (ExperimenterClass.VALUE.equals(entry.getOxmClass())) {
83                 ExperimenterIdCase entryValue = (ExperimenterIdCase) entry.getMatchEntryValue();
84                 key.setExperimenterId(entryValue.getExperimenter().getExperimenter().getValue());
85             } else {
86                 key.setExperimenterId(null);
87             }
88             OFSerializer<MatchEntry> entrySerializer = registry.getSerializer(key);
89             entrySerializer.serialize(entry, out);
90         }
91     }
92
93     @Override
94     public void injectSerializerRegistry(final SerializerRegistry serializerRegistry) {
95         registry = serializerRegistry;
96     }
97 }