2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.policy.pdp.test.conformance;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
30 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
31 import org.openecomp.policy.common.logging.flexlogger.Logger;
33 import com.att.research.xacml.api.Attribute;
34 import com.att.research.xacml.api.AttributeValue;
35 import com.att.research.xacml.api.pdp.ScopeQualifier;
36 import com.att.research.xacml.api.pdp.ScopeResolver;
37 import com.att.research.xacml.api.pdp.ScopeResolverException;
38 import com.att.research.xacml.api.pdp.ScopeResolverResult;
39 import com.att.research.xacml.std.StdMutableAttribute;
40 import com.att.research.xacml.std.StdScopeResolverResult;
41 import com.att.research.xacml.std.StdStatus;
42 import com.att.research.xacml.std.StdStatusCode;
43 import com.att.research.xacml.std.datatypes.DataTypes;
46 * ConformanceScopeResolver implements {@link com.att.research.xacml.pdp.ScopeResolver} for the conformance tests
47 * using a fixed set of hierarchical resources defined in a map.
51 public class ConformanceScopeResolver implements ScopeResolver {
52 private Logger logger = FlexLogger.getLogger(ConformanceScopeResolver.class);
53 private Map<URI, List<URI>> mapIdentifierToChildren = new HashMap<URI,List<URI>>();
55 public ConformanceScopeResolver() {
58 public void add(URI identifierRoot, URI identifierChild) {
59 List<URI> listChildrenRoot = this.mapIdentifierToChildren.get(identifierRoot);
60 if (listChildrenRoot == null) {
61 listChildrenRoot = new ArrayList<URI>();
62 this.mapIdentifierToChildren.put(identifierRoot, listChildrenRoot);
64 listChildrenRoot.add(identifierChild);
67 private void addChildren(Attribute attributeResourceId, URI urnResourceIdValue, boolean bDescendants, List<Attribute> listAttributes) {
68 List<URI> listChildren = this.mapIdentifierToChildren.get(urnResourceIdValue);
69 if (listChildren != null) {
70 for (URI uriChild : listChildren) {
71 AttributeValue<URI> attributeValueURI = null;
73 attributeValueURI = DataTypes.DT_ANYURI.createAttributeValue(uriChild);
74 if (attributeValueURI != null) {
75 listAttributes.add(new StdMutableAttribute(attributeResourceId.getCategory(), attributeResourceId.getAttributeId(), attributeValueURI, attributeResourceId.getIssuer(), attributeResourceId.getIncludeInResults()));
77 } catch (Exception ex) {
78 this.logger.error("Exception converting URI to an AttributeValue");
81 this.addChildren(attributeResourceId, uriChild, bDescendants, listAttributes);
87 private void addChildren(Attribute attributeResourceId, boolean bDescendants, List<Attribute> listAttributes) {
89 * Iterate over the values that are URNs
91 Iterator<AttributeValue<URI>> iterAttributeValueURNs = attributeResourceId.findValues(DataTypes.DT_ANYURI);
92 if (iterAttributeValueURNs != null) {
93 while (iterAttributeValueURNs.hasNext()) {
94 this.addChildren(attributeResourceId, iterAttributeValueURNs.next().getValue(), bDescendants, listAttributes);
100 public ScopeResolverResult resolveScope(Attribute attributeResourceId, ScopeQualifier scopeQualifier) throws ScopeResolverException {
101 List<Attribute> listAttributes = new ArrayList<Attribute>();
102 switch(scopeQualifier) {
104 listAttributes.add(attributeResourceId);
105 this.addChildren(attributeResourceId, false, listAttributes);
108 listAttributes.add(attributeResourceId);
109 this.addChildren(attributeResourceId, true, listAttributes);
112 listAttributes.add(attributeResourceId);
115 this.logger.error("Unknown ScopeQualifier: " + scopeQualifier.name());
116 return new StdScopeResolverResult(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "Unknown ScopeQualifier " + scopeQualifier.name()));
118 return new StdScopeResolverResult(listAttributes);