2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 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.DataTypeFactory;
29 import com.att.research.xacml.api.Identifier;
30 import com.att.research.xacml.api.Request;
31 import com.att.research.xacml.api.XACML3;
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.XACMLSubject;
36 import java.util.ArrayList;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.List;
41 import java.util.Map.Entry;
42 import org.onap.policy.models.decisions.concepts.DecisionRequest;
43 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
44 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
45 import org.onap.policy.pdp.xacml.application.common.std.StdMatchablePolicyRequest;
47 public class OptimizationSubscriberRequest extends StdMatchablePolicyRequest {
49 @XACMLSubject(attributeId = "urn:org:onap:optimization:subscriber:name", includeInResults = true)
50 List<String> subscriberRoles;
53 * Create an instance of xacml request.
55 * @param decisionRequest Incoming DecisionRequest object
56 * @return XACML request
57 * @throws XacmlApplicationException XacmlApplicationException
59 @SuppressWarnings({"rawtypes", "unchecked"})
60 public static Request createInstance(DecisionRequest decisionRequest) throws XacmlApplicationException {
61 Request request = StdMatchablePolicyRequest.createInstance(decisionRequest);
64 // Add in the context attributes
66 StdMutableRequest mutableRequest = new StdMutableRequest(request);
67 StdMutableRequestAttributes contextAttributes = new StdMutableRequestAttributes();
68 contextAttributes.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
70 // Add the context attributes
72 Map<String, Object> contexts = decisionRequest.getContext();
73 for (Entry<String, Object> entrySet : contexts.entrySet()) {
76 // Should always be a collection, but in case someone changes
77 // the class without checking this repo.
79 if (entrySet.getValue() instanceof Collection) {
80 addSubject(contextAttributes, (Collection) entrySet.getValue(),
81 ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
83 addSubject(contextAttributes, Arrays.asList(entrySet.getValue().toString()),
84 ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
86 } catch (DataTypeException e) {
87 throw new XacmlApplicationException("Failed to add resource ", e);
90 mutableRequest.add(contextAttributes);
91 return mutableRequest;
94 protected static StdMutableRequestAttributes addSubject(StdMutableRequestAttributes attributes,
95 Collection<Object> values, Identifier id) throws DataTypeException {
97 DataTypeFactory factory = getDataTypeFactory();
98 if (factory == null) {
101 for (Object value : values) {
102 StdMutableAttribute mutableAttribute = new StdMutableAttribute();
103 mutableAttribute.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
104 mutableAttribute.setAttributeId(id);
105 mutableAttribute.setIncludeInResults(true);
107 DataType<?> dataTypeExtended = factory.getDataType(XACML3.ID_DATATYPE_STRING);
108 AttributeValue<?> attributeValue = dataTypeExtended.createAttributeValue(value);
109 Collection<AttributeValue<?>> attributeValues = new ArrayList<>();
110 attributeValues.add(attributeValue);
111 mutableAttribute.setValues(attributeValues);
113 attributes.add(mutableAttribute);