Add optimization subscriber request
[policy/xacml-pdp.git] / applications / optimization / src / main / java / org / onap / policy / xacml / pdp / application / optimization / OptimizationSubscriberRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.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;
40 import java.util.Map;
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;
46
47 public class OptimizationSubscriberRequest extends StdMatchablePolicyRequest {
48
49     @XACMLSubject(attributeId = "urn:org:onap:optimization:subscriber:name", includeInResults = true)
50     List<String> subscriberRoles;
51
52     /**
53      * Create an instance of xacml request.
54      *
55      * @param decisionRequest Incoming DecisionRequest object
56      * @return XACML request
57      * @throws XacmlApplicationException XacmlApplicationException
58      */
59     @SuppressWarnings({"rawtypes", "unchecked"})
60     public static Request createInstance(DecisionRequest decisionRequest) throws XacmlApplicationException {
61         Request request = StdMatchablePolicyRequest.createInstance(decisionRequest);
62
63         //
64         // Add in the context attributes
65         //
66         StdMutableRequest mutableRequest = new StdMutableRequest(request);
67         StdMutableRequestAttributes contextAttributes = new StdMutableRequestAttributes();
68         contextAttributes.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
69         //
70         // Add the context attributes
71         //
72         Map<String, Object> contexts = decisionRequest.getContext();
73         for (Entry<String, Object> entrySet : contexts.entrySet()) {
74             try {
75                 if (entrySet.getValue() instanceof Collection) {
76                     addSubject(contextAttributes, (Collection) entrySet.getValue(),
77                             ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
78                 } else {
79                     addSubject(contextAttributes, Arrays.asList(entrySet.getValue().toString()),
80                             ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME);
81                 }
82             } catch (DataTypeException e) {
83                 throw new XacmlApplicationException("Failed to add resource ", e);
84             }
85         }
86         mutableRequest.add(contextAttributes);
87         return mutableRequest;
88     }
89
90     protected static StdMutableRequestAttributes addSubject(StdMutableRequestAttributes attributes,
91             Collection<Object> values, Identifier id) throws DataTypeException {
92
93         DataTypeFactory factory = getDataTypeFactory();
94         if (factory == null) {
95             return null;
96         }
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);
102
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);
108
109             attributes.add(mutableAttribute);
110         }
111         return attributes;
112     }
113 }