Policy 1707 commit to LF
[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.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;
43
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;
48
49 public class CreateBRMSRawController{
50
51         private static final Logger logger = FlexLogger.getLogger(CreateBRMSRawController.class);
52
53         protected PolicyRestAdapter policyAdapter = null;
54         private ArrayList<Object> attributeList;
55
56         
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
64                         // Extensions.
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());
68                         }
69                         policyAdapter.setPolicyName(policyNameValue);
70                         String description = "";
71                         try{
72                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
73                         }catch(Exception e){
74                                 description = policy.getDescription();
75                         }
76                         policyAdapter.setPolicyDescription(description);
77                         // Set Attributes. 
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("");
93                         }
94                         policyAdapter.setBrmsDependency(dependencies);
95                     }else if(attributeAssignment.getAttributeId().startsWith("controller:")){
96                         policyAdapter.setBrmsController(attributeAssignment.getAttributeId().replace("controller:", ""));
97                                         }
98                                 }
99                                 policyAdapter.setAttributes(attributeList);
100                         }
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();
115                                                         int index = 0;
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 attributevalue 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
131                                                                                 if (index ==  3){
132                                                                                         policyAdapter.setRiskType(value);
133                                                                                 }
134
135                                                                                 if (index ==  4){
136                                                                                         policyAdapter.setRiskLevel(value);
137                                                                                 }
138                                                                                 
139                                                                                 if (index ==  5){
140                                                                                         policyAdapter.setGuard(value);
141                                                                                 }
142                                                                                 if (index == 6 && !value.contains("NA")){
143                                                                                         String newDate = convertDate(value, true);
144                                                                                         policyAdapter.setTtlDate(newDate);
145                                                                                 }
146
147                                                                                 index++;
148                                                                         }
149                                                                 }
150                                                         }
151                                                 }
152                                         }
153                                 }
154                         }
155                 } 
156         }
157
158         private String convertDate(String dateTTL, boolean portalType) {
159                 String formateDate = null;
160                 String[] date;
161                 String[] parts;
162                 
163                 if (portalType){
164                         parts = dateTTL.split("-");
165                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
166                 } else {
167                         date  = dateTTL.split("T");
168                         parts = date[0].split("-");
169                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
170                 }
171                 return formateDate;
172         }
173 }