Add arbitrary mask for nxm-reg
[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
9 package org.opendaylight.openflowjava.nx.codec.match;
10
11 import io.netty.buffer.ByteBuf;
12
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
24     private static final int VALUE_LENGTH = 4;
25
26     @Override
27     public MatchEntry deserialize(ByteBuf message) {
28         final MatchEntryBuilder matchEntriesBuilder = deserializeHeader(message);
29         final RegValuesBuilder regValuesBuilder = new RegValuesBuilder();
30         regValuesBuilder.setValue(message.readUnsignedInt());
31
32         if (matchEntriesBuilder.isHasMask()) {
33             regValuesBuilder.setMask(message.readUnsignedInt());
34         }
35
36         return matchEntriesBuilder
37             .setMatchEntryValue(new RegCaseValueBuilder()
38                 .setRegValues(regValuesBuilder.build())
39                 .build())
40             .build();
41     }
42
43     @Override
44     public void serialize(MatchEntry input, ByteBuf outBuffer) {
45         serializeHeader(input, outBuffer);
46         final RegCaseValue regCase = ((RegCaseValue) input.getMatchEntryValue());
47         outBuffer.writeInt(regCase.getRegValues().getValue().intValue());
48
49         if (input.isHasMask()) {
50             outBuffer.writeInt(regCase.getRegValues().getMask().intValue());
51         }
52     }
53
54     @Override
55     public int getOxmClassCode() {
56         return OxmMatchConstants.NXM_1_CLASS;
57     }
58
59     @Override
60     public int getValueLength() {
61         return VALUE_LENGTH;
62     }
63
64     @Override
65     public Class<? extends OxmClassBase> getOxmClass() {
66         return Nxm1Class.class;
67     }
68
69 }