2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controller;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.HashMap;
27 import java.util.Iterator;
28 import java.util.List;
31 import javax.xml.bind.JAXBElement;
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.AllOfType;
36 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
37 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeAssignmentExpressionType;
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
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;
55 private ArrayList<Object> attributeList;
58 @SuppressWarnings("unchecked")
59 public void prePopulateBRMSRawPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
60 attributeList = new ArrayList<>();
61 if (policyAdapter.getPolicyData() instanceof PolicyType) {
62 PolicyType policy = (PolicyType) policyAdapter.getPolicyData();
63 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
64 // policy name value is the policy name without any prefix and
66 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("BRMS_Raw_") + 9);
67 if (logger.isDebugEnabled()) {
68 logger.debug("Prepopulating form data for BRMS RAW Policy selected:" + policyAdapter.getPolicyName());
70 policyAdapter.setPolicyName(policyNameValue);
71 String description = "";
73 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
75 logger.info("Not able to see the createdby in description. So, add generic description", e);
76 description = policy.getDescription();
78 policyAdapter.setPolicyDescription(description);
80 AdviceExpressionsType expressionTypes = ((RuleType)policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().get(0)).getAdviceExpressions();
81 for( AdviceExpressionType adviceExpression: expressionTypes.getAdviceExpression()){
82 for(AttributeAssignmentExpressionType attributeAssignment: adviceExpression.getAttributeAssignmentExpression()){
83 if(attributeAssignment.getAttributeId().startsWith("key:")){
84 Map<String, String> attribute = new HashMap<>();
85 String key = attributeAssignment.getAttributeId().replace("key:", "");
86 attribute.put("key", key);
87 JAXBElement<AttributeValueType> attributevalue = (JAXBElement<AttributeValueType>) attributeAssignment.getExpression();
88 String value = (String) attributevalue.getValue().getContent().get(0);
89 attribute.put("value", value);
90 attributeList.add(attribute);
91 }else if(attributeAssignment.getAttributeId().startsWith("dependencies:")){
92 ArrayList<String> dependencies = new ArrayList<>(Arrays.asList(attributeAssignment.getAttributeId().replace("dependencies:", "").split(",")));
93 if(dependencies.contains("")){
94 dependencies.remove("");
96 policyAdapter.setBrmsDependency(dependencies);
97 }else if(attributeAssignment.getAttributeId().startsWith("controller:")){
98 policyAdapter.setBrmsController(attributeAssignment.getAttributeId().replace("controller:", ""));
101 policyAdapter.setAttributes(attributeList);
103 // Get the target data under policy.
104 policyAdapter.setConfigBodyData(entity.getConfigurationData().getConfigBody());
105 TargetType target = policy.getTarget();
106 if (target != null) {
107 // Under target we have AnyOFType
108 List<AnyOfType> anyOfList = target.getAnyOf();
109 if (anyOfList != null) {
110 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
111 while (iterAnyOf.hasNext()) {
112 AnyOfType anyOf = iterAnyOf.next();
113 // Under AnyOFType we have AllOFType
114 List<AllOfType> allOfList = anyOf.getAllOf();
115 if (allOfList != null) {
116 Iterator<AllOfType> iterAllOf = allOfList.iterator();
117 while (iterAllOf.hasNext()) {
118 AllOfType allOf = iterAllOf.next();
119 // Under AllOFType we have Match
120 List<MatchType> matchList = allOf.getMatch();
121 if (matchList != null) {
122 Iterator<MatchType> iterMatch = matchList.iterator();
123 while (iterMatch.hasNext()) {
124 MatchType match = iterMatch.next();
126 // Under the match we have attribute value and
127 // attributeDesignator. So,finally down to the actual attribute.
129 AttributeValueType attributeValue = match.getAttributeValue();
130 String value = (String) attributeValue.getContent().get(0);
131 AttributeDesignatorType designator = match.getAttributeDesignator();
132 String attributeId = designator.getAttributeId();
134 if ("RiskType".equals(attributeId)){
135 policyAdapter.setRiskType(value);
137 if ("RiskLevel".equals(attributeId)){
138 policyAdapter.setRiskLevel(value);
140 if ("guard".equals(attributeId)){
141 policyAdapter.setGuard(value);
143 if ("TTLDate".equals(attributeId) && !value.contains("NA")){
144 PolicyController controller = new PolicyController();
145 String newDate = controller.convertDate(value);
146 policyAdapter.setTtlDate(newDate);