7ec8f6764d284817593de940ccf9bf6488f70d13
[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.xacml.pdp.application.optimization;
24
25 import com.att.research.xacml.api.XACML3;
26 import com.att.research.xacml.util.XACMLPolicyWriter;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.Map;
33 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.EffectType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
42 import org.apache.commons.lang3.StringUtils;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
45 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
46 import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslatorUtils;
47 import org.onap.policy.pdp.xacml.application.common.std.StdMatchableTranslator;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class OptimizationPdpApplicationTranslator extends StdMatchableTranslator {
52     private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplicationTranslator.class);
53
54     private static final String OPTIMIZATION_POLICYTYPE_SUBSCRIBER =
55             "onap.policies.optimization.service.SubscriberPolicy";
56
57     @SuppressWarnings("unchecked")
58     @Override
59     public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
60         //
61         // Have our superclass do the work
62         //
63         PolicyType policy = super.convertPolicy(toscaPolicy);
64         //
65         // Check if this is the subscriber policy
66         //
67         if (OPTIMIZATION_POLICYTYPE_SUBSCRIBER.equals(toscaPolicy.getType())) {
68             //
69             // Ensure the policy has the subscriber properties
70             //
71             Map<String, Object> subscriberProperties = (Map<String, Object>) toscaPolicy.getProperties()
72                     .get("subscriberProperties");
73             if (subscriberProperties == null) {
74                 throw new ToscaPolicyConversionException("Missing subscriberProperties from subscriber policy");
75             }
76             //
77             // Add subscriber name to the target so the policy
78             // only matches for the given subscriberName.
79             //
80             addSubscriberNameIntoTarget(policy, subscriberProperties);
81             //
82             // Add subscriber advice
83             //
84             policy.setAdviceExpressions(generateSubscriberAdvice(toscaPolicy, subscriberProperties));
85             //
86             // Dump our revised policy out
87             //
88             try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
89                 XACMLPolicyWriter.writePolicyFile(os, policy);
90                 LOGGER.info("{}", os);
91             } catch (IOException e) {
92                 LOGGER.error("Failed to create byte array stream", e);
93             }
94         }
95         return policy;
96     }
97
98     @SuppressWarnings("unchecked")
99     private static PolicyType addSubscriberNameIntoTarget(PolicyType policy,
100             Map<String, Object> subscriberProperties) throws ToscaPolicyConversionException {
101         //
102         // Find the subscriber names
103         //
104         Object subscriberNames = subscriberProperties.get("subscriberName");
105         if (subscriberNames == null) {
106             throw new ToscaPolicyConversionException("Missing subscriberName property");
107         }
108         //
109         // Iterate through all the subscriber names
110         //
111         AnyOfType anyOf = new AnyOfType();
112         for (Object subscriberName : subscriberNames instanceof Collection ? (List<Object>) subscriberNames :
113             Arrays.asList(subscriberNames)) {
114
115             MatchType match = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
116                     XACML3.ID_FUNCTION_STRING_EQUAL,
117                     subscriberName,
118                     XACML3.ID_DATATYPE_STRING,
119                     ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME,
120                     XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
121
122             anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(match));
123         }
124         //
125         // Add to the target
126         //
127         policy.getTarget().getAnyOf().add(anyOf);
128         //
129         // Return for convenience
130         //
131         return policy;
132     }
133
134     @SuppressWarnings("unchecked")
135     private static AdviceExpressionsType generateSubscriberAdvice(ToscaPolicy toscaPolicy,
136             Map<String, Object> subscriberProperties) throws ToscaPolicyConversionException {
137         //
138         // Get the subscriber role
139         //
140         Object role = subscriberProperties.get("subscriberRole");
141         if (role == null || StringUtils.isBlank(role.toString())) {
142             throw new ToscaPolicyConversionException("Missing subscriberRole");
143         }
144         //
145         // Get the provision status
146         // TODO
147         //
148         // Our subscriber Advice expression holds all the attribute assignments
149         //
150         AdviceExpressionType adviceExpression = new AdviceExpressionType();
151         adviceExpression.setAppliesTo(EffectType.PERMIT);
152         adviceExpression.setAdviceId(ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER.stringValue());
153         //
154         // Add in subscriber role
155         //
156         generateSubscriberRoleAdvice(adviceExpression, role instanceof Collection ? (List<Object>) role :
157             Arrays.asList(role));
158
159         AdviceExpressionsType adviceExpressions = new AdviceExpressionsType();
160         adviceExpressions.getAdviceExpression().add(adviceExpression);
161
162         return adviceExpressions;
163     }
164
165     private static AdviceExpressionType generateSubscriberRoleAdvice(AdviceExpressionType adviceExpression,
166             Collection<Object> subscriberRoles) {
167         for (Object subscriberRole : subscriberRoles) {
168             AttributeValueType value = new AttributeValueType();
169             value.setDataType(XACML3.ID_DATATYPE_STRING.stringValue());
170             value.getContent().add(subscriberRole.toString());
171
172             AttributeAssignmentExpressionType assignment = new AttributeAssignmentExpressionType();
173             assignment.setAttributeId(ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER_ROLE.stringValue());
174             assignment.setCategory(XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue());
175             assignment.setExpression(new ObjectFactory().createAttributeValue(value));
176
177             adviceExpression.getAttributeAssignmentExpression().add(assignment);
178
179         }
180         //
181         // Return for convenience
182         //
183         return adviceExpression;
184     }
185 }