2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pdp.xacml.application.common.std;
25 import com.att.research.xacml.api.AttributeValue;
26 import com.att.research.xacml.api.DataType;
27 import com.att.research.xacml.api.DataTypeException;
28 import com.att.research.xacml.api.DataTypeFactory;
29 import com.att.research.xacml.api.Request;
30 import com.att.research.xacml.api.XACML3;
31 import com.att.research.xacml.std.IdentifierImpl;
32 import com.att.research.xacml.std.StdMutableAttribute;
33 import com.att.research.xacml.std.StdMutableRequest;
34 import com.att.research.xacml.std.StdMutableRequestAttributes;
35 import com.att.research.xacml.std.annotations.RequestParser;
36 import com.att.research.xacml.std.annotations.XACMLAction;
37 import com.att.research.xacml.std.annotations.XACMLRequest;
38 import com.att.research.xacml.std.annotations.XACMLSubject;
39 import com.att.research.xacml.util.FactoryException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collection;
44 import java.util.Map.Entry;
47 import lombok.ToString;
48 import org.onap.policy.models.decisions.concepts.DecisionRequest;
49 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
50 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
57 @XACMLRequest(ReturnPolicyIdList = true)
58 public class StdMatchablePolicyRequest {
60 private static final Logger LOGGER = LoggerFactory.getLogger(StdMatchablePolicyRequest.class);
62 public static final String POLICY_TYPE_KEY = "policy-type";
64 @XACMLSubject(includeInResults = true)
65 private String onapName;
67 @XACMLSubject(attributeId = "urn:org:onap:onap-component", includeInResults = true)
68 private String onapComponent;
70 @XACMLSubject(attributeId = "urn:org:onap:onap-instance", includeInResults = true)
71 private String onapInstance;
74 private String action;
76 protected static DataTypeFactory dataTypeFactory = null;
78 public StdMatchablePolicyRequest() {
82 protected static synchronized DataTypeFactory getDataTypeFactory() {
84 if (dataTypeFactory != null) {
85 return dataTypeFactory;
87 dataTypeFactory = DataTypeFactory.newInstance();
88 } catch (FactoryException e) {
89 LOGGER.error("Can't get Data type Factory", e);
91 return dataTypeFactory;
95 * Parses the DecisionRequest into a XAML request.
97 * @param decisionRequest Input DecisionRequest
98 * @return Request XACML Request object
99 * @throws XacmlApplicationException Exception occurred parsing or creating request
101 @SuppressWarnings({"rawtypes", "unchecked"})
102 public static Request createInstance(DecisionRequest decisionRequest) throws XacmlApplicationException {
104 // Create our request object
106 var request = new StdMatchablePolicyRequest();
108 // Add the subject attributes
110 request.onapName = decisionRequest.getOnapName();
111 request.onapComponent = decisionRequest.getOnapComponent();
112 request.onapInstance = decisionRequest.getOnapInstance();
114 // Add the action attribute
116 request.action = decisionRequest.getAction();
118 // Parse the request - we use the annotations to create a
119 // basic XACML request.
121 Request xacmlRequest;
123 xacmlRequest = RequestParser.parseRequest(request);
124 } catch (IllegalAccessException | DataTypeException e) {
125 throw new XacmlApplicationException("Could not parse request ", e);
128 // Create an object we can add to
130 var mutableRequest = new StdMutableRequest(xacmlRequest);
131 var resourceAttributes = new StdMutableRequestAttributes();
132 resourceAttributes.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
134 // Add the resource attributes
136 Map<String, Object> resources = decisionRequest.getResource();
137 for (Entry<String, Object> entrySet : resources.entrySet()) {
139 // Check for special policy-type
142 if (POLICY_TYPE_KEY.equals(entrySet.getKey())) {
143 attributeId = ToscaDictionary.ID_RESOURCE_POLICY_TYPE.stringValue();
145 attributeId = ToscaDictionary.ID_RESOURCE_MATCHABLE + entrySet.getKey();
148 // Making an assumption that these fields are matchable.
149 // Its possible we may have to load the policy type model
150 // and use that to validate the fields that are matchable.
153 if (entrySet.getValue() instanceof Collection) {
154 addResources(resourceAttributes, (Collection) entrySet.getValue(), attributeId);
156 addResources(resourceAttributes, Arrays.asList(entrySet.getValue().toString()), attributeId);
158 } catch (DataTypeException e) {
159 throw new XacmlApplicationException("Failed to add resource ", e);
162 mutableRequest.add(resourceAttributes);
163 return mutableRequest;
166 protected static StdMutableRequestAttributes addResources(StdMutableRequestAttributes attributes,
167 Collection<Object> values, String id) throws DataTypeException {
169 var factory = getDataTypeFactory();
170 if (factory == null) {
173 for (Object value : values) {
174 var mutableAttribute = new StdMutableAttribute();
175 mutableAttribute.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
176 mutableAttribute.setAttributeId(new IdentifierImpl(id));
177 mutableAttribute.setIncludeInResults(true);
179 DataType<?> dataTypeExtended = factory.getDataType(XACML3.ID_DATATYPE_STRING);
180 AttributeValue<?> attributeValue = dataTypeExtended.createAttributeValue(value);
181 Collection<AttributeValue<?>> attributeValues = new ArrayList<>();
182 attributeValues.add(attributeValue);
183 mutableAttribute.setValues(attributeValues);
185 attributes.add(mutableAttribute);