52f1ec0ab7aeddbfd4f4fcf9fdc492e6840b9b77
[policy/xacml-pdp.git] / applications / optimization / src / main / java / org / onap / policy / xacml / pdp / application / optimization / OptimizationPdpApplicationTranslator.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 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.Advice;
26 import com.att.research.xacml.api.AttributeAssignment;
27 import com.att.research.xacml.api.Identifier;
28 import com.att.research.xacml.api.XACML3;
29 import com.att.research.xacml.util.XACMLPolicyWriter;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collection;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
46 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
47 import org.apache.commons.lang3.StringUtils;
48 import org.onap.policy.models.decisions.concepts.DecisionResponse;
49 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
50 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
51 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
52 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
53 import org.onap.policy.pdp.xacml.application.common.std.StdMatchableTranslator;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56
57 public class OptimizationPdpApplicationTranslator extends StdMatchableTranslator {
58     private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplicationTranslator.class);
59
60     private static final String OPTIMIZATION_POLICYTYPE_SUBSCRIBER =
61             "onap.policies.optimization.service.SubscriberPolicy";
62
63     private static final String FIELD_SUBSCRIBER_ROLE = "subscriberRole";
64     private static final String FIELD_PROV_STATUS = "provStatus";
65
66     @SuppressWarnings("unchecked")
67     @Override
68     public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
69         //
70         // Have our superclass do the work - NOTE we are assuming
71         // that we are getting a PolicyType converted.
72         //
73         PolicyType policy = (PolicyType) super.convertPolicy(toscaPolicy);
74         //
75         // Check if this is the subscriber policy
76         //
77         if (OPTIMIZATION_POLICYTYPE_SUBSCRIBER.equals(toscaPolicy.getType())) {
78             //
79             // Ensure the policy has the subscriber properties
80             //
81             Map<String, Object> subscriberProperties = (Map<String, Object>) toscaPolicy.getProperties()
82                     .get("subscriberProperties");
83             if (subscriberProperties == null) {
84                 throw new ToscaPolicyConversionException("Missing subscriberProperties from subscriber policy");
85             }
86             //
87             // Add subscriber name to the target so the policy
88             // only matches for the given subscriberName.
89             //
90             addSubscriberNameIntoTarget(policy, subscriberProperties);
91             //
92             // Add subscriber advice
93             //
94             policy.setAdviceExpressions(generateSubscriberAdvice(subscriberProperties));
95             //
96             // Dump our revised policy out
97             //
98             try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
99                 XACMLPolicyWriter.writePolicyFile(os, policy);
100                 LOGGER.info("{}", os);
101             } catch (IOException e) {
102                 LOGGER.error("Failed to create byte array stream", e);
103             }
104         }
105         return policy;
106     }
107
108     @Override
109     protected void scanAdvice(Collection<Advice> advice, DecisionResponse decisionResponse) {
110         for (Advice adv : advice) {
111             if (! ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER.equals(adv.getId())) {
112                 LOGGER.warn("Unknown advice id {}", adv.getId());
113                 continue;
114             }
115             //
116             // Get the existing advice if any, we are appending to it.
117             //
118             Map<String, Object> mapAdvice = decisionResponse.getAdvice();
119             //
120             // If there's nothing, create a map
121             //
122             if (mapAdvice == null) {
123                 mapAdvice = new HashMap<>();
124             }
125             for (AttributeAssignment assignment : adv.getAttributeAssignments()) {
126                 if (ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER_ROLE.equals(assignment.getAttributeId())) {
127                     addValuesToMap(assignment.getAttributeValue().getValue(), FIELD_SUBSCRIBER_ROLE, mapAdvice);
128                 } else if (ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER_STATUS.equals(
129                         assignment.getAttributeId())) {
130                     addValuesToMap(assignment.getAttributeValue().getValue(), FIELD_PROV_STATUS, mapAdvice);
131                 }
132             }
133             if (! mapAdvice.isEmpty()) {
134                 decisionResponse.setAdvice(mapAdvice);
135             }
136         }
137     }
138
139     @SuppressWarnings("unchecked")
140     private static void addValuesToMap(Object values, String key, Map<String, Object> mapAdvice) {
141         if (values instanceof Collection) {
142             List<String> valueList = new ArrayList<>();
143             ((Collection<Object>) values).forEach(val -> valueList.add(val.toString()));
144             mapAdvice.put(key, valueList);
145         } else {
146             mapAdvice.put(key, values.toString());
147         }
148
149     }
150
151     @SuppressWarnings("unchecked")
152     private static PolicyType addSubscriberNameIntoTarget(PolicyType policy,
153             Map<String, Object> subscriberProperties) throws ToscaPolicyConversionException {
154         //
155         // Find the subscriber names
156         //
157         Object subscriberNames = subscriberProperties.get("subscriberName");
158         if (subscriberNames == null) {
159             throw new ToscaPolicyConversionException("Missing subscriberName property");
160         }
161         //
162         // Iterate through all the subscriber names
163         //
164         AnyOfType anyOf = new AnyOfType();
165         for (Object subscriberName : subscriberNames instanceof Collection ? (List<Object>) subscriberNames :
166             Arrays.asList(subscriberNames)) {
167
168             MatchType match = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
169                     XACML3.ID_FUNCTION_STRING_EQUAL,
170                     subscriberName,
171                     XACML3.ID_DATATYPE_STRING,
172                     ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME,
173                     XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
174
175             anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(match));
176         }
177         //
178         // Add to the target
179         //
180         policy.getTarget().getAnyOf().add(anyOf);
181         //
182         // Return for convenience
183         //
184         return policy;
185     }
186
187     @SuppressWarnings("unchecked")
188     private static AdviceExpressionsType generateSubscriberAdvice(Map<String, Object> subscriberProperties)
189             throws ToscaPolicyConversionException {
190         //
191         // Get the subscriber role
192         //
193         Object role = subscriberProperties.get(FIELD_SUBSCRIBER_ROLE);
194         if (role == null || StringUtils.isBlank(role.toString())) {
195             throw new ToscaPolicyConversionException("Missing subscriberRole");
196         }
197         //
198         // Create our subscriber advice expression
199         //
200         AdviceExpressionType adviceExpression = new AdviceExpressionType();
201         adviceExpression.setAppliesTo(EffectType.PERMIT);
202         adviceExpression.setAdviceId(ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER.stringValue());
203         //
204         // Add in subscriber role advice attributes
205         //
206         generateSubscriberAdviceAttributes(
207                 adviceExpression,
208                 ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER_ROLE,
209                 role instanceof Collection ? (List<Object>) role : Arrays.asList(role));
210         //
211         // Get the provision status
212         //
213         Object provision = subscriberProperties.get(FIELD_PROV_STATUS);
214         if (provision == null || StringUtils.isBlank(provision.toString())) {
215             throw new ToscaPolicyConversionException("Missing provStatus");
216         }
217         adviceExpression = generateSubscriberAdviceAttributes(
218                 adviceExpression,
219                 ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER_STATUS,
220                 role instanceof Collection ? (List<Object>) provision : Arrays.asList(role));
221         //
222         // Add it to the overall expressions
223         //
224         AdviceExpressionsType adviceExpressions = new AdviceExpressionsType();
225         adviceExpressions.getAdviceExpression().add(adviceExpression);
226         //
227         // Done return our advice expressions
228         //
229         return adviceExpressions;
230     }
231
232     private static AdviceExpressionType generateSubscriberAdviceAttributes(AdviceExpressionType adviceExpression,
233             Identifier attributeId, Collection<Object> adviceAttribute) {
234         for (Object attribute : adviceAttribute) {
235             AttributeValueType value = new AttributeValueType();
236             value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
237             value.getContent().add(attribute.toString());
238
239             AttributeAssignmentExpressionType assignment = new AttributeAssignmentExpressionType();
240             assignment.setAttributeId(attributeId.stringValue());
241             assignment.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue());
242             assignment.setExpression(new ObjectFactory().createAttributeValue(value));
243
244             adviceExpression.getAttributeAssignmentExpression().add(assignment);
245         }
246         //
247         // Return for convenience
248         //
249         return adviceExpression;
250     }
251 }