Remove EncodeConstants.SIZE_OF_{BYTE,SHORT,INT,LONG}_IN_BYTES
[openflowplugin.git] / extension / openflowjava-extension-eric / src / main / java / org / opendaylight / openflowjava / eric / codec / match / AbstractMatchCodec.java
1 /*
2  * Copyright (c) 2019 Ericsson India Global Services Pvt Ltd. 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.eric.codec.match;
9
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;
19
20 public abstract class AbstractMatchCodec implements
21         OFSerializer<MatchEntry>,
22         OFDeserializer<MatchEntry>,
23         HeaderSerializer<MatchEntry>,
24         HeaderDeserializer<MatchEntry> {
25
26     protected EricHeader headerWithMask;
27     protected EricHeader headerWithoutMask;
28
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);
39         return builder;
40     }
41
42     @Override
43     public MatchEntry deserializeHeader(ByteBuf message) {
44         return deserializeHeaderToBuilder(message).build();
45     }
46
47     @Override
48     public void serializeHeader(MatchEntry input, ByteBuf outBuffer) {
49         serializeHeader(getHeader(input.isHasMask()), outBuffer);
50     }
51
52     public void serializeHeader(EricHeader input, ByteBuf outBuffer) {
53         outBuffer.writeInt((int) input.toLong());
54     }
55
56     protected EricHeader getHeader(boolean hasMask) {
57         if (hasMask) {
58             if (headerWithMask == null) {
59                 headerWithMask = buildHeader(hasMask);
60             }
61             return headerWithMask;
62         } else {
63             if (headerWithoutMask == null) {
64                 headerWithoutMask = buildHeader(hasMask);
65             }
66             return headerWithoutMask;
67         }
68     }
69
70     protected EricHeader buildHeader(boolean hasMask) {
71         return new EricHeader(
72                 getOxmClassCode(),
73                 getEricFieldCode(),
74                 hasMask,
75                 hasMask ? getValueLength() * 2 : getValueLength()
76         );
77     }
78
79     public EricHeader getHeaderWithoutHasMask() {
80         return getHeader(false);
81     }
82
83     public EricHeader getHeaderWithHasMask() {
84         return getHeader(true);
85     }
86
87     /**
88      * Returns the numeric representation of eric_field.
89      */
90     public abstract int getEricFieldCode();
91
92     /**
93      * Returns the numeric representation of oxm_class.
94      */
95     public abstract int getOxmClassCode();
96
97     /**
98      * Returns the match entry value length.
99      */
100     public abstract int getValueLength();
101
102     /**
103      * Returns the eric_field class.
104      */
105     public abstract Class<? extends MatchField> getEricField();
106
107     /**
108      * Returns the oxm_class class.
109      */
110     public abstract Class<? extends OxmClassBase> getOxmClass();
111 }