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