Bug 5503 - remove package cyclic dependency in BGP-FS
[bgpcep.git] / bgp / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / handlers / AbstractNumericOperandParser.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.handlers;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import io.netty.buffer.ByteBuf;
12 import java.util.List;
13 import java.util.Set;
14
15 import org.opendaylight.protocol.util.BitArray;
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev150807.NumericOperand;
17
18 /**
19  * Common parent class for numeric operands.
20  *
21  * @param <N> numeric operand type
22  */
23 abstract public class AbstractNumericOperandParser<N> extends AbstractOperandParser<NumericOperand> {
24
25     @VisibleForTesting
26     public static final String EQUALS_VALUE = "equals";
27     @VisibleForTesting
28     public static final String GREATER_THAN_VALUE = "greater-than";
29     @VisibleForTesting
30     public static final String LESS_THAN_VALUE = "less-than";
31
32     protected static final int LESS_THAN = 5;
33     protected static final int GREATER_THAN = 6;
34     protected static final int EQUAL = 7;
35
36     /**
37      * Serializes specific numeric operand type depending on the length field value.
38      *
39      * @param list of operands to be serialized
40      * @param nlriByteBuf where the operands will be serialized
41      */
42     protected abstract <T extends N> void serialize(final List<T> list, final ByteBuf nlriByteBuf);
43
44     protected abstract <T extends N> String toString(final List<T> list);
45
46     @Override
47     public final NumericOperand create(final Set<String> operandValues) {
48         return new NumericOperand(operandValues.contains(AND_BIT_VALUE), operandValues.contains(END_OF_LIST_VALUE), operandValues.contains(EQUALS_VALUE), operandValues.contains(GREATER_THAN_VALUE), operandValues.contains(LESS_THAN_VALUE));
49     }
50
51     @Override
52     public final void serialize(final NumericOperand operand, final int length, final ByteBuf buffer) {
53         final BitArray operandValues = new BitArray(OPERAND_LENGTH);
54         operandValues.set(END_OF_LIST, operand.isEndOfList());
55         operandValues.set(AND_BIT, operand.isAndBit());
56         operandValues.set(LESS_THAN, operand.isLessThan());
57         operandValues.set(GREATER_THAN, operand.isGreaterThan());
58         operandValues.set(EQUAL, operand.isEquals());
59         final byte byteLength = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT);
60         buffer.writeByte(operandValues.toByte() | byteLength);
61     }
62
63     @Override
64     public final NumericOperand parse(final byte operand) {
65         final BitArray operandValues = BitArray.valueOf(operand);
66         return new NumericOperand(operandValues.get(AND_BIT), operandValues.get(END_OF_LIST), operandValues.get(EQUAL), operandValues.get(GREATER_THAN), operandValues.get(LESS_THAN));
67     }
68
69     @Override
70     public String toString(final NumericOperand operand, final boolean isFirst) {
71         final StringBuilder buffer = new StringBuilder();
72         if (operand.isAndBit()) {
73             buffer.append("and ");
74         } else if (!isFirst) {
75             buffer.append("or ");
76         }
77         if (operand.isLessThan()) {
78             buffer.append("is less than ");
79             if (operand.isEquals()) {
80                 buffer.append("or equals to ");
81             }
82             return buffer.toString();
83         }
84         if (operand.isGreaterThan()) {
85             buffer.append("is greater than ");
86             if (operand.isEquals()) {
87                 buffer.append("or equals to ");
88             }
89             return buffer.toString();
90         }
91         if (operand.isEquals()) {
92             buffer.append("equals to ");
93         }
94         return buffer.toString();
95     }
96 }