All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.att.research.xacmlatt.pdp.policy.ObligationExpression Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
/*
 *
 *          Copyright (c) 2013,2019  AT&T Knowledge Ventures
 *                     SPDX-License-Identifier: MIT
 */
package com.att.research.xacmlatt.pdp.policy;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import com.att.research.xacml.api.AttributeAssignment;
import com.att.research.xacml.api.Decision;
import com.att.research.xacml.api.Identifier;
import com.att.research.xacml.api.Obligation;
import com.att.research.xacml.api.StatusCode;
import com.att.research.xacml.std.StdMutableObligation;
import com.att.research.xacml.std.StdStatusCode;
import com.att.research.xacmlatt.pdp.eval.EvaluationContext;
import com.att.research.xacmlatt.pdp.eval.EvaluationException;

/**
 * ObligationExpression extends {@link com.att.research.xacmlatt.pdp.policy.PolicyComponent} to implement the XACML
 * ObligationExpression element.
 * 
 * @author car
 * @version $Revision: 1.1 $
 */
public class ObligationExpression extends PolicyComponent {
	private Identifier							obligationId;
	private RuleEffect							ruleEffect;
	private List	attributeAssignmentExpressions;
	
	protected List getAttributeAssignmentExpressionList(boolean bNoNull) {
		if (this.attributeAssignmentExpressions == null && bNoNull) {
			this.attributeAssignmentExpressions	= new ArrayList<>();
		}
		return this.attributeAssignmentExpressions;
	}
	
	protected void clearAttributeAssignmentExpressions() {
		if (this.attributeAssignmentExpressions != null) {
			this.attributeAssignmentExpressions.clear();
		}
	}
	
	public ObligationExpression(StatusCode statusCodeIn, String statusMessageIn) {
		super(statusCodeIn, statusMessageIn);
	}

	public ObligationExpression(StatusCode statusCodeIn) {
		super(statusCodeIn);
	}

	public ObligationExpression() {
	}
	
	public Identifier getObligationId() {
		return this.obligationId;
	}
	
	public void setObligationId(Identifier identifier) {
		this.obligationId	= identifier;
	}
	
	public RuleEffect getRuleEffect() {
		return this.ruleEffect;
	}
	
	public void setRuleEffect(RuleEffect ruleEffectIn) {
		this.ruleEffect	= ruleEffectIn;
	}
	
	public Iterator getAttributeAssignmentExpressions() {
		List listAttributeAssignmentExpressions	= this.getAttributeAssignmentExpressionList(false);
		return (listAttributeAssignmentExpressions == null ? null : listAttributeAssignmentExpressions.iterator());
	}
	
	public void setAttributeAssignmentExpressions(Collection attributeAssignmentExpressionsIn) {
		this.clearAttributeAssignmentExpressions();
		if (attributeAssignmentExpressionsIn != null) {
			this.addAttributeAssignmentExpressions(attributeAssignmentExpressionsIn);
		}
	}
	
	public void addAttributeAssignmentExpression(AttributeAssignmentExpression attributeAssignmentExpression) {
		List listAttributeAssignmentExpressions	= this.getAttributeAssignmentExpressionList(true);
		listAttributeAssignmentExpressions.add(attributeAssignmentExpression);
	}
	
	public void addAttributeAssignmentExpressions(Collection attributeAssignmentExpressionsIn) {
		List listAttributeAssignmentExpressions	= this.getAttributeAssignmentExpressionList(true);
		listAttributeAssignmentExpressions.addAll(attributeAssignmentExpressionsIn);
	}
	
	/**
	 * Evaluates this ObligationExpression in the given {@link com.att.research.xacmlatt.pdp.eval.EvaluationContext}
	 * to get an {@link com.att.research.xacml.api.Obligation} to include in a PDP result.
	 * 
	 * @param evaluationContext the EvaluationContext in which to evaluate this ObligationExpression
	 * @param policyDefaults the PolicyDefaults to apply to the evaluation
	 * @return a new Obliagion from this ObligationExpression
	 * @throws EvaluationException if there is an error evaluating any of the AttributeAssignmentExpressions
	 */
	public Obligation evaluate(EvaluationContext evaluationContext, PolicyDefaults policyDefaults) throws EvaluationException {
		if (!this.validate()) {
			return null;
		}
		List listAttributeAssignments							= new ArrayList<>();
		Iterator iterAttributeAssignmentExpressions	= this.getAttributeAssignmentExpressions();
		if (iterAttributeAssignmentExpressions != null) {
			while (iterAttributeAssignmentExpressions.hasNext()) {
				AttributeAssignmentResult attributeAssignmentResult	= iterAttributeAssignmentExpressions.next().evaluate(evaluationContext, policyDefaults);
				if (attributeAssignmentResult.isOk() && attributeAssignmentResult.getNumAttributeAssignments() > 0) {
					Iterator iterAttributeAssignments	= attributeAssignmentResult.getAttributeAssignments();
					while (iterAttributeAssignments.hasNext()) {
						listAttributeAssignments.add(iterAttributeAssignments.next());
					}
				}
			}
		}
		return new StdMutableObligation(this.getObligationId(), listAttributeAssignments);
	}
	
	/**
	 * Evaluates a Collection of ObligationExpressions in the given EvaluationContext and returns
	 * a List of Obligations.
	 * 
	 * @param evaluationContext
	 * @param policyDefaults
	 * @param decision 
	 * @param listObligationExpressions
	 * @return List of Obligation objects
	 * @throws EvaluationException
	 */
	public static List evaluate(EvaluationContext evaluationContext, PolicyDefaults policyDefaults, Decision decision, Collection listObligationExpressions) throws EvaluationException {
		List listObligations	= new ArrayList<>();
		Iterator iterObligationExpressions	= listObligationExpressions.iterator();
		while (iterObligationExpressions.hasNext()) {
			ObligationExpression obligationExpression	= iterObligationExpressions.next();
			obligationExpression.validateComponent();
			if ( ! obligationExpression.isOk()) {
				throw new EvaluationException(obligationExpression.getStatusMessage());
			}
			if (decision == null || obligationExpression.getRuleEffect().getDecision().equals(decision)) {
				Obligation obligation	= obligationExpression.evaluate(evaluationContext, policyDefaults);
				if (obligation != null) {
					listObligations.add(obligation);
				}
			}
		}
		return listObligations;
	}

	@Override
	protected boolean validateComponent() {
		if (this.getObligationId() == null) {
			this.setStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "Missing ObligationId attribute");
			return false;
		} else if (this.getRuleEffect() == null) {
			this.setStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "Missing FulfillOn attribute");
			return false;
		} else {
			this.setStatus(StdStatusCode.STATUS_CODE_OK, null);
			return true;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy