Initial OpenECOMP policy/engine commit
[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.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;
34
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; 
46
47 /**
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.
50  * 
51  * @version $Revision$
52  */
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>>();
56         
57         public ConformanceScopeResolver() {
58         }
59         
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);
65                 }
66                 listChildrenRoot.add(identifierChild);
67         }
68         
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;
74                                 try {
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()));
78                                         }
79                                 } catch (Exception ex) {
80                                         this.logger.error("Exception converting URI to an AttributeValue");
81                                 }
82                                 if (bDescendants) {
83                                         this.addChildren(attributeResourceId, uriChild, bDescendants, listAttributes);
84                                 }
85                         }
86                 }
87         }
88         
89         private void addChildren(Attribute attributeResourceId, boolean bDescendants, List<Attribute> listAttributes) {
90                 /*
91                  * Iterate over the values that are URNs
92                  */
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);
97                         }
98                 }
99         }
100         
101         @Override
102         public ScopeResolverResult resolveScope(Attribute attributeResourceId, ScopeQualifier scopeQualifier) throws ScopeResolverException {
103                 List<Attribute> listAttributes  = new ArrayList<Attribute>();
104                 switch(scopeQualifier) {
105                 case CHILDREN:
106                         listAttributes.add(attributeResourceId);
107                         this.addChildren(attributeResourceId, false, listAttributes);
108                         break;
109                 case DESCENDANTS:
110                         listAttributes.add(attributeResourceId);
111                         this.addChildren(attributeResourceId, true, listAttributes);
112                         break;
113                 case IMMEDIATE:
114                         listAttributes.add(attributeResourceId);
115                         break;
116                 default:
117                         this.logger.error("Unknown ScopeQualifier: " + scopeQualifier.name());
118                         return new StdScopeResolverResult(new StdStatus(StdStatusCode.STATUS_CODE_SYNTAX_ERROR, "Unknown ScopeQualifier " + scopeQualifier.name()));
119                 }
120                 return new StdScopeResolverResult(listAttributes);
121         }
122
123 }