af7cec53cbc87c7b1c2a92fa06d0861969770b87
[openflowplugin.git] / openflowjava / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / match / AbstractOxmMatchEntryDeserializer.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.deserialization.match;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.buffer.ByteBuf;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.openflowjava.protocol.api.extensibility.HeaderDeserializer;
15 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
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 /**
23  * Base class for an Oxm match entry deserializer.
24  *
25  * @author michal.polkorab
26  */
27 public abstract class AbstractOxmMatchEntryDeserializer
28         implements HeaderDeserializer<MatchEntry>, OFDeserializer<MatchEntry> {
29     private final @NonNull OxmClassBase oxmClass;
30     private final @NonNull MatchField oxmField;
31
32     protected AbstractOxmMatchEntryDeserializer(final OxmClassBase oxmClass,
33             final MatchField oxmField) {
34         this.oxmClass = requireNonNull(oxmClass);
35         this.oxmField = requireNonNull(oxmField);
36     }
37
38     protected AbstractOxmMatchEntryDeserializer(final MatchField oxmField) {
39         this(OpenflowBasicClass.VALUE, oxmField);
40     }
41
42     @Override
43     public final MatchEntry deserialize(final ByteBuf input) {
44         final MatchEntryBuilder builder = processHeader(input);
45         deserialize(input, builder);
46         return builder.build();
47     }
48
49     protected abstract void deserialize(ByteBuf input, MatchEntryBuilder builder);
50
51     @Override
52     public final MatchEntry deserializeHeader(final ByteBuf input) {
53         return processHeader(input).build();
54     }
55
56     /**
57      * Prepares match entry header - sets oxm_class, oxm_field, hasMask
58      *  + sets the buffer.readerIndex() to the end of match entry
59      *  - where augmentation starts.
60      *
61      * @param input input bytebuf
62      * @return MatchEntriesBuilder which can be filled with MatchEntry augmentation
63      */
64     protected final MatchEntryBuilder processHeader(final ByteBuf input) {
65         // skip oxm_class (provided)
66         input.skipBytes(Short.BYTES);
67         final MatchEntryBuilder builder = new MatchEntryBuilder().setOxmClass(oxmClass).setOxmMatchField(oxmField)
68                 .setHasMask((input.readUnsignedByte() & 1) != 0);
69         // skip match entry length - not needed
70         input.skipBytes(Byte.BYTES);
71         return builder;
72     }
73 }