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