Policy TestSuite Enabled
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / openecomp / policy / controller / CreateBRMSRawController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ECOMP Policy Engine
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.policy.controller;
22
23
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;
29 import java.util.Map;
30
31 import javax.xml.bind.JAXBElement;
32
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;
44
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;
49
50 public class CreateBRMSRawController{
51
52         private static final Logger logger = FlexLogger.getLogger(CreateBRMSRawController.class);
53
54         protected PolicyRestAdapter policyAdapter = null;
55         private ArrayList<Object> attributeList;
56
57         
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
65                         // Extensions.
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());
69                         }
70                         policyAdapter.setPolicyName(policyNameValue);
71                         String description = "";
72                         try{
73                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
74                         }catch(Exception e){
75                                 description = policy.getDescription();
76                         }
77                         policyAdapter.setPolicyDescription(description);
78                         // Set Attributes. 
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("");
94                         }
95                         policyAdapter.setBrmsDependency(dependencies);
96                     }else if(attributeAssignment.getAttributeId().startsWith("controller:")){
97                         policyAdapter.setBrmsController(attributeAssignment.getAttributeId().replace("controller:", ""));
98                                         }
99                                 }
100                                 policyAdapter.setAttributes(attributeList);
101                         }
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();
124                                                                                 //
125                                                                                 // Under the match we have attribute value and
126                                                                                 // attributeDesignator. So,finally down to the actual attribute.
127                                                                                 //
128                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
129                                                                                 String value = (String) attributeValue.getContent().get(0);
130                                                                                 AttributeDesignatorType designator = match.getAttributeDesignator();
131                                                                                 String attributeId = designator.getAttributeId();
132                                                                                 
133                                                                                 if (attributeId.equals("RiskType")){
134                                                                                         policyAdapter.setRiskType(value);
135                                                                                 }
136                                                                                 if (attributeId.equals("RiskLevel")){
137                                                                                         policyAdapter.setRiskLevel(value);
138                                                                                 }
139                                                                                 if (attributeId.equals("guard")){
140                                                                                         policyAdapter.setGuard(value);
141                                                                                 }
142                                                                                 if (attributeId.equals("TTLDate") && !value.contains("NA")){
143                                                                                         String newDate = convertDate(value, true);
144                                                                                         policyAdapter.setTtlDate(newDate);
145                                                                                 }
146                                                                         }
147                                                                 }
148                                                         }
149                                                 }
150                                         }
151                                 }
152                         }
153                 } 
154         }
155
156         private String convertDate(String dateTTL, boolean portalType) {
157                 String formateDate = null;
158                 String[] date;
159                 String[] parts;
160                 
161                 if (portalType){
162                         parts = dateTTL.split("-");
163                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
164                 } else {
165                         date  = dateTTL.split("T");
166                         parts = date[0].split("-");
167                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
168                 }
169                 return formateDate;
170         }
171 }