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