Policy 1707 commit to LF
[policy/engine.git] / ECOMP-PDP / src / test / java / org / openecomp / policy / pdp / test / conformance / ConformanceScopeResolver.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP-PDP
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.pdp.test.conformance;
22
23 import java.net.URI;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
31 import org.openecomp.policy.common.logging.flexlogger.Logger;
32
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; 
44
45 /**
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.
48  * 
49  * @version $Revision$
50  */
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>>();
54         
55         public ConformanceScopeResolver() {
56         }
57         
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);
63                 }
64                 listChildrenRoot.add(identifierChild);
65         }
66         
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;
72                                 try {
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()));
76                                         }
77                                 } catch (Exception ex) {
78                                         this.logger.error("Exception converting URI to an AttributeValue");
79                                 }
80                                 if (bDescendants) {
81                                         this.addChildren(attributeResourceId, uriChild, bDescendants, listAttributes);
82                                 }
83                         }
84                 }
85         }
86         
87         private void addChildren(Attribute attributeResourceId, boolean bDescendants, List<Attribute> listAttributes) {
88                 /*
89                  * Iterate over the values that are URNs
90                  */
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);
95                         }
96                 }
97         }
98         
99         @Override
100         public ScopeResolverResult resolveScope(Attribute attributeResourceId, ScopeQualifier scopeQualifier) throws ScopeResolverException {
101                 List<Attribute> listAttributes  = new ArrayList<Attribute>();
102                 switch(scopeQualifier) {
103                 case CHILDREN:
104                         listAttributes.add(attributeResourceId);
105                         this.addChildren(attributeResourceId, false, listAttributes);
106                         break;
107                 case DESCENDANTS:
108                         listAttributes.add(attributeResourceId);
109                         this.addChildren(attributeResourceId, true, listAttributes);
110                         break;
111                 case IMMEDIATE:
112                         listAttributes.add(attributeResourceId);
113                         break;
114                 default:
115                         this.logger.error("Unknown ScopeQualifier: " + scopeQualifier.name());
116                         return new StdScopeResolverResult(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "Unknown ScopeQualifier " + scopeQualifier.name()));
117                 }
118                 return new StdScopeResolverResult(listAttributes);
119         }
120
121 }