Bug 2756 - Match model update
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / deserialization / match / OxmVlanVidDeserializer.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 io.netty.buffer.ByteBuf;
11
12 import org.opendaylight.openflowjava.protocol.api.extensibility.OFDeserializer;
13 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.MatchField;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OpenflowBasicClass;
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.VlanVid;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.VlanVidCaseBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.vlan.vid._case.VlanVidBuilder;
22
23 /**
24  * @author michal.polkorab
25  *
26  */
27 public class OxmVlanVidDeserializer extends AbstractOxmMatchEntryDeserializer
28         implements OFDeserializer<MatchEntry> {
29
30     @Override
31     public MatchEntry deserialize(ByteBuf input) {
32         MatchEntryBuilder builder = processHeader(getOxmClass(), getOxmField(), input);
33         addVlanVidValue(input, builder);
34         return builder.build();
35     }
36
37     private static void addVlanVidValue(ByteBuf input, MatchEntryBuilder builder) {
38         VlanVidCaseBuilder caseBuilder = new VlanVidCaseBuilder();
39         VlanVidBuilder vlanBuilder = new VlanVidBuilder();
40         int vidEntryValue = input.readUnsignedShort();
41         vlanBuilder.setCfiBit((vidEntryValue & (1 << 12)) != 0); // cfi is 13-th bit
42         vlanBuilder.setVlanVid(vidEntryValue & ((1 << 12) - 1)); // value without 13-th bit
43         if (builder.isHasMask()) {
44             vlanBuilder.setMask(OxmDeserializerHelper
45                     .convertMask(input, EncodeConstants.SIZE_OF_SHORT_IN_BYTES));
46         }
47         caseBuilder.setVlanVid(vlanBuilder.build());
48         builder.setMatchEntryValue(caseBuilder.build());
49     }
50
51     @Override
52     protected Class<? extends MatchField> getOxmField() {
53         return VlanVid.class;
54     }
55
56     @Override
57     protected Class<? extends OxmClassBase> getOxmClass() {
58         return OpenflowBasicClass.class;
59     }
60 }