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