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