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.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.openecomp.policy.common.logging.flexlogger.FlexLogger;
46 import org.openecomp.policy.common.logging.flexlogger.Logger;
47 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
48 import org.openecomp.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<Object>();
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 description = policy.getDescription();
77 policyAdapter.setPolicyDescription(description);
79 AdviceExpressionsType expressionTypes = ((RuleType)policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition().get(0)).getAdviceExpressions();
80 for( AdviceExpressionType adviceExpression: expressionTypes.getAdviceExpression()){
81 for(AttributeAssignmentExpressionType attributeAssignment: adviceExpression.getAttributeAssignmentExpression()){
82 if(attributeAssignment.getAttributeId().startsWith("key:")){
83 Map<String, String> attribute = new HashMap<String, String>();
84 String key = attributeAssignment.getAttributeId().replace("key:", "");
85 attribute.put("key", key);
86 JAXBElement<AttributeValueType> attributevalue = (JAXBElement<AttributeValueType>) attributeAssignment.getExpression();
87 String value = (String) attributevalue.getValue().getContent().get(0);
88 attribute.put("value", value);
89 attributeList.add(attribute);
90 }else if(attributeAssignment.getAttributeId().startsWith("dependencies:")){
91 ArrayList<String> dependencies = new ArrayList<String>(Arrays.asList(attributeAssignment.getAttributeId().replace("dependencies:", "").split(",")));
92 if(dependencies.contains("")){
93 dependencies.remove("");
95 policyAdapter.setBrmsDependency(dependencies);
96 }else if(attributeAssignment.getAttributeId().startsWith("controller:")){
97 policyAdapter.setBrmsController(attributeAssignment.getAttributeId().replace("controller:", ""));
100 policyAdapter.setAttributes(attributeList);
102 // Get the target data under policy.
103 policyAdapter.setConfigBodyData(entity.getConfigurationData().getConfigBody());
104 TargetType target = policy.getTarget();
105 if (target != null) {
106 // Under target we have AnyOFType
107 List<AnyOfType> anyOfList = target.getAnyOf();
108 if (anyOfList != null) {
109 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
110 while (iterAnyOf.hasNext()) {
111 AnyOfType anyOf = iterAnyOf.next();
112 // Under AnyOFType we have AllOFType
113 List<AllOfType> allOfList = anyOf.getAllOf();
114 if (allOfList != null) {
115 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 attribute value and
126 // attributeDesignator. So,finally down to the actual attribute.
128 AttributeValueType attributeValue = match.getAttributeValue();
129 String value = (String) attributeValue.getContent().get(0);
130 AttributeDesignatorType designator = match.getAttributeDesignator();
131 String attributeId = designator.getAttributeId();
133 if (attributeId.equals("RiskType")){
134 policyAdapter.setRiskType(value);
136 if (attributeId.equals("RiskLevel")){
137 policyAdapter.setRiskLevel(value);
139 if (attributeId.equals("guard")){
140 policyAdapter.setGuard(value);
142 if (attributeId.equals("TTLDate") && !value.contains("NA")){
143 String newDate = convertDate(value, true);
144 policyAdapter.setTtlDate(newDate);
156 private String convertDate(String dateTTL, boolean portalType) {
157 String formateDate = null;
162 parts = dateTTL.split("-");
163 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
165 date = dateTTL.split("T");
166 parts = date[0].split("-");
167 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];