diff options
Diffstat (limited to 'ONAP-PDP/src/test/java/org/onap/policy/pdp/test/conformance/ConformanceScopeResolver.java')
-rw-r--r-- | ONAP-PDP/src/test/java/org/onap/policy/pdp/test/conformance/ConformanceScopeResolver.java | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/ONAP-PDP/src/test/java/org/onap/policy/pdp/test/conformance/ConformanceScopeResolver.java b/ONAP-PDP/src/test/java/org/onap/policy/pdp/test/conformance/ConformanceScopeResolver.java new file mode 100644 index 000000000..654145424 --- /dev/null +++ b/ONAP-PDP/src/test/java/org/onap/policy/pdp/test/conformance/ConformanceScopeResolver.java @@ -0,0 +1,121 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP-PDP + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.policy.pdp.test.conformance; + +import java.net.URI; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; + +import org.onap.policy.common.logging.flexlogger.FlexLogger; +import org.onap.policy.common.logging.flexlogger.Logger; + +import com.att.research.xacml.api.Attribute; +import com.att.research.xacml.api.AttributeValue; +import com.att.research.xacml.api.pdp.ScopeQualifier; +import com.att.research.xacml.api.pdp.ScopeResolver; +import com.att.research.xacml.api.pdp.ScopeResolverException; +import com.att.research.xacml.api.pdp.ScopeResolverResult; +import com.att.research.xacml.std.StdMutableAttribute; +import com.att.research.xacml.std.StdScopeResolverResult; +import com.att.research.xacml.std.StdStatus; +import com.att.research.xacml.std.StdStatusCode; +import com.att.research.xacml.std.datatypes.DataTypes; + +/** + * ConformanceScopeResolver implements {@link com.att.research.xacml.pdp.ScopeResolver} for the conformance tests + * using a fixed set of hierarchical resources defined in a map. + * + * @version $Revision$ + */ +public class ConformanceScopeResolver implements ScopeResolver { + private Logger logger = FlexLogger.getLogger(ConformanceScopeResolver.class); + private Map<URI, List<URI>> mapIdentifierToChildren = new HashMap<>(); + + public ConformanceScopeResolver() { + } + + public void add(URI identifierRoot, URI identifierChild) { + List<URI> listChildrenRoot = this.mapIdentifierToChildren.get(identifierRoot); + if (listChildrenRoot == null) { + listChildrenRoot = new ArrayList<>(); + this.mapIdentifierToChildren.put(identifierRoot, listChildrenRoot); + } + listChildrenRoot.add(identifierChild); + } + + private void addChildren(Attribute attributeResourceId, URI urnResourceIdValue, boolean bDescendants, List<Attribute> listAttributes) { + List<URI> listChildren = this.mapIdentifierToChildren.get(urnResourceIdValue); + if (listChildren != null) { + for (URI uriChild : listChildren) { + AttributeValue<URI> attributeValueURI = null; + try { + attributeValueURI = DataTypes.DT_ANYURI.createAttributeValue(uriChild); + if (attributeValueURI != null) { + listAttributes.add(new StdMutableAttribute(attributeResourceId.getCategory(), attributeResourceId.getAttributeId(), attributeValueURI, attributeResourceId.getIssuer(), attributeResourceId.getIncludeInResults())); + } + } catch (Exception ex) { + this.logger.error("Exception converting URI to an AttributeValue"); + } + if (bDescendants) { + this.addChildren(attributeResourceId, uriChild, bDescendants, listAttributes); + } + } + } + } + + private void addChildren(Attribute attributeResourceId, boolean bDescendants, List<Attribute> listAttributes) { + /* + * Iterate over the values that are URNs + */ + Iterator<AttributeValue<URI>> iterAttributeValueURNs = attributeResourceId.findValues(DataTypes.DT_ANYURI); + if (iterAttributeValueURNs != null) { + while (iterAttributeValueURNs.hasNext()) { + this.addChildren(attributeResourceId, iterAttributeValueURNs.next().getValue(), bDescendants, listAttributes); + } + } + } + + @Override + public ScopeResolverResult resolveScope(Attribute attributeResourceId, ScopeQualifier scopeQualifier) throws ScopeResolverException { + List<Attribute> listAttributes = new ArrayList<>(); + switch(scopeQualifier) { + case CHILDREN: + listAttributes.add(attributeResourceId); + this.addChildren(attributeResourceId, false, listAttributes); + break; + case DESCENDANTS: + listAttributes.add(attributeResourceId); + this.addChildren(attributeResourceId, true, listAttributes); + break; + case IMMEDIATE: + listAttributes.add(attributeResourceId); + break; + default: + this.logger.error("Unknown ScopeQualifier: " + scopeQualifier.name()); + return new StdScopeResolverResult(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "Unknown ScopeQualifier " + scopeQualifier.name())); + } + return new StdScopeResolverResult(listAttributes); + } + +} |