summaryrefslogtreecommitdiffstats
path: root/applications/optimization/src/main/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationSubscriberRequest.java
blob: 263c6540dd3a321f64c809ff1de1735c94836d12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*-
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.xacml.pdp.application.optimization;

import com.att.research.xacml.api.AttributeValue;
import com.att.research.xacml.api.DataType;
import com.att.research.xacml.api.DataTypeException;
import com.att.research.xacml.api.DataTypeFactory;
import com.att.research.xacml.api.Identifier;
import com.att.research.xacml.api.Request;
import com.att.research.xacml.api.XACML3;
import com.att.research.xacml.std.StdMutableAttribute;
import com.att.research.xacml.std.StdMutableRequest;
import com.att.research.xacml.std.StdMutableRequestAttributes;
import com.att.research.xacml.std.annotations.XACMLSubject;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
import org.onap.policy.pdp.xacml.application.common.std.StdMatchablePolicyRequest;

public class OptimizationSubscriberRequest extends StdMatchablePolicyRequest {

    @XACMLSubject(attributeId = "urn:org:onap:optimization:subscriber:name", includeInResults = true)
    List<String> subscriberRoles;

    /**
     * Create an instance of xacml request.
     *
     * @param decisionRequest Incoming DecisionRequest object
     * @return XACML request
     * @throws XacmlApplicationException XacmlApplicationException
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static Request createInstance(DecisionRequest decisionRequest) throws XacmlApplicationException {
        Request request = StdMatchablePolicyRequest.createInstance(decisionRequest);

        //
        // Add in the context attributes
        //
        StdMutableRequest mutableRequest = new StdMutableRequest(request);
        StdMutableRequestAttributes contextAttributes = new StdMutableRequestAttributes();
        contextAttributes.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
        //
        // Add the context attributes
        //
        Map<String, Object> contexts = decisionRequest.getContext();
        for (Entry<String, Object> entrySet : contexts.entrySet()) {
            try {
                //
                // Should always be a collection, but in case someone changes
                // the class without checking this repo.
                //
                if (entrySet.getValue() instanceof Collection) {
                    addSubject(contextAttributes, (Collection) entrySet.getValue(),
                            ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
                } else {
                    addSubject(contextAttributes, Arrays.asList(entrySet.getValue().toString()),
                            ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
                }
            } catch (DataTypeException e) {
                throw new XacmlApplicationException("Failed to add resource ", e);
            }
        }
        mutableRequest.add(contextAttributes);
        return mutableRequest;
    }

    protected static StdMutableRequestAttributes addSubject(StdMutableRequestAttributes attributes,
            Collection<Object> values, Identifier id) throws DataTypeException {

        DataTypeFactory factory = getDataTypeFactory();
        if (factory == null) {
            return null;
        }
        for (Object value : values) {
            StdMutableAttribute mutableAttribute    = new StdMutableAttribute();
            mutableAttribute.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
            mutableAttribute.setAttributeId(id);
            mutableAttribute.setIncludeInResults(true);

            DataType<?> dataTypeExtended    = factory.getDataType(XACML3.ID_DATATYPE_STRING);
            AttributeValue<?> attributeValue = dataTypeExtended.createAttributeValue(value);
            Collection<AttributeValue<?>> attributeValues = new ArrayList<>();
            attributeValues.add(attributeValue);
            mutableAttribute.setValues(attributeValues);

            attributes.add(mutableAttribute);
        }
        return attributes;
    }
}