6d86bdb42bd5acf16cbaaa985bf218c915aeaae7
[policy/xacml-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.xacml.pdp.application.optimization;
24
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;
39 import java.util.Map;
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;
45
46 public class OptimizationSubscriberRequest extends StdMatchablePolicyRequest {
47
48     @XACMLSubject(attributeId = "urn:org:onap:optimization:subscriber:name", includeInResults = true)
49     private List<String> subscriberRoles;
50
51     /**
52      * Create an instance of xacml request.
53      *
54      * @param decisionRequest Incoming DecisionRequest object
55      * @return XACML request
56      * @throws XacmlApplicationException XacmlApplicationException
57      */
58     @SuppressWarnings({"rawtypes", "unchecked"})
59     public static Request createInstance(DecisionRequest decisionRequest) throws XacmlApplicationException {
60         var request = StdMatchablePolicyRequest.createInstance(decisionRequest);
61
62         //
63         // Add in the context attributes
64         //
65         var mutableRequest = new StdMutableRequest(request);
66         var contextAttributes = new StdMutableRequestAttributes();
67         contextAttributes.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
68         //
69         // Add the context attributes
70         //
71         Map<String, Object> contexts = decisionRequest.getContext();
72         for (Entry<String, Object> entrySet : contexts.entrySet()) {
73             try {
74                 //
75                 // Should always be a collection, but in case someone changes
76                 // the class without checking this repo.
77                 //
78                 if (entrySet.getValue() instanceof Collection) {
79                     addSubject(contextAttributes, (Collection) entrySet.getValue(),
80                             ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
81                 } else {
82                     addSubject(contextAttributes, Arrays.asList(entrySet.getValue().toString()),
83                             ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
84                 }
85             } catch (DataTypeException e) {
86                 throw new XacmlApplicationException("Failed to add resource ", e);
87             }
88         }
89         mutableRequest.add(contextAttributes);
90         return mutableRequest;
91     }
92
93     protected static StdMutableRequestAttributes addSubject(StdMutableRequestAttributes attributes,
94             Collection<Object> values, Identifier id) throws DataTypeException {
95
96         var factory = getDataTypeFactory();
97         if (factory == null) {
98             return null;
99         }
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);
105
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);
111
112             attributes.add(mutableAttribute);
113         }
114         return attributes;
115     }
116 }