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