Clean up instance checks and casts
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / match / Ipv6FlabelEntryDeserializer.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.deserialization.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.impl.deserialization.match.OxmDeserializerHelper;
15 import org.opendaylight.openflowplugin.openflow.md.util.ByteUtil;
16 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6FlowLabel;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.ipv6.match.fields.Ipv6LabelBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6Match;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.layer._3.match.Ipv6MatchBuilder;
21
22 public class Ipv6FlabelEntryDeserializer extends AbstractMatchEntryDeserializer {
23
24     @Override
25     public void deserializeEntry(ByteBuf message, MatchBuilder builder) {
26         final boolean hasMask = processHeader(message);
27         final Ipv6LabelBuilder ipv6labelBuilder = new Ipv6LabelBuilder()
28             .setIpv6Flabel(new Ipv6FlowLabel(message.readUnsignedInt()));
29
30         if (hasMask) {
31             final byte[] mask = OxmDeserializerHelper.convertMask(message, EncodeConstants.SIZE_OF_INT_IN_BYTES);
32             ipv6labelBuilder.setFlabelMask(new Ipv6FlowLabel(ByteUtil.bytesToUnsignedInt(mask)));
33         }
34
35         if (Objects.isNull(builder.getLayer3Match())) {
36             builder.setLayer3Match(new Ipv6MatchBuilder()
37                     .setIpv6Label(ipv6labelBuilder.build())
38                     .build());
39         } else if (builder.getLayer3Match() instanceof Ipv6Match
40             && Objects.isNull(((Ipv6Match) builder.getLayer3Match()).getIpv6Label())) {
41             final Ipv6Match match = (Ipv6Match) builder.getLayer3Match();
42             builder.setLayer3Match(new Ipv6MatchBuilder(match)
43                     .setIpv6Label(ipv6labelBuilder.build())
44                     .build());
45         } else {
46             throwErrorOnMalformed(builder, "layer3Match", "ipv6Label");
47         }
48
49     }
50
51 }