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
8 package org.opendaylight.openflowjava.eric.codec.match;
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderDeserializer;
12 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderSerializer;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
20 public abstract class AbstractMatchCodec implements
21 OFSerializer<MatchEntry>,
22 OFDeserializer<MatchEntry>,
23 HeaderSerializer<MatchEntry>,
24 HeaderDeserializer<MatchEntry> {
26 protected EricHeader headerWithMask;
27 protected EricHeader headerWithoutMask;
29 protected MatchEntryBuilder deserializeHeaderToBuilder(ByteBuf message) {
30 MatchEntryBuilder builder = new MatchEntryBuilder();
31 builder.setOxmClass(getOxmClass());
32 // skip oxm_class - provided
33 message.skipBytes(Short.BYTES);
34 builder.setOxmMatchField(getEricField());
35 boolean hasMask = (message.readUnsignedByte() & 1) != 0;
36 builder.setHasMask(hasMask);
37 // skip experimenter class, match length and experimenter id - not needed
38 message.skipBytes(Byte.BYTES);
43 public MatchEntry deserializeHeader(ByteBuf message) {
44 return deserializeHeaderToBuilder(message).build();
48 public void serializeHeader(MatchEntry input, ByteBuf outBuffer) {
49 serializeHeader(getHeader(input.isHasMask()), outBuffer);
52 public void serializeHeader(EricHeader input, ByteBuf outBuffer) {
53 outBuffer.writeInt((int) input.toLong());
56 protected EricHeader getHeader(boolean hasMask) {
58 if (headerWithMask == null) {
59 headerWithMask = buildHeader(hasMask);
61 return headerWithMask;
63 if (headerWithoutMask == null) {
64 headerWithoutMask = buildHeader(hasMask);
66 return headerWithoutMask;
70 protected EricHeader buildHeader(boolean hasMask) {
71 return new EricHeader(
75 hasMask ? getValueLength() * 2 : getValueLength()
79 public EricHeader getHeaderWithoutHasMask() {
80 return getHeader(false);
83 public EricHeader getHeaderWithHasMask() {
84 return getHeader(true);
88 * Returns the numeric representation of eric_field.
90 public abstract int getEricFieldCode();
93 * Returns the numeric representation of oxm_class.
95 public abstract int getOxmClassCode();
98 * Returns the match entry value length.
100 public abstract int getValueLength();
103 * Returns the eric_field class.
105 public abstract Class<? extends MatchField> getEricField();
108 * Returns the oxm_class class.
110 public abstract Class<? extends OxmClassBase> getOxmClass();