2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
56 @XACMLRequest(ReturnPolicyIdList = true)
57 public class StdMatchablePolicyRequest {
59 private static final Logger LOGGER = LoggerFactory.getLogger(StdMatchablePolicyRequest.class);
61 @XACMLSubject(includeInResults = true)
62 private String onapName;
64 @XACMLSubject(attributeId = "urn:org:onap:onap-component", includeInResults = true)
65 private String onapComponent;
67 @XACMLSubject(attributeId = "urn:org:onap:onap-instance", includeInResults = true)
68 private String onapInstance;
71 private String action;
73 public StdMatchablePolicyRequest() {
77 protected static DataTypeFactory dataTypeFactory = null;
79 protected static synchronized DataTypeFactory getDataTypeFactory() {
81 if (dataTypeFactory != null) {
82 return dataTypeFactory;
84 dataTypeFactory = DataTypeFactory.newInstance();
85 if (dataTypeFactory == null) {
86 LOGGER.error("Could not create data type factory");
88 } catch (FactoryException e) {
89 LOGGER.error("Can't get Data type Factory: {}", e);
91 return dataTypeFactory;
95 * Parses the DecisionRequest into a MonitoringRequest.
97 * @param decisionRequest Input DecisionRequest
98 * @return Request XACML Request object
99 * @throws DataTypeException DataType exception
100 * @throws IllegalAccessException Illegal access exception
102 @SuppressWarnings({"rawtypes", "unchecked"})
103 public static Request createInstance(DecisionRequest decisionRequest) throws IllegalAccessException,
106 // Create our request object
108 StdMatchablePolicyRequest request = new StdMatchablePolicyRequest();
110 // Add the subject attributes
112 request.onapName = decisionRequest.getOnapName();
113 request.onapComponent = decisionRequest.getOnapComponent();
114 request.onapInstance = decisionRequest.getOnapInstance();
116 // Add the action attribute
118 request.action = decisionRequest.getAction();
120 // Parse the request - we use the annotations to create a
121 // basic XACML request.
123 Request xacmlRequest = RequestParser.parseRequest(request);
125 // Create an object we can add to
127 StdMutableRequest mutableRequest = new StdMutableRequest(xacmlRequest);
128 StdMutableRequestAttributes resourceAttributes = new StdMutableRequestAttributes();
129 resourceAttributes.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
131 // Add the resource attributes
133 Map<String, Object> resources = decisionRequest.getResource();
134 for (Entry<String, Object> entrySet : resources.entrySet()) {
136 // Making an assumption that these fields are matchable.
137 // Its possible we may have to load the policy type model
138 // and use that to validate the fields that are matchable.
140 if (entrySet.getValue() instanceof Collection) {
141 addResources(resourceAttributes, (Collection) entrySet.getValue(), entrySet.getKey());
143 addResources(resourceAttributes, Arrays.asList(entrySet.getValue().toString()), entrySet.getKey());
146 mutableRequest.add(resourceAttributes);
147 return mutableRequest;
150 private static StdMutableRequestAttributes addResources(StdMutableRequestAttributes attributes,
151 Collection<Object> values, String id) throws DataTypeException {
153 DataTypeFactory factory = getDataTypeFactory();
154 if (factory == null) {
157 for (Object value : values) {
158 StdMutableAttribute mutableAttribute = new StdMutableAttribute();
159 mutableAttribute.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
160 mutableAttribute.setAttributeId(new IdentifierImpl(ToscaDictionary.ID_RESOURCE_MATCHABLE + id));
161 mutableAttribute.setIncludeInResults(true);
163 DataType<?> dataTypeExtended = factory.getDataType(XACML3.ID_DATATYPE_STRING);
164 AttributeValue<?> attributeValue = dataTypeExtended.createAttributeValue(value);
165 Collection<AttributeValue<?>> attributeValues = new ArrayList<>();
166 attributeValues.add(attributeValue);
167 mutableAttribute.setValues(attributeValues);
169 attributes.add(mutableAttribute);