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.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()) {
75 if (entrySet.getValue() instanceof Collection) {
76 addSubject(contextAttributes, (Collection) entrySet.getValue(),
77 ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
79 addSubject(contextAttributes, Arrays.asList(entrySet.getValue().toString()),
80 ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
82 } catch (DataTypeException e) {
83 throw new XacmlApplicationException("Failed to add resource ", e);
86 mutableRequest.add(contextAttributes);
87 return mutableRequest;
90 protected static StdMutableRequestAttributes addSubject(StdMutableRequestAttributes attributes,
91 Collection<Object> values, Identifier id) throws DataTypeException {
93 DataTypeFactory factory = getDataTypeFactory();
94 if (factory == null) {
97 for (Object value : values) {
98 StdMutableAttribute mutableAttribute = new StdMutableAttribute();
99 mutableAttribute.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
100 mutableAttribute.setAttributeId(id);
101 mutableAttribute.setIncludeInResults(true);
103 DataType<?> dataTypeExtended = factory.getDataType(XACML3.ID_DATATYPE_STRING);
104 AttributeValue<?> attributeValue = dataTypeExtended.createAttributeValue(value);
105 Collection<AttributeValue<?>> attributeValues = new ArrayList<>();
106 attributeValues.add(attributeValue);
107 mutableAttribute.setValues(attributeValues);
109 attributes.add(mutableAttribute);