aboutsummaryrefslogtreecommitdiffstats
path: root/ECOMP-TEST/src/test/java/org/openecomp/policy/pdp/test/conformance/ConformanceScopeResolver.java
blob: 5e2b3ed0fa8bcb6de61d5c0ddef95018e866a807 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*-
 * ============LICENSE_START=======================================================
 * ECOMP-TEST
 * ================================================================================
 * 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.openecomp.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.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

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 Log logger									= LogFactory.getLog(ConformanceScopeResolver.class);
	private Map<URI, List<URI>> mapIdentifierToChildren	= new HashMap<URI,List<URI>>();
	
	public ConformanceScopeResolver() {
	}
	
	public void add(URI identifierRoot, URI identifierChild) {
		List<URI> listChildrenRoot	= this.mapIdentifierToChildren.get(identifierRoot);
		if (listChildrenRoot == null) {
			listChildrenRoot	= new ArrayList<URI>();
			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<Attribute>();
		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);
	}

}