daa750717e856b11c425fbc089f75b025c86f233
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / protocol / serialization / match / MetadataEntrySerializerTest.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 static org.junit.Assert.assertArrayEquals;
12
13 import com.google.common.primitives.Longs;
14 import java.math.BigInteger;
15 import org.junit.Test;
16 import org.opendaylight.openflowjava.protocol.api.util.EncodeConstants;
17 import org.opendaylight.openflowjava.protocol.api.util.OxmMatchConstants;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.Match;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.model.match.types.rev131026.match.MetadataBuilder;
21
22 public class MetadataEntrySerializerTest extends AbstractMatchEntrySerializerTest {
23
24     @Test
25     public void testSerialize() {
26         final byte[] metadata = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7 };
27         final byte[] metadataMask = new byte[] { 30, 30, 30, 30, 30, 0, 0, 0 };
28
29
30         final Match metadataMatch = new MatchBuilder()
31                 .setMetadata(new MetadataBuilder()
32                         .setMetadata(BigInteger.valueOf(Longs.fromByteArray(metadata)))
33                         .setMetadataMask(BigInteger.valueOf(Longs.fromByteArray(metadataMask)))
34                         .build())
35                 .build();
36
37         assertMatch(metadataMatch, true, (out) -> {
38             byte[] addressBytes = new byte[8];
39             out.readBytes(addressBytes);
40             assertArrayEquals(addressBytes, metadata);
41
42             byte[] maskBytes = new byte[8];
43             out.readBytes(maskBytes);
44             assertArrayEquals(maskBytes, metadataMask);
45         });
46
47         final Match metadataMatchNoMask = new MatchBuilder()
48                 .setMetadata(new MetadataBuilder()
49                         .setMetadata(BigInteger.valueOf(Longs.fromByteArray(metadata)))
50                         .build())
51                 .build();
52
53         assertMatch(metadataMatchNoMask, false, (out) -> {
54             byte[] addressBytes = new byte[8];
55             out.readBytes(addressBytes);
56             assertArrayEquals(addressBytes, metadata);
57         });
58     }
59
60     @Override
61     protected short getLength() {
62         return EncodeConstants.SIZE_OF_LONG_IN_BYTES;
63     }
64
65     @Override
66     protected int getOxmFieldCode() {
67         return OxmMatchConstants.METADATA;
68     }
69
70     @Override
71     protected int getOxmClassCode() {
72         return OxmMatchConstants.OPENFLOW_BASIC_CLASS;
73     }
74
75 }