Code clean up
[bgpcep.git] / bgp / 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.rev180329.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     @Override
38     public BitmaskOperand create(final Set<String> opValues) {
39         return new BitmaskOperand(
40                 opValues.contains(AND_BIT_VALUE),
41                 opValues.contains(END_OF_LIST_VALUE),
42                 opValues.contains(MATCH_VALUE),
43                 opValues.contains(NOT_VALUE)
44         );
45     }
46
47     @Override
48     public void serialize(final BitmaskOperand op, final int length, final boolean endOfList,
49             final ByteBuf buffer) {
50         final BitArray bs = new BitArray(OPERAND_LENGTH);
51         bs.set(END_OF_LIST, endOfList);
52         bs.set(AND_BIT, op.isAndBit());
53         bs.set(MATCH, op.isMatch());
54         bs.set(NOT, op.isNot());
55         final byte len = (byte) (Integer.numberOfTrailingZeros(length) << LENGTH_SHIFT);
56         buffer.writeByte(bs.toByte() | len);
57     }
58
59     @Override
60     public BitmaskOperand parse(final byte op) {
61         final BitArray bs = BitArray.valueOf(op);
62         return new BitmaskOperand(bs.get(AND_BIT), bs.get(END_OF_LIST), bs.get(MATCH), bs.get(NOT));
63     }
64
65     @Override
66     public String toString(final BitmaskOperand op, final boolean isFirst) {
67         final StringBuilder buffer = new StringBuilder();
68         if (!op.isAndBit() && !isFirst) {
69             buffer.append("or ");
70         }
71         if (op.isAndBit()) {
72             buffer.append("and ");
73         }
74         if (op.isMatch()) {
75             buffer.append("does ");
76             if (op.isNot()) {
77                 buffer.append("not ");
78             }
79             buffer.append("match ");
80         } else if (op.isNot()) {
81             buffer.append("is not ");
82         }
83         return buffer.toString();
84     }
85 }