0f3a03380e58f33e52bc0d4c9d447412b650f106
[policy/xacml-pdp.git] /
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.pdp.xacml.application.common.std;
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.Request;
30 import com.att.research.xacml.api.XACML3;
31 import com.att.research.xacml.std.IdentifierImpl;
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.RequestParser;
36 import com.att.research.xacml.std.annotations.XACMLAction;
37 import com.att.research.xacml.std.annotations.XACMLRequest;
38 import com.att.research.xacml.std.annotations.XACMLSubject;
39 import com.att.research.xacml.util.FactoryException;
40 import java.util.ArrayList;
41 import java.util.Arrays;
42 import java.util.Collection;
43 import java.util.Map;
44 import java.util.Map.Entry;
45 import lombok.Getter;
46 import lombok.Setter;
47 import lombok.ToString;
48 import org.onap.policy.models.decisions.concepts.DecisionRequest;
49 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
50 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53
54 @Getter
55 @Setter
56 @ToString
57 @XACMLRequest(ReturnPolicyIdList = true)
58 public class StdMatchablePolicyRequest {
59
60     private static final Logger LOGGER = LoggerFactory.getLogger(StdMatchablePolicyRequest.class);
61
62     @XACMLSubject(includeInResults = true)
63     private String onapName;
64
65     @XACMLSubject(attributeId = "urn:org:onap:onap-component", includeInResults = true)
66     private String onapComponent;
67
68     @XACMLSubject(attributeId = "urn:org:onap:onap-instance",  includeInResults = true)
69     private String onapInstance;
70
71     @XACMLAction()
72     private String action;
73
74     protected static DataTypeFactory dataTypeFactory        = null;
75
76     public StdMatchablePolicyRequest() {
77         super();
78     }
79
80     protected static synchronized DataTypeFactory getDataTypeFactory() {
81         try {
82             if (dataTypeFactory != null) {
83                 return dataTypeFactory;
84             }
85             dataTypeFactory = DataTypeFactory.newInstance();
86         } catch (FactoryException e) {
87             LOGGER.error("Can't get Data type Factory: {}", e);
88         }
89         return dataTypeFactory;
90     }
91
92     /**
93      * Parses the DecisionRequest into a MonitoringRequest.
94      *
95      * @param decisionRequest Input DecisionRequest
96      * @return Request XACML Request object
97      * @throws XacmlApplicationException Exception occurred parsing or creating request
98      */
99     @SuppressWarnings({"rawtypes", "unchecked"})
100     public static Request createInstance(DecisionRequest decisionRequest) throws XacmlApplicationException {
101         //
102         // Create our request object
103         //
104         StdMatchablePolicyRequest request = new StdMatchablePolicyRequest();
105         //
106         // Add the subject attributes
107         //
108         request.onapName = decisionRequest.getOnapName();
109         request.onapComponent = decisionRequest.getOnapComponent();
110         request.onapInstance = decisionRequest.getOnapInstance();
111         //
112         // Add the action attribute
113         //
114         request.action = decisionRequest.getAction();
115         //
116         // Parse the request - we use the annotations to create a
117         // basic XACML request.
118         //
119         Request xacmlRequest;
120         try {
121             xacmlRequest = RequestParser.parseRequest(request);
122         } catch (IllegalAccessException | DataTypeException e) {
123             throw new XacmlApplicationException("Could not parse request " + e.getLocalizedMessage());
124         }
125         //
126         // Create an object we can add to
127         //
128         StdMutableRequest mutableRequest = new StdMutableRequest(xacmlRequest);
129         StdMutableRequestAttributes resourceAttributes = new StdMutableRequestAttributes();
130         resourceAttributes.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
131         //
132         // Add the resource attributes
133         //
134         Map<String, Object> resources = decisionRequest.getResource();
135         for (Entry<String, Object> entrySet : resources.entrySet()) {
136             //
137             // Making an assumption that these fields are matchable.
138             // Its possible we may have to load the policy type model
139             // and use that to validate the fields that are matchable.
140             //
141             try {
142                 if (entrySet.getValue() instanceof Collection) {
143                     addResources(resourceAttributes, (Collection) entrySet.getValue(), entrySet.getKey());
144                 } else {
145                     addResources(resourceAttributes, Arrays.asList(entrySet.getValue().toString()), entrySet.getKey());
146                 }
147             } catch (DataTypeException e) {
148                 throw new XacmlApplicationException("Failed to add resource " + e.getLocalizedMessage());
149             }
150         }
151         mutableRequest.add(resourceAttributes);
152         return mutableRequest;
153     }
154
155     private static StdMutableRequestAttributes addResources(StdMutableRequestAttributes attributes,
156             Collection<Object> values, String id) throws DataTypeException {
157
158         DataTypeFactory factory = getDataTypeFactory();
159         if (factory == null) {
160             return null;
161         }
162         for (Object value : values) {
163             StdMutableAttribute mutableAttribute    = new StdMutableAttribute();
164             mutableAttribute.setCategory(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE);
165             mutableAttribute.setAttributeId(new IdentifierImpl(ToscaDictionary.ID_RESOURCE_MATCHABLE + id));
166             mutableAttribute.setIncludeInResults(true);
167
168             DataType<?> dataTypeExtended    = factory.getDataType(XACML3.ID_DATATYPE_STRING);
169             AttributeValue<?> attributeValue = dataTypeExtended.createAttributeValue(value);
170             Collection<AttributeValue<?>> attributeValues = new ArrayList<>();
171             attributeValues.add(attributeValue);
172             mutableAttribute.setValues(attributeValues);
173
174             attributes.add(mutableAttribute);
175         }
176         return attributes;
177     }
178
179 }