Migrate uint/ByteBuf interactions
[openflowplugin.git] / extension / openflowjava-extension-nicira / src / main / java / org / opendaylight / openflowjava / nx / codec / match / AbstractRegCodec.java
1 /*
2  * Copyright (c) 2014, 2015 Cisco Systems, Inc. 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.openflowjava.nx.codec.match;
9
10 import static org.opendaylight.yangtools.yang.common.netty.ByteBufUtils.readUint32;
11
12 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.Nxm1Class;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.OxmClassBase;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry;
17 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.ofj.nxm.nx.match.reg.grouping.RegValuesBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValue;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.oxm.container.match.entry.value.RegCaseValueBuilder;
21
22 public abstract class AbstractRegCodec extends AbstractMatchCodec {
23     private static final int VALUE_LENGTH = 4;
24
25     @Override
26     public MatchEntry deserialize(ByteBuf message) {
27         final MatchEntryBuilder matchEntriesBuilder = deserializeHeaderToBuilder(message);
28         final RegValuesBuilder regValuesBuilder = new RegValuesBuilder();
29         regValuesBuilder.setValue(readUint32(message));
30
31         if (matchEntriesBuilder.isHasMask()) {
32             regValuesBuilder.setMask(readUint32(message));
33         }
34
35         return matchEntriesBuilder
36             .setMatchEntryValue(new RegCaseValueBuilder()
37                 .setRegValues(regValuesBuilder.build())
38                 .build())
39             .build();
40     }
41
42     @Override
43     public void serialize(MatchEntry input, ByteBuf outBuffer) {
44         serializeHeader(input, outBuffer);
45         final RegCaseValue regCase = (RegCaseValue) input.getMatchEntryValue();
46         outBuffer.writeInt(regCase.getRegValues().getValue().intValue());
47
48         if (input.isHasMask()) {
49             outBuffer.writeInt(regCase.getRegValues().getMask().intValue());
50         }
51     }
52
53     @Override
54     public int getOxmClassCode() {
55         return OxmMatchConstants.NXM_1_CLASS;
56     }
57
58     @Override
59     public int getValueLength() {
60         return VALUE_LENGTH;
61     }
62
63     @Override
64     public Class<? extends OxmClassBase> getOxmClass() {
65         return Nxm1Class.class;
66     }
67 }