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