Use ByteBuf.readRetainedSlice()
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / multipart / tablefeatures / matchfield / AbstractMatchFieldSerializer.java
1 /*
2  * Copyright (c) 2017 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 package org.opendaylight.openflowplugin.impl.protocol.serialization.multipart.tablefeatures.matchfield;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
12 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.set.field.match.SetFieldMatch;
13
14 public abstract class AbstractMatchFieldSerializer implements OFSerializer<SetFieldMatch> {
15
16     @Override
17     public void serialize(final SetFieldMatch setFieldMatch, final ByteBuf byteBuf) {
18         byteBuf.writeShort(getOxmClassCode());
19
20         int fieldAndMask = getOxmFieldCode() << 1;
21         int length = getValueLength();
22
23         if (setFieldMatch.getHasMask()) {
24             fieldAndMask |= 1;
25             length *= 2;
26         }
27
28         byteBuf.writeByte(fieldAndMask);
29         byteBuf.writeByte(length);
30     }
31
32     protected abstract int getOxmClassCode();
33
34     protected abstract int getOxmFieldCode();
35
36     protected abstract int getValueLength();
37 }