Clean up instance checks and casts
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / Ipv6ExtHeaderEntrySerializer.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.openflowplugin.openflow.md.util.ByteUtil;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6ExtHeader;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
19
20 public class Ipv6ExtHeaderEntrySerializer extends AbstractMatchEntrySerializer {
21
22     @Override
23     public void serialize(Match match, ByteBuf outBuffer) {
24         super.serialize(match, outBuffer);
25         final Ipv6ExtHeader ipv6ExtHeader = ((Ipv6Match) match.getLayer3Match()).getIpv6ExtHeader();
26         outBuffer.writeShort(ipv6ExtHeader.getIpv6Exthdr());
27
28         if (getHasMask(match)) {
29             writeMask(ByteUtil.unsignedShortToBytes(
30                     ipv6ExtHeader.getIpv6ExthdrMask()),
31                     outBuffer,
32                     getValueLength());
33         }
34     }
35
36     @Override
37     public boolean matchTypeCheck(Match match) {
38         return Objects.nonNull(match.getLayer3Match())
39                 && match.getLayer3Match() instanceof Ipv6Match
40                 && Objects.nonNull(((Ipv6Match) match.getLayer3Match()).getIpv6ExtHeader());
41     }
42
43     @Override
44     protected boolean getHasMask(Match match) {
45         return Objects.nonNull(((Ipv6Match) match.getLayer3Match()).getIpv6ExtHeader().getIpv6ExthdrMask());
46     }
47
48     @Override
49     protected int getOxmFieldCode() {
50         return OxmMatchConstants.IPV6_EXTHDR;
51     }
52
53     @Override
54     protected int getOxmClassCode() {
55         return OxmMatchConstants.OPENFLOW_BASIC_CLASS;
56     }
57
58     @Override
59     protected int getValueLength() {
60         return EncodeConstants.SIZE_OF_SHORT_IN_BYTES;
61     }
62
63 }