Merge "Fixes for sonar critical issues"
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / controller / CreatePolicyController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP 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.onap.policy.controller;
22
23
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.onap.policy.common.logging.flexlogger.FlexLogger;
31 import org.onap.policy.common.logging.flexlogger.Logger;
32 import org.onap.policy.rest.adapter.PolicyRestAdapter;
33 import org.onap.policy.rest.jpa.PolicyEntity;
34 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
35 import org.springframework.stereotype.Controller;
36 import org.springframework.web.bind.annotation.RequestMapping;
37
38 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
39 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
40 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
41 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
42 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
43 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
44 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
45 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
46
47 @Controller
48 @RequestMapping("/")
49 public class CreatePolicyController extends RestrictedBaseController{
50     private static Logger policyLogger = FlexLogger.getLogger(CreatePolicyController.class);
51         protected PolicyRestAdapter policyAdapter = null;
52         private ArrayList<Object> attributeList;
53         boolean isValidForm = false;
54
55         private String convertDate(String dateTTL, boolean portalType) {
56                 String formateDate = null;
57                 String[] date;
58                 String[] parts;
59
60                 if (portalType){
61                         parts = dateTTL.split("-");
62                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
63                 } else {
64                         date  = dateTTL.split("T");
65                         parts = date[0].split("-");
66                         formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
67                 }
68                 return formateDate;
69         }
70
71         public void prePopulateBaseConfigPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
72                 attributeList = new ArrayList<>();
73                 if (policyAdapter.getPolicyData() instanceof PolicyType) {
74                         Object policyData = policyAdapter.getPolicyData();
75                         PolicyType policy = (PolicyType) policyData;
76                         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
77                         policyAdapter.setConfigType(entity.getConfigurationData().getConfigType());
78                         policyAdapter.setConfigBodyData(entity.getConfigurationData().getConfigBody());
79                         String policyNameValue = policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf(' ') + 1);
80                         policyAdapter.setPolicyName(policyNameValue);
81                         String description = "";
82                         try{
83                                 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
84                         }catch(Exception e){
85                             policyLogger.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue ,e);
86                                 description = policy.getDescription();
87                         }
88                         policyAdapter.setPolicyDescription(description);
89                         // Get the target data under policy.
90                         TargetType target = policy.getTarget();
91                         if (target != null) {
92                                 // Under target we have AnyOFType
93                                 List<AnyOfType> anyOfList = target.getAnyOf();
94                                 if (anyOfList != null) {
95                                         Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
96                                         while (iterAnyOf.hasNext()) {
97                                                 AnyOfType anyOf = iterAnyOf.next();
98                                                 // Under AnyOFType we have AllOFType
99                                                 List<AllOfType> allOfList = anyOf.getAllOf();
100                                                 if (allOfList != null) {
101                                                         Iterator<AllOfType> iterAllOf = allOfList.iterator();
102                                                         int index = 0;
103                                                         while (iterAllOf.hasNext()) {
104                                                                 AllOfType allOf = iterAllOf.next();
105                                                                 // Under AllOFType we have Match
106                                                                 List<MatchType> matchList = allOf.getMatch();
107                                                                 if (matchList != null) {
108                                                                         Iterator<MatchType> iterMatch = matchList.iterator();
109                                                                         while (iterMatch.hasNext()) {
110                                                                                 MatchType match = iterMatch.next();
111                                                                                 //
112                                                                                 // Under the match we have attribute value and
113                                                                                 // attributeDesignator. So,finally down to the actual attribute.
114                                                                                 //
115                                                                                 AttributeValueType attributeValue = match.getAttributeValue();
116                                                                                 String value = (String) attributeValue.getContent().get(0);
117                                                                                 AttributeDesignatorType designator = match.getAttributeDesignator();
118                                                                                 String attributeId = designator.getAttributeId();
119                                                                                 // First match in the target is OnapName, so set that value.
120                                                                                 if (attributeId.equals("ONAPName")) {
121                                                                                         policyAdapter.setOnapName(value);
122                                                                                 }
123                                                                                 if (attributeId.equals("RiskType")){
124                                                                                         policyAdapter.setRiskType(value);
125                                                                                 }
126                                                                                 if (attributeId.equals("RiskLevel")){
127                                                                                         policyAdapter.setRiskLevel(value);
128                                                                                 }
129                                                                                 if (attributeId.equals("guard")){
130                                                                                         policyAdapter.setGuard(value);
131                                                                                 }
132                                                                                 if (attributeId.equals("TTLDate") && !value.contains("NA")){
133                                                                                         String newDate = convertDate(value, true);
134                                                                                         policyAdapter.setTtlDate(newDate);
135                                                                                 }
136                                                                                 if (attributeId.equals("ConfigName")){
137                                                                                         policyAdapter.setConfigName(value);
138                                                                                 }
139                                                                                 // After Onap and Config it is optional to have attributes, so
140                                                                                 // check weather dynamic values or there or not.
141                                                                                 if (index >= 7) {
142                                                                                         Map<String, String> attribute = new HashMap<>();
143                                                                                         attribute.put("key", attributeId);
144                                                                                         attribute.put("value", value);
145                                                                                         attributeList.add(attribute);
146                                                                                 }
147                                                                                 index++;
148                                                                         }
149                                                                 }
150                                                         }
151                                                 }
152                                         }
153                                 }
154
155                                 policyAdapter.setAttributes(attributeList);
156                         }
157                         List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
158                         for (Object o : ruleList) {
159                                 if (o instanceof RuleType) {
160                                         // get the condition data under the rule for rule  Algorithms.
161                                         policyAdapter.setRuleID(((RuleType) o).getRuleId());
162                                 }
163                         }
164                 }               
165         }
166 }