2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017, 2019 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;
30 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).forEach(allOfList ->
95 // Extract nonNull list of MatchType objs from each AllOFType obj
96 allOfList.stream().map(AllOfType::getMatch).filter(Objects::nonNull).flatMap(Collection::stream)
98 // Under the match we have attribute value and
99 // attributeDesignator. So,finally down to the actual attribute.
100 AttributeValueType attributeValue = match.getAttributeValue();
101 String value = (String) attributeValue.getContent().get(0);
102 AttributeDesignatorType designator = match.getAttributeDesignator();
103 String attributeId = designator.getAttributeId();
104 if ("RiskType".equals(attributeId)) {
105 policyAdapter.setRiskType(value);
106 } else if ("RiskLevel".equals(attributeId)) {
107 policyAdapter.setRiskLevel(value);
108 } else if ("guard".equals(attributeId)) {
109 policyAdapter.setGuard(value);
110 } else if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
111 PolicyController controller = new PolicyController();
112 String newDate = controller.convertDate(value);
113 policyAdapter.setTtlDate(newDate);
118 private void setPolicyAdapterNameValue(final PolicyRestAdapter policyAdapter) {
119 // policy name value is the policy name without any prefix and extensions.
120 String policyNameValue =
121 policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("BRMS_Raw_") + 9);
122 if (logger.isDebugEnabled()) {
123 logger.debug("Prepopulating form data for BRMS RAW Policy selected:" + policyAdapter.getPolicyName());
125 policyAdapter.setPolicyName(policyNameValue);
128 private void setPolicyAdapterDescription(final PolicyRestAdapter policyAdapter, final PolicyType policy) {
131 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
132 } catch (Exception e) {
133 logger.info("Not able to see the createdby in description. So, add generic description", e);
134 description = policy.getDescription();
136 policyAdapter.setPolicyDescription(description);
139 private void setPolicyAdapterAttributes(final PolicyRestAdapter policyAdapter, final PolicyType policy) {
140 ArrayList<Object> attributeList = new ArrayList<>();
141 AdviceExpressionsType expressionTypes =
142 ((RuleType) policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().get(0))
143 .getAdviceExpressions();
144 for (AdviceExpressionType adviceExpression : expressionTypes.getAdviceExpression()) {
145 for (AttributeAssignmentExpressionType attributeAssignment : adviceExpression
146 .getAttributeAssignmentExpression()) {
147 if (attributeAssignment.getAttributeId().startsWith("key:")) {
148 Map<String, String> attribute = new HashMap<>();
149 String key = attributeAssignment.getAttributeId().replace("key:", "");
150 attribute.put("key", key);
151 JAXBElement<AttributeValueType> attributeValue =
152 (JAXBElement<AttributeValueType>) attributeAssignment.getExpression();
153 String value = (String) attributeValue.getValue().getContent().get(0);
154 attribute.put("value", value);
155 attributeList.add(attribute);
156 } else if (attributeAssignment.getAttributeId().startsWith("dependencies:")) {
157 ArrayList<String> dependencies = new ArrayList<>(Arrays
158 .asList(attributeAssignment.getAttributeId().replace("dependencies:", "").split(",")));
159 dependencies.remove("");
160 policyAdapter.setBrmsDependency(dependencies);
161 } else if (attributeAssignment.getAttributeId().startsWith("controller:")) {
162 policyAdapter.setBrmsController(attributeAssignment.getAttributeId().replace("controller:", ""));
165 policyAdapter.setAttributes(attributeList);