Mass replace CRLF->LF
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / serialization / match / AbstractOxmMatchEntrySerializer.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 package org.opendaylight.openflowjava.protocol.impl.serialization.match;
9
10 import io.netty.buffer.ByteBuf;
11
12 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderSerializer;
13 import org.opendaylight.openflowjava.protocol.api.extensibility.OFSerializer;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.augments.rev131002.MaskMatchEntry;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev130731.oxm.fields.grouping.MatchEntries;
16
17 /**
18  * Parent for all match entry serializers
19  * @author michal.polkorab
20  */
21 public abstract class AbstractOxmMatchEntrySerializer
22     implements OFSerializer<MatchEntries>, HeaderSerializer<MatchEntries>{
23
24     @Override
25     public void serialize(MatchEntries entry, ByteBuf outBuffer) {
26         serializeHeader(entry, outBuffer);
27     }
28
29     @Override
30     public void serializeHeader(MatchEntries entry, ByteBuf outBuffer) {
31         outBuffer.writeShort(getOxmClassCode());
32         writeOxmFieldAndLength(outBuffer, getOxmFieldCode(), entry.isHasMask(),
33                 getValueLength());
34     }
35
36     protected static void writeMask(MatchEntries entry, ByteBuf out, int length) {
37         if (entry.isHasMask()) {
38             byte[] mask = entry.getAugmentation(MaskMatchEntry.class).getMask();
39             if (mask != null && mask.length != length) {
40                 throw new IllegalArgumentException("incorrect length of mask: "+
41                         mask.length + ", expected: " + length);
42             }
43             out.writeBytes(mask);
44         }
45     }
46
47     protected static void writeOxmFieldAndLength(ByteBuf out, int fieldValue, boolean hasMask, int lengthArg) {
48         int fieldAndMask = fieldValue << 1;
49         int length = lengthArg;
50         if (hasMask) {
51             fieldAndMask |= 1;
52             length *= 2;
53         }
54         out.writeByte(fieldAndMask);
55         out.writeByte(length);
56     }
57
58     /**
59      * @return numeric representation of oxm_field
60      */
61     protected abstract int getOxmFieldCode();
62
63     /**
64      * @return numeric representation of oxm_class
65      */
66     protected abstract int getOxmClassCode();
67
68     /**
69      * @return match entry value length (without mask length)
70      */
71     protected abstract int getValueLength();
72
73 }