2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2019 Bell Canada
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.controller;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.HashMap;
28 import java.util.List;
31 import java.util.Objects;
32 import javax.xml.bind.JAXBElement;
34 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
35 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
45 import org.onap.policy.common.logging.flexlogger.FlexLogger;
46 import org.onap.policy.common.logging.flexlogger.Logger;
47 import org.onap.policy.rest.adapter.PolicyRestAdapter;
48 import org.onap.policy.rest.jpa.PolicyEntity;
50 public class CreateBRMSRawController {
52 private static final Logger logger = FlexLogger.getLogger(CreateBRMSRawController.class);
54 protected PolicyRestAdapter policyAdapter = null;
56 @SuppressWarnings("unchecked")
57 public void prePopulateBRMSRawPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
59 if (policyAdapter.getPolicyData() instanceof PolicyType) {
60 PolicyType policy = (PolicyType) policyAdapter.getPolicyData();
61 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
63 // Set PolicyAdapter name value
64 setPolicyAdapterNameValue(policyAdapter);
66 // Set PolicyAdapter description.
67 setPolicyAdapterDescription(policyAdapter, policy);
69 // Set PolicyAdapter attributes.
70 setPolicyAdapterAttributes(policyAdapter, policy);
72 // Set PolicyAdapter configBodyData
73 policyAdapter.setConfigBodyData(entity.getConfigurationData().getConfigBody());
75 // Get the target data under policy.
76 TargetType target = policy.getTarget();
80 // Under target we have AnyOFType
81 List<AnyOfType> anyOfList = target.getAnyOf();
82 if (anyOfList == null) {
86 // Set PolicyAdapter riskType, riskLevel, guard, ttlDate from match attributes
87 setPolicyAdapterMatchAttributes(policyAdapter, policy.getTarget().getAnyOf());
91 private void setPolicyAdapterMatchAttributes(PolicyRestAdapter policyAdapter, List<AnyOfType> anyOfList) {
93 //Extract nonNull list of AllOfType objs from each AnyOfType obj
94 .map(AnyOfType::getAllOf).filter(Objects::nonNull)
96 //Extract nonNull list of MatchType objs from each AllOFType obj
97 allOfList.stream().map(AllOfType::getMatch).filter(Objects::nonNull)
98 .flatMap(Collection::stream)
100 // Under the match we have attribute value and
101 // attributeDesignator. So,finally down to the actual attribute.
102 AttributeValueType attributeValue = match.getAttributeValue();
103 String value = (String) attributeValue.getContent().get(0);
104 AttributeDesignatorType designator = match.getAttributeDesignator();
105 String attributeId = designator.getAttributeId();
106 if ("RiskType".equals(attributeId)) {
107 policyAdapter.setRiskType(value);
108 } else if ("RiskLevel".equals(attributeId)) {
109 policyAdapter.setRiskLevel(value);
110 } else if ("guard".equals(attributeId)) {
111 policyAdapter.setGuard(value);
112 } else if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
113 PolicyController controller = new PolicyController();
114 String newDate = controller.convertDate(value);
115 policyAdapter.setTtlDate(newDate);
120 private void setPolicyAdapterNameValue(final PolicyRestAdapter policyAdapter) {
121 // policy name value is the policy name without any prefix and extensions.
122 String policyNameValue = policyAdapter.getPolicyName()
123 .substring(policyAdapter.getPolicyName().indexOf("BRMS_Raw_") + 9);
124 if (logger.isDebugEnabled()) {
125 logger.debug("Prepopulating form data for BRMS RAW Policy selected:" + policyAdapter.getPolicyName());
127 policyAdapter.setPolicyName(policyNameValue);
130 private void setPolicyAdapterDescription(final PolicyRestAdapter policyAdapter, final PolicyType policy) {
133 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
134 } catch (Exception e) {
135 logger.info("Not able to see the createdby in description. So, add generic description", e);
136 description = policy.getDescription();
138 policyAdapter.setPolicyDescription(description);
141 private void setPolicyAdapterAttributes(final PolicyRestAdapter policyAdapter, final PolicyType policy) {
142 ArrayList<Object> attributeList = new ArrayList<>();
143 AdviceExpressionsType expressionTypes = ((RuleType) policy
144 .getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().get(0)).getAdviceExpressions();
145 for (AdviceExpressionType adviceExpression : expressionTypes.getAdviceExpression()) {
146 for (AttributeAssignmentExpressionType attributeAssignment : adviceExpression
147 .getAttributeAssignmentExpression()) {
148 if (attributeAssignment.getAttributeId().startsWith("key:")) {
149 Map<String, String> attribute = new HashMap<>();
150 String key = attributeAssignment.getAttributeId().replace("key:", "");
151 attribute.put("key", key);
152 JAXBElement<AttributeValueType> attributeValue = (JAXBElement<AttributeValueType>) attributeAssignment
154 String value = (String) attributeValue.getValue().getContent().get(0);
155 attribute.put("value", value);
156 attributeList.add(attribute);
157 } else if (attributeAssignment.getAttributeId().startsWith("dependencies:")) {
158 ArrayList<String> dependencies = new ArrayList<>(Arrays
159 .asList(attributeAssignment.getAttributeId().replace("dependencies:", "").split(",")));
160 dependencies.remove("");
161 policyAdapter.setBrmsDependency(dependencies);
162 } else if (attributeAssignment.getAttributeId().startsWith("controller:")) {
164 .setBrmsController(attributeAssignment.getAttributeId().replace("controller:", ""));
167 policyAdapter.setAttributes(attributeList);