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.handlers;
10 import com.google.common.annotations.VisibleForTesting;
11 import io.netty.buffer.ByteBuf;
13 import org.opendaylight.protocol.util.BitArray;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.flowspec.rev200120.BitmaskOperand;
17 * Parser class for BitmaskOperand.
19 public final class BitmaskOperandParser extends AbstractOperandParser<BitmaskOperand> {
21 public static final BitmaskOperandParser INSTANCE;
24 INSTANCE = new BitmaskOperandParser();
28 public static final String MATCH_VALUE = "match";
30 public static final String NOT_VALUE = "not";
32 private static final int NOT = 6;
33 private static final int MATCH = 7;
35 private BitmaskOperandParser() {
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)
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);
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));
68 public String toString(final BitmaskOperand op, final boolean isFirst) {
69 final StringBuilder buffer = new StringBuilder();
70 if (!op.getAndBit() && !isFirst) {
74 buffer.append("and ");
77 buffer.append("does ");
79 buffer.append("not ");
81 buffer.append("match ");
82 } else if (op.getNot()) {
83 buffer.append("is not ");
85 return buffer.toString();