Add coverage to a few Controllers
[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     /**
55      * prePopulateBaseConfigPolicyData.
56      *
57      * @param policyAdapter PolicyRestAdapter
58      * @param entity PolicyEntity
59      */
60     public void prePopulateBaseConfigPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
61         attributeList = new ArrayList<>();
62         if (! (policyAdapter.getPolicyData() instanceof PolicyType)) {
63             return;
64         }
65         Object policyData = policyAdapter.getPolicyData();
66         PolicyType policy = (PolicyType) policyData;
67         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
68         policyAdapter.setConfigType(entity.getConfigurationData().getConfigType());
69         policyAdapter.setConfigBodyData(entity.getConfigurationData().getConfigBody());
70         String policyNameValue =
71                 policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf('_') + 1);
72         policyAdapter.setPolicyName(policyNameValue);
73         String description = "";
74         try {
75             description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
76         } catch (Exception e) {
77             policyLogger.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue, e);
78             description = policy.getDescription();
79         }
80         policyAdapter.setPolicyDescription(description);
81         // Get the target data under policy.
82         TargetType target = policy.getTarget();
83         //
84         // NOTE: target.getAnyOf() will NEVER return null
85         //
86         if (target != null) {
87             // Under target we have AnyOFType
88             List<AnyOfType> anyOfList = target.getAnyOf();
89             Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
90             while (iterAnyOf.hasNext()) {
91                 AnyOfType anyOf = iterAnyOf.next();
92                 // Under AnyOFType we have AllOFType
93                 //
94                 // NOTE: anyOf.getAllOf() will NEVER return null
95                 //
96                 List<AllOfType> allOfList = anyOf.getAllOf();
97                 Iterator<AllOfType> iterAllOf = allOfList.iterator();
98                 int index = 0;
99                 while (iterAllOf.hasNext()) {
100                     AllOfType allOf = iterAllOf.next();
101                     // Under AllOFType we have Match
102                     // NOTE: allOf.getMatch() will NEVER be NULL
103                     //
104                     List<MatchType> matchList = allOf.getMatch();
105                     Iterator<MatchType> iterMatch = matchList.iterator();
106                     while (iterMatch.hasNext()) {
107                         MatchType match = iterMatch.next();
108                         //
109                         // Under the match we have attribute value and
110                         // attributeDesignator. So,finally down to the actual attribute.
111                         //
112                         AttributeValueType attributeValue = match.getAttributeValue();
113                         String value = (String) attributeValue.getContent().get(0);
114                         AttributeDesignatorType designator = match.getAttributeDesignator();
115                         String attributeId = designator.getAttributeId();
116                         // First match in the target is OnapName, so set that value.
117                         if ("ONAPName".equals(attributeId)) {
118                             policyAdapter.setOnapName(value);
119                         }
120                         if ("RiskType".equals(attributeId)) {
121                             policyAdapter.setRiskType(value);
122                         }
123                         if ("RiskLevel".equals(attributeId)) {
124                             policyAdapter.setRiskLevel(value);
125                         }
126                         if ("guard".equals(attributeId)) {
127                             policyAdapter.setGuard(value);
128                         }
129                         if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
130                             PolicyController controller = new PolicyController();
131                             String newDate = controller.convertDate(value);
132                             policyAdapter.setTtlDate(newDate);
133                         }
134                         if ("ConfigName".equals(attributeId)) {
135                             policyAdapter.setConfigName(value);
136                         }
137                         // After Onap and Config it is optional to have attributes, so
138                         // check weather dynamic values or there or not.
139                         if (index >= 7) {
140                             Map<String, String> attribute = new HashMap<>();
141                             attribute.put("key", attributeId);
142                             attribute.put("value", value);
143                             attributeList.add(attribute);
144                         }
145                         index++;
146                     }
147                 }
148             }
149             policyAdapter.setAttributes(attributeList);
150         }
151         List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
152         for (Object o : ruleList) {
153             if (o instanceof RuleType) {
154                 // get the condition data under the rule for rule Algorithms.
155                 policyAdapter.setRuleID(((RuleType) o).getRuleId());
156             }
157         }
158     }
159 }