2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.protocol.bgp.flowspec;
10 import com.google.common.annotations.VisibleForTesting;
11 import io.netty.buffer.ByteBuf;
15 * Top-level abstract class for all defined operands.
17 * @param <T> operand Type
19 abstract class AbstractOperandParser<T> {
22 static final String AND_BIT_VALUE = "and-bit";
24 static final String END_OF_LIST_VALUE = "end-of-list";
26 protected static final int OPERAND_LENGTH = 8;
28 protected static final int END_OF_LIST = 0;
29 protected static final int AND_BIT = 1;
31 protected static final int LENGTH_SHIFT = 4;
33 private static final int LENGTH_BITMASK = 48;
36 static final short parseLength(final byte op) {
37 return (short) (1 << ((op & LENGTH_BITMASK) >> LENGTH_SHIFT));
41 * Creates operand from a set of operand values.
43 * @param opValues set of operand values
44 * @return specific type of operand
46 protected abstract T create(final Set<String> opValues);
49 * Serializes operand to bytes.
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
55 protected abstract void serialize(final T op, final int length, final ByteBuf buffer);
58 * Parses operand from byte value.
60 * @param op byte representation of an operand
61 * @return operand object
63 protected abstract T parse(final byte op);
66 * Creates a string representation of the operand.
67 * E.g. : 'and does not match'
70 * @param isFirst true if this operand is the first in list of operands
71 * @return String representation of the operand
73 protected abstract String toString(final T op, final boolean isFirst);