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.xacml.pdp.application.optimization;
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.Identifier;
29 import com.att.research.xacml.api.Request;
30 import com.att.research.xacml.api.XACML3;
31 import com.att.research.xacml.std.StdMutableAttribute;
32 import com.att.research.xacml.std.StdMutableRequest;
33 import com.att.research.xacml.std.StdMutableRequestAttributes;
34 import com.att.research.xacml.std.annotations.XACMLSubject;
35 import java.util.ArrayList;
36 import java.util.Arrays;
37 import java.util.Collection;
38 import java.util.List;
40 import java.util.Map.Entry;
41 import org.onap.policy.models.decisions.concepts.DecisionRequest;
42 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
43 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
44 import org.onap.policy.pdp.xacml.application.common.std.StdMatchablePolicyRequest;
46 public class OptimizationSubscriberRequest extends StdMatchablePolicyRequest {
48 @XACMLSubject(attributeId = "urn:org:onap:optimization:subscriber:name", includeInResults = true)
49 private List<String> subscriberRoles;
52 * Create an instance of xacml request.
54 * @param decisionRequest Incoming DecisionRequest object
55 * @return XACML request
56 * @throws XacmlApplicationException XacmlApplicationException
58 @SuppressWarnings({"rawtypes", "unchecked"})
59 public static Request createInstance(DecisionRequest decisionRequest) throws XacmlApplicationException {
60 var request = StdMatchablePolicyRequest.createInstance(decisionRequest);
63 // Add in the context attributes
65 var mutableRequest = new StdMutableRequest(request);
66 var contextAttributes = new StdMutableRequestAttributes();
67 contextAttributes.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
69 // Add the context attributes
71 Map<String, Object> contexts = decisionRequest.getContext();
72 for (Entry<String, Object> entrySet : contexts.entrySet()) {
75 // Should always be a collection, but in case someone changes
76 // the class without checking this repo.
78 if (entrySet.getValue() instanceof Collection) {
79 addSubject(contextAttributes, (Collection) entrySet.getValue(),
80 ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
82 addSubject(contextAttributes, Arrays.asList(entrySet.getValue().toString()),
83 ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
85 } catch (DataTypeException e) {
86 throw new XacmlApplicationException("Failed to add resource ", e);
89 mutableRequest.add(contextAttributes);
90 return mutableRequest;
93 protected static StdMutableRequestAttributes addSubject(StdMutableRequestAttributes attributes,
94 Collection<Object> values, Identifier id) throws DataTypeException {
96 var factory = getDataTypeFactory();
97 if (factory == null) {
100 for (Object value : values) {
101 var mutableAttribute = new StdMutableAttribute();
102 mutableAttribute.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
103 mutableAttribute.setAttributeId(id);
104 mutableAttribute.setIncludeInResults(true);
106 DataType<?> dataTypeExtended = factory.getDataType(XACML3.ID_DATATYPE_STRING);
107 AttributeValue<?> attributeValue = dataTypeExtended.createAttributeValue(value);
108 Collection<AttributeValue<?>> attributeValues = new ArrayList<>();
109 attributeValues.add(attributeValue);
110 mutableAttribute.setValues(attributeValues);
112 attributes.add(mutableAttribute);