Extensibility support (serialization part)
[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.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;
25
26 /**
27  * Serializes ofp_match (OpenFlow v1.3)
28  * @author michal.polkorab
29  * @author timotej.kubas
30  */
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;
36
37     @Override
38     public void serialize(Match match, ByteBuf outBuffer) {
39         if (match == null) {
40             LOGGER.debug("Match is null");
41             return;
42         }
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);
54         }
55     }
56
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);
62         }
63     }
64
65     /**
66      * Serializes MatchEntries
67      * @param matchEntries list of match entries (oxm_fields)
68      * @param out output ByteBuf
69      */
70     public void serializeMatchEntries(List<MatchEntries> matchEntries, ByteBuf out) {
71         if (matchEntries == null) {
72             LOGGER.debug("Match entries are null");
73             return;
74         }
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);
80         }
81     }
82
83     @Override
84     public void injectSerializerRegistry(SerializerRegistry serializerRegistry) {
85         this.registry = serializerRegistry;
86     }
87
88 }