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.openecomp.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.AttributeValueType;
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.PolicyType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
44 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
45 import org.openecomp.policy.common.logging.flexlogger.Logger;
46 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
47 import org.openecomp.policy.rest.jpa.PolicyEntity;
49 public class CreateBRMSRawController{
51 private static final Logger logger = FlexLogger.getLogger(CreateBRMSRawController.class);
53 protected PolicyRestAdapter policyAdapter = null;
54 private ArrayList<Object> attributeList;
57 @SuppressWarnings("unchecked")
58 public void prePopulateBRMSRawPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
59 attributeList = new ArrayList<Object>();
60 if (policyAdapter.getPolicyData() instanceof PolicyType) {
61 PolicyType policy = (PolicyType) policyAdapter.getPolicyData();
62 policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
63 // policy name value is the policy name without any prefix and
65 String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf("BRMS_Raw_") + 9);
66 if (logger.isDebugEnabled()) {
67 logger.debug("Prepopulating form data for BRMS RAW Policy selected:" + policyAdapter.getPolicyName());
69 policyAdapter.setPolicyName(policyNameValue);
70 String description = "";
72 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
74 description = policy.getDescription();
76 policyAdapter.setPolicyDescription(description);
78 AdviceExpressionsType expressionTypes = ((RuleType)policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().get(0)).getAdviceExpressions();
79 for( AdviceExpressionType adviceExpression: expressionTypes.getAdviceExpression()){
80 for(AttributeAssignmentExpressionType attributeAssignment: adviceExpression.getAttributeAssignmentExpression()){
81 if(attributeAssignment.getAttributeId().startsWith("key:")){
82 Map<String, String> attribute = new HashMap<String, String>();
83 String key = attributeAssignment.getAttributeId().replace("key:", "");
84 attribute.put("key", key);
85 JAXBElement<AttributeValueType> attributevalue = (JAXBElement<AttributeValueType>) attributeAssignment.getExpression();
86 String value = (String) attributevalue.getValue().getContent().get(0);
87 attribute.put("value", value);
88 attributeList.add(attribute);
89 }else if(attributeAssignment.getAttributeId().startsWith("dependencies:")){
90 ArrayList<String> dependencies = new ArrayList<String>(Arrays.asList(attributeAssignment.getAttributeId().replace("dependencies:", "").split(",")));
91 if(dependencies.contains("")){
92 dependencies.remove("");
94 policyAdapter.setBrmsDependency(dependencies);
95 }else if(attributeAssignment.getAttributeId().startsWith("controller:")){
96 policyAdapter.setBrmsController(attributeAssignment.getAttributeId().replace("controller:", ""));
99 policyAdapter.setAttributes(attributeList);
101 // Get the target data under policy.
102 policyAdapter.setConfigBodyData(entity.getConfigurationData().getConfigBody());
103 TargetType target = policy.getTarget();
104 if (target != null) {
105 // Under target we have AnyOFType
106 List<AnyOfType> anyOfList = target.getAnyOf();
107 if (anyOfList != null) {
108 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
109 while (iterAnyOf.hasNext()) {
110 AnyOfType anyOf = iterAnyOf.next();
111 // Under AnyOFType we have AllOFType
112 List<AllOfType> allOfList = anyOf.getAllOf();
113 if (allOfList != null) {
114 Iterator<AllOfType> iterAllOf = allOfList.iterator();
116 while (iterAllOf.hasNext()) {
117 AllOfType allOf = iterAllOf.next();
118 // Under AllOFType we have Match
119 List<MatchType> matchList = allOf.getMatch();
120 if (matchList != null) {
121 Iterator<MatchType> iterMatch = matchList.iterator();
122 while (iterMatch.hasNext()) {
123 MatchType match = iterMatch.next();
125 // Under the match we have attributevalue and
126 // attributeDesignator. So,finally down to the actual attribute.
128 AttributeValueType attributeValue = match.getAttributeValue();
129 String value = (String) attributeValue.getContent().get(0);
132 policyAdapter.setRiskType(value);
136 policyAdapter.setRiskLevel(value);
140 policyAdapter.setGuard(value);
142 if (index == 6 && !value.contains("NA")){
143 String newDate = convertDate(value, true);
144 policyAdapter.setTtlDate(newDate);
158 private String convertDate(String dateTTL, boolean portalType) {
159 String formateDate = null;
164 parts = dateTTL.split("-");
165 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
167 date = dateTTL.split("T");
168 parts = date[0].split("-");
169 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];