Merge "OPNFLWPLUG-1076 Migrate lldp-speaker, forwardingrules-sync and arbitratorrecon...
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / VlanVidEntrySerializer.java
1 /*
2  * Copyright (c) 2016 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.openflowplugin.impl.protocol.serialization.match;
9
10 import io.netty.buffer.ByteBuf;
11 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
12 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
13 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanId;
15
16 public class VlanVidEntrySerializer extends AbstractMatchEntrySerializer {
17     private static final byte[] VLAN_VID_MASK = new byte[]{16, 0};
18
19     @Override
20     public void serialize(final Match match, final ByteBuf outBuffer) {
21         super.serialize(match, outBuffer);
22         final org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId vlanId =
23                 match.getVlanMatch().getVlanId().getVlanId();
24
25         int vlanVidValue = vlanId != null ? vlanId.getValue().toJava() : 0;
26
27         if (Boolean.TRUE.equals(match.getVlanMatch().getVlanId().isVlanIdPresent())) {
28             short cfi = 1 << 12;
29             vlanVidValue = vlanVidValue | cfi;
30         }
31
32         outBuffer.writeShort(vlanVidValue);
33
34         if (getHasMask(match)) {
35             writeMask(VLAN_VID_MASK, outBuffer, getValueLength());
36         }
37     }
38
39     @Override
40     public boolean matchTypeCheck(final Match match) {
41         return match.getVlanMatch() != null && match.getVlanMatch().getVlanId() != null;
42     }
43
44     @Override
45     protected boolean getHasMask(final Match match) {
46         final VlanId vlanId = match.getVlanMatch().getVlanId();
47         return Boolean.TRUE.equals(vlanId.isVlanIdPresent())
48                 && (vlanId.getVlanId() == null || vlanId.getVlanId().getValue().toJava() == 0);
49     }
50
51     @Override
52     protected int getOxmFieldCode() {
53         return OxmMatchConstants.VLAN_VID;
54     }
55
56     @Override
57     protected int getOxmClassCode() {
58         return OxmMatchConstants.OPENFLOW_BASIC_CLASS;
59     }
60
61     @Override
62     protected int getValueLength() {
63         return EncodeConstants.SIZE_OF_SHORT_IN_BYTES;
64     }
65 }