2  * ============LICENSE_START=======================================================
 
   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
 
  11  *      http://www.apache.org/licenses/LICENSE-2.0
 
  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.
 
  19  * SPDX-License-Identifier: Apache-2.0
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.policy.xacml.pdp.application.optimization;
 
  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;
 
  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;
 
  51 public class OptimizationPdpApplicationTranslator extends StdMatchableTranslator {
 
  52     private static final Logger LOGGER = LoggerFactory.getLogger(OptimizationPdpApplicationTranslator.class);
 
  54     private static final String OPTIMIZATION_POLICYTYPE_SUBSCRIBER =
 
  55             "onap.policies.optimization.service.SubscriberPolicy";
 
  57     @SuppressWarnings("unchecked")
 
  59     public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
 
  61         // Have our superclass do the work
 
  63         PolicyType policy = super.convertPolicy(toscaPolicy);
 
  65         // Check if this is the subscriber policy
 
  67         if (OPTIMIZATION_POLICYTYPE_SUBSCRIBER.equals(toscaPolicy.getType())) {
 
  69             // Ensure the policy has the subscriber properties
 
  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");
 
  77             // Add subscriber name to the target so the policy
 
  78             // only matches for the given subscriberName.
 
  80             addSubscriberNameIntoTarget(policy, subscriberProperties);
 
  82             // Add subscriber advice
 
  84             policy.setAdviceExpressions(generateSubscriberAdvice(toscaPolicy, subscriberProperties));
 
  86             // Dump our revised policy out
 
  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);
 
  98     @SuppressWarnings("unchecked")
 
  99     private static PolicyType addSubscriberNameIntoTarget(PolicyType policy,
 
 100             Map<String, Object> subscriberProperties) throws ToscaPolicyConversionException {
 
 102         // Find the subscriber names
 
 104         Object subscriberNames = subscriberProperties.get("subscriberName");
 
 105         if (subscriberNames == null) {
 
 106             throw new ToscaPolicyConversionException("Missing subscriberName property");
 
 109         // Iterate through all the subscriber names
 
 111         AnyOfType anyOf = new AnyOfType();
 
 112         for (Object subscriberName : subscriberNames instanceof Collection ? (List<Object>) subscriberNames :
 
 113             Arrays.asList(subscriberNames)) {
 
 115             MatchType match = ToscaPolicyTranslatorUtils.buildMatchTypeDesignator(
 
 116                     XACML3.ID_FUNCTION_STRING_EQUAL,
 
 118                     XACML3.ID_DATATYPE_STRING,
 
 119                     ToscaDictionary.ID_SUBJECT_OPTIMIZATION_SUBSCRIBER_NAME,
 
 120                     XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT);
 
 122             anyOf.getAllOf().add(ToscaPolicyTranslatorUtils.buildAllOf(match));
 
 127         policy.getTarget().getAnyOf().add(anyOf);
 
 129         // Return for convenience
 
 134     @SuppressWarnings("unchecked")
 
 135     private static AdviceExpressionsType generateSubscriberAdvice(ToscaPolicy toscaPolicy,
 
 136             Map<String, Object> subscriberProperties) throws ToscaPolicyConversionException {
 
 138         // Get the subscriber role
 
 140         Object role = subscriberProperties.get("subscriberRole");
 
 141         if (role == null || StringUtils.isBlank(role.toString())) {
 
 142             throw new ToscaPolicyConversionException("Missing subscriberRole");
 
 145         // Get the provision status
 
 148         // Our subscriber Advice expression holds all the attribute assignments
 
 150         AdviceExpressionType adviceExpression = new AdviceExpressionType();
 
 151         adviceExpression.setAppliesTo(EffectType.PERMIT);
 
 152         adviceExpression.setAdviceId(ToscaDictionary.ID_ADVICE_OPTIMIZATION_SUBSCRIBER.stringValue());
 
 154         // Add in subscriber role
 
 156         generateSubscriberRoleAdvice(adviceExpression, role instanceof Collection ? (List<Object>) role :
 
 157             Arrays.asList(role));
 
 159         AdviceExpressionsType adviceExpressions = new AdviceExpressionsType();
 
 160         adviceExpressions.getAdviceExpression().add(adviceExpression);
 
 162         return adviceExpressions;
 
 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());
 
 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));
 
 177             adviceExpression.getAttributeAssignmentExpression().add(assignment);
 
 181         // Return for convenience
 
 183         return adviceExpression;