2 * Copyright (c) 2019 Ericsson India Global Services Pvt Ltd. 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.eric.codec.match;
11 import io.netty.buffer.ByteBuf;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderDeserializer;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderSerializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
22 public abstract class AbstractMatchCodec implements
23 OFSerializer<MatchEntry>,
24 OFDeserializer<MatchEntry>,
25 HeaderSerializer<MatchEntry>,
26 HeaderDeserializer<MatchEntry> {
28 protected EricHeader headerWithMask;
29 protected EricHeader headerWithoutMask;
31 protected MatchEntryBuilder deserializeHeaderToBuilder(ByteBuf message) {
32 MatchEntryBuilder builder = new MatchEntryBuilder();
33 builder.setOxmClass(getOxmClass());
34 // skip oxm_class - provided
35 message.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES);
36 builder.setOxmMatchField(getEricField());
37 boolean hasMask = (message.readUnsignedByte() & 1) != 0;
38 builder.setHasMask(hasMask);
39 // skip experimenter class, match length and experimenter id - not needed
40 message.skipBytes(EncodeConstants.SIZE_OF_BYTE_IN_BYTES);
45 public MatchEntry deserializeHeader(ByteBuf message) {
46 return deserializeHeaderToBuilder(message).build();
50 public void serializeHeader(MatchEntry input, ByteBuf outBuffer) {
51 serializeHeader(getHeader(input.isHasMask()), outBuffer);
54 public void serializeHeader(EricHeader input, ByteBuf outBuffer) {
55 outBuffer.writeInt((int) input.toLong());
58 protected EricHeader getHeader(boolean hasMask) {
60 if (headerWithMask == null) {
61 headerWithMask = buildHeader(hasMask);
63 return headerWithMask;
65 if (headerWithoutMask == null) {
66 headerWithoutMask = buildHeader(hasMask);
68 return headerWithoutMask;
72 protected EricHeader buildHeader(boolean hasMask) {
73 return new EricHeader(
77 hasMask ? getValueLength() * 2 : getValueLength()
81 public EricHeader getHeaderWithoutHasMask() {
82 return getHeader(false);
85 public EricHeader getHeaderWithHasMask() {
86 return getHeader(true);
90 * Returns the numeric representation of eric_field.
92 public abstract int getEricFieldCode();
95 * Returns the numeric representation of oxm_class.
97 public abstract int getOxmClassCode();
100 * Returns the match entry value length.
102 public abstract int getValueLength();
105 * Returns the eric_field class.
107 public abstract Class<? extends MatchField> getEricField();
110 * Returns the oxm_class class.
112 public abstract Class<? extends OxmClassBase> getOxmClass();