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.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
33 import org.openecomp.policy.common.logging.flexlogger.Logger;
35 import com.att.research.xacml.api.Attribute;
36 import com.att.research.xacml.api.AttributeValue;
37 import com.att.research.xacml.api.pdp.ScopeQualifier;
38 import com.att.research.xacml.api.pdp.ScopeResolver;
39 import com.att.research.xacml.api.pdp.ScopeResolverException;
40 import com.att.research.xacml.api.pdp.ScopeResolverResult;
41 import com.att.research.xacml.std.StdMutableAttribute;
42 import com.att.research.xacml.std.StdScopeResolverResult;
43 import com.att.research.xacml.std.StdStatus;
44 import com.att.research.xacml.std.StdStatusCode;
45 import com.att.research.xacml.std.datatypes.DataTypes;
48 * ConformanceScopeResolver implements {@link com.att.research.xacml.pdp.ScopeResolver} for the conformance tests
49 * using a fixed set of hierarchical resources defined in a map.
53 public class ConformanceScopeResolver implements ScopeResolver {
54 private Logger logger = FlexLogger.getLogger(ConformanceScopeResolver.class);
55 private Map<URI, List<URI>> mapIdentifierToChildren = new HashMap<URI,List<URI>>();
57 public ConformanceScopeResolver() {
60 public void add(URI identifierRoot, URI identifierChild) {
61 List<URI> listChildrenRoot = this.mapIdentifierToChildren.get(identifierRoot);
62 if (listChildrenRoot == null) {
63 listChildrenRoot = new ArrayList<URI>();
64 this.mapIdentifierToChildren.put(identifierRoot, listChildrenRoot);
66 listChildrenRoot.add(identifierChild);
69 private void addChildren(Attribute attributeResourceId, URI urnResourceIdValue, boolean bDescendants, List<Attribute> listAttributes) {
70 List<URI> listChildren = this.mapIdentifierToChildren.get(urnResourceIdValue);
71 if (listChildren != null) {
72 for (URI uriChild : listChildren) {
73 AttributeValue<URI> attributeValueURI = null;
75 attributeValueURI = DataTypes.DT_ANYURI.createAttributeValue(uriChild);
76 if (attributeValueURI != null) {
77 listAttributes.add(new StdMutableAttribute(attributeResourceId.getCategory(), attributeResourceId.getAttributeId(), attributeValueURI, attributeResourceId.getIssuer(), attributeResourceId.getIncludeInResults()));
79 } catch (Exception ex) {
80 this.logger.error("Exception converting URI to an AttributeValue");
83 this.addChildren(attributeResourceId, uriChild, bDescendants, listAttributes);
89 private void addChildren(Attribute attributeResourceId, boolean bDescendants, List<Attribute> listAttributes) {
91 * Iterate over the values that are URNs
93 Iterator<AttributeValue<URI>> iterAttributeValueURNs = attributeResourceId.findValues(DataTypes.DT_ANYURI);
94 if (iterAttributeValueURNs != null) {
95 while (iterAttributeValueURNs.hasNext()) {
96 this.addChildren(attributeResourceId, iterAttributeValueURNs.next().getValue(), bDescendants, listAttributes);
102 public ScopeResolverResult resolveScope(Attribute attributeResourceId, ScopeQualifier scopeQualifier) throws ScopeResolverException {
103 List<Attribute> listAttributes = new ArrayList<Attribute>();
104 switch(scopeQualifier) {
106 listAttributes.add(attributeResourceId);
107 this.addChildren(attributeResourceId, false, listAttributes);
110 listAttributes.add(attributeResourceId);
111 this.addChildren(attributeResourceId, true, listAttributes);
114 listAttributes.add(attributeResourceId);
117 this.logger.error("Unknown ScopeQualifier: " + scopeQualifier.name());
118 return new StdScopeResolverResult(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "Unknown ScopeQualifier " + scopeQualifier.name()));
120 return new StdScopeResolverResult(listAttributes);