Bug 4827: MultiPathSupport utilities
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / AbstractOperandParser.java
1 /*
2  * Copyright (c) 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.protocol.bgp.flowspec;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import io.netty.buffer.ByteBuf;
12 import java.util.Set;
13
14 /**
15  * Top-level abstract class for all defined operands.
16  *
17  * @param <T> operand Type
18  */
19 abstract class AbstractOperandParser<T> {
20
21     @VisibleForTesting
22     static final String AND_BIT_VALUE = "and-bit";
23     @VisibleForTesting
24     static final String END_OF_LIST_VALUE = "end-of-list";
25
26     protected static final int OPERAND_LENGTH = 8;
27
28     protected static final int END_OF_LIST = 0;
29     protected static final int AND_BIT = 1;
30
31     protected static final int LENGTH_SHIFT = 4;
32
33     private static final int LENGTH_BITMASK = 48;
34
35     @VisibleForTesting
36     static final short parseLength(final byte op) {
37         return (short) (1 << ((op & LENGTH_BITMASK) >> LENGTH_SHIFT));
38     }
39
40     /**
41      * Creates operand from a set of operand values.
42      *
43      * @param opValues set of operand values
44      * @return specific type of operand
45      */
46     protected abstract T create(final Set<String> opValues);
47
48     /**
49      * Serializes operand to bytes.
50      *
51      * @param op operand to be serialized
52      * @param length value of the 'length' field
53      * @param buffer where the operand will be serialized to
54      */
55     protected abstract void serialize(final T op, final int length, final ByteBuf buffer);
56
57     /**
58      * Parses operand from byte value.
59      *
60      * @param op byte representation of an operand
61      * @return operand object
62      */
63     protected abstract T parse(final byte op);
64
65     /**
66      * Creates a string representation of the operand.
67      * E.g. : 'and does not match'
68      *
69      * @param op operand
70      * @param isFirst true if this operand is the first in list of operands
71      * @return String representation of the operand
72      */
73     protected abstract String toString(final T op, final boolean isFirst);
74 }