Bump versions to 0.21.7-SNAPSHOT
[bgpcep.git] / bgp / extensions / flowspec / src / main / java / org / opendaylight / protocol / bgp / flowspec / handlers / BitmaskOperandParser.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.Set;
13 import org.opendaylight.protocol.util.BitArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.BitmaskOperand;
15
16 /**
17  * Parser class for BitmaskOperand.
18  */
19 public final class BitmaskOperandParser extends AbstractOperandParser<BitmaskOperand> {
20
21     public static final BitmaskOperandParser INSTANCE;
22
23     static {
24         INSTANCE = new BitmaskOperandParser();
25     }
26
27     @VisibleForTesting
28     public static final String MATCH_VALUE = "match";
29     @VisibleForTesting
30     public static final String NOT_VALUE = "not";
31
32     private static final int NOT = 6;
33     private static final int MATCH = 7;
34
35     private BitmaskOperandParser() {
36
37     }
38
39     @Override
40     public BitmaskOperand create(final Set<String> opValues) {
41         return new BitmaskOperand(
42                 opValues.contains(AND_BIT_VALUE),
43                 opValues.contains(END_OF_LIST_VALUE),
44                 opValues.contains(MATCH_VALUE),
45                 opValues.contains(NOT_VALUE)
46         );
47     }
48
49     @Override
50     public void serialize(final BitmaskOperand op, final int length, final boolean endOfList,
51             final ByteBuf buffer) {
52         final BitArray bs = new BitArray(OPERAND_LENGTH);
53         bs.set(END_OF_LIST, endOfList);
54         bs.set(AND_BIT, op.getAndBit());
55         bs.set(MATCH, op.getMatch());
56         bs.set(NOT, op.getNot());
57         final byte len = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT);
58         buffer.writeByte(bs.toByte() | len);
59     }
60
61     @Override
62     public BitmaskOperand parse(final byte op) {
63         final BitArray bs = BitArray.valueOf(op);
64         return new BitmaskOperand(bs.get(AND_BIT), bs.get(END_OF_LIST), bs.get(MATCH), bs.get(NOT));
65     }
66
67     @Override
68     public String toString(final BitmaskOperand op, final boolean isFirst) {
69         final StringBuilder buffer = new StringBuilder();
70         if (!op.getAndBit() && !isFirst) {
71             buffer.append("or ");
72         }
73         if (op.getAndBit()) {
74             buffer.append("and ");
75         }
76         if (op.getMatch()) {
77             buffer.append("does ");
78             if (op.getNot()) {
79                 buffer.append("not ");
80             }
81             buffer.append("match ");
82         } else if (op.getNot()) {
83             buffer.append("is not ");
84         }
85         return buffer.toString();
86     }
87 }