use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.
the class ExpressionExpander method negate.
private ExpressionNode negate(NonLeafExpressionNode nlExp) {
ExpressionNode notChild = nlExp.getChildExps().get(0);
if (notChild instanceof LeafExpressionNode) {
return nlExp;
}
NonLeafExpressionNode nlNotChild = (NonLeafExpressionNode) notChild;
if (nlNotChild.getOperator() == Operator.NOT) {
// negate the negate
return nlNotChild.getChildExps().get(0);
}
Operator negateOp = nlNotChild.getOperator() == Operator.AND ? Operator.OR : Operator.AND;
NonLeafExpressionNode newNode = new NonLeafExpressionNode(negateOp);
for (ExpressionNode expNode : nlNotChild.getChildExps()) {
NonLeafExpressionNode negateNode = new NonLeafExpressionNode(Operator.NOT);
negateNode.addChildExp(expNode.deepClone());
newNode.addChildExp(expand(negateNode));
}
return newNode;
}
use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.
the class ExpressionExpander method expandNonLeaf.
private NonLeafExpressionNode expandNonLeaf(NonLeafExpressionNode newNode, Operator outerOp) {
// Now go for the merge or expansion across brackets
List<ExpressionNode> newChildExps = newNode.getChildExps();
assert newChildExps.size() == 2;
ExpressionNode leftChild = newChildExps.get(0);
ExpressionNode rightChild = newChildExps.get(1);
if (rightChild.isSingleNode()) {
// Merge the single right node into the left side
assert leftChild instanceof NonLeafExpressionNode;
newNode = mergeChildNodes(newNode, outerOp, rightChild, (NonLeafExpressionNode) leftChild);
} else if (leftChild.isSingleNode()) {
// Merge the single left node into the right side
assert rightChild instanceof NonLeafExpressionNode;
newNode = mergeChildNodes(newNode, outerOp, leftChild, (NonLeafExpressionNode) rightChild);
} else {
// Both the child exp nodes are non single.
NonLeafExpressionNode leftChildNLE = (NonLeafExpressionNode) leftChild;
NonLeafExpressionNode rightChildNLE = (NonLeafExpressionNode) rightChild;
if (outerOp == leftChildNLE.getOperator() && outerOp == rightChildNLE.getOperator()) {
// Merge
NonLeafExpressionNode leftChildNLEClone = leftChildNLE.deepClone();
leftChildNLEClone.addChildExps(rightChildNLE.getChildExps());
newNode = leftChildNLEClone;
} else {
// (a | b) & (c & d) ...
if (outerOp == Operator.OR) {
// (a | b) | (c & d)
if (leftChildNLE.getOperator() == Operator.OR && rightChildNLE.getOperator() == Operator.AND) {
leftChildNLE.addChildExp(rightChildNLE);
newNode = leftChildNLE;
} else if (leftChildNLE.getOperator() == Operator.AND && rightChildNLE.getOperator() == Operator.OR) {
// (a & b) | (c | d)
rightChildNLE.addChildExp(leftChildNLE);
newNode = rightChildNLE;
}
// (a & b) | (c & d)
// This case no need to do any thing
} else {
// (a | b) & (c & d) => (a & c & d) | (b & c & d)
if (leftChildNLE.getOperator() == Operator.OR && rightChildNLE.getOperator() == Operator.AND) {
newNode = new NonLeafExpressionNode(Operator.OR);
for (ExpressionNode exp : leftChildNLE.getChildExps()) {
NonLeafExpressionNode rightChildNLEClone = rightChildNLE.deepClone();
rightChildNLEClone.addChildExp(exp);
newNode.addChildExp(rightChildNLEClone);
}
} else if (leftChildNLE.getOperator() == Operator.AND && rightChildNLE.getOperator() == Operator.OR) {
// (a & b) & (c | d) => (a & b & c) | (a & b & d)
newNode = new NonLeafExpressionNode(Operator.OR);
for (ExpressionNode exp : rightChildNLE.getChildExps()) {
NonLeafExpressionNode leftChildNLEClone = leftChildNLE.deepClone();
leftChildNLEClone.addChildExp(exp);
newNode.addChildExp(leftChildNLEClone);
}
} else {
// (a | b) & (c | d) => (a & c) | (a & d) | (b & c) | (b & d)
newNode = new NonLeafExpressionNode(Operator.OR);
for (ExpressionNode leftExp : leftChildNLE.getChildExps()) {
for (ExpressionNode rightExp : rightChildNLE.getChildExps()) {
NonLeafExpressionNode newChild = new NonLeafExpressionNode(Operator.AND);
newChild.addChildExp(leftExp.deepClone());
newChild.addChildExp(rightExp.deepClone());
newNode.addChildExp(newChild);
}
}
}
}
}
}
return newNode;
}
use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.
the class ExpressionExpander method mergeChildNodes.
private NonLeafExpressionNode mergeChildNodes(NonLeafExpressionNode newOuterNode, Operator outerOp, ExpressionNode lChild, NonLeafExpressionNode nlChild) {
// Merge the single right/left node into the other side
if (nlChild.getOperator() == outerOp) {
NonLeafExpressionNode leftChildNLEClone = nlChild.deepClone();
leftChildNLEClone.addChildExp(lChild);
newOuterNode = leftChildNLEClone;
} else if (outerOp == Operator.AND) {
assert nlChild.getOperator() == Operator.OR;
// outerOp is & here. We need to expand the node here
// (a | b) & c -> (a & c) | (b & c)
// OR
// c & (a | b) -> (c & a) | (c & b)
newOuterNode = new NonLeafExpressionNode(Operator.OR);
for (ExpressionNode exp : nlChild.getChildExps()) {
newOuterNode.addChildExp(new NonLeafExpressionNode(Operator.AND, exp, lChild));
}
}
return newOuterNode;
}
use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.
the class ExpressionParser method processCloseParan.
private void processCloseParan(Stack<ExpressionNode> expStack, String expS, int index) throws ParseException {
if (expStack.size() < 2) {
// in stack.
throw new ParseException();
} else {
ExpressionNode top = expStack.pop();
ExpressionNode secondTop = expStack.pop();
// any thing else
if (top == LeafExpressionNode.OPEN_PARAN_NODE || secondTop != LeafExpressionNode.OPEN_PARAN_NODE) {
throw new ParseException("Error parsing expression " + expS + " at column : " + index);
}
// (a&) is not valid.
if (top instanceof NonLeafExpressionNode) {
NonLeafExpressionNode nlTop = (NonLeafExpressionNode) top;
if ((nlTop.getOperator() == Operator.NOT && nlTop.getChildExps().size() != 1) || (nlTop.getOperator() != Operator.NOT && nlTop.getChildExps().size() != 2)) {
throw new ParseException("Error parsing expression " + expS + " at column : " + index);
}
}
// node.
if (!expStack.isEmpty()) {
ExpressionNode thirdTop = expStack.peek();
if (thirdTop instanceof NonLeafExpressionNode) {
NonLeafExpressionNode nlThirdTop = (NonLeafExpressionNode) expStack.pop();
nlThirdTop.addChildExp(top);
if (nlThirdTop.getOperator() == Operator.NOT) {
// completed NOT can be added now.
if (!expStack.isEmpty()) {
ExpressionNode fourthTop = expStack.peek();
if (fourthTop instanceof NonLeafExpressionNode) {
// Its Operator will be OR or AND
NonLeafExpressionNode nlFourthTop = (NonLeafExpressionNode) fourthTop;
assert nlFourthTop.getOperator() != Operator.NOT;
// Also for sure its number of children will be 1
assert nlFourthTop.getChildExps().size() == 1;
nlFourthTop.addChildExp(nlThirdTop);
// This case no need to add back the nlThirdTop.
return;
}
}
}
top = nlThirdTop;
}
}
expStack.push(top);
}
}
use of org.apache.hadoop.hbase.security.visibility.expression.NonLeafExpressionNode in project hbase by apache.
the class VisibilityUtils method getLabelOrdinals.
private static void getLabelOrdinals(ExpressionNode node, List<Integer> labelOrdinals, Set<Integer> auths, boolean checkAuths, VisibilityLabelOrdinalProvider ordinalProvider) throws IOException, InvalidLabelException {
if (node.isSingleNode()) {
String identifier = null;
int labelOrdinal = 0;
if (node instanceof LeafExpressionNode) {
identifier = ((LeafExpressionNode) node).getIdentifier();
if (LOG.isTraceEnabled()) {
LOG.trace("The identifier is " + identifier);
}
labelOrdinal = ordinalProvider.getLabelOrdinal(identifier);
checkAuths(auths, labelOrdinal, identifier, checkAuths);
} else {
// This is a NOT node.
LeafExpressionNode lNode = (LeafExpressionNode) ((NonLeafExpressionNode) node).getChildExps().get(0);
identifier = lNode.getIdentifier();
labelOrdinal = ordinalProvider.getLabelOrdinal(identifier);
checkAuths(auths, labelOrdinal, identifier, checkAuths);
// Store NOT node as -ve ordinal.
labelOrdinal = -1 * labelOrdinal;
}
if (labelOrdinal == 0) {
throw new InvalidLabelException("Invalid visibility label " + identifier);
}
labelOrdinals.add(labelOrdinal);
} else {
List<ExpressionNode> childExps = ((NonLeafExpressionNode) node).getChildExps();
for (ExpressionNode child : childExps) {
getLabelOrdinals(child, labelOrdinals, auths, checkAuths, ordinalProvider);
}
}
}
Aggregations