Add Match entry deserializers
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / protocol / deserialization / match / VlanVidEntryDeserializer.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.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.VlanMatchBuilder;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.vlan.match.fields.VlanIdBuilder;
18
19 public class VlanVidEntryDeserializer extends AbstractMatchEntryDeserializer {
20
21     @Override
22     public void deserializeEntry(ByteBuf message, MatchBuilder builder) {
23         final boolean hasMask = processHeader(message);
24         final VlanIdBuilder vlanIdBuilder = new VlanIdBuilder();
25         final int vlanVidValue = message.readUnsignedShort();
26
27         if (hasMask) {
28             message.skipBytes(EncodeConstants.SIZE_OF_SHORT_IN_BYTES); // Skip mask
29             vlanIdBuilder
30                     .setVlanId(new VlanId(0))
31                     .setVlanIdPresent(true);
32         } else {
33             final boolean vidPresent = (vlanVidValue & (1 << 12)) != 0;
34
35             vlanIdBuilder
36                     .setVlanId(new VlanId((vidPresent ? vlanVidValue & ((1 << 12) - 1) : vlanVidValue)))
37                     .setVlanIdPresent(vidPresent);
38         }
39
40         if (Objects.isNull(builder.getVlanMatch())) {
41             builder.setVlanMatch(new VlanMatchBuilder()
42                     .setVlanId(vlanIdBuilder.build())
43                     .build());
44         } else if (Objects.nonNull(builder.getVlanMatch().getVlanId())) {
45             builder.setVlanMatch(new VlanMatchBuilder(builder.getVlanMatch())
46                     .setVlanId(vlanIdBuilder.build())
47                     .build());
48         } else {
49             throwErrorOnMalformed(builder, "vlanMatch");
50         }
51     }
52
53 }