2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.controller;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
30 import org.openecomp.policy.rest.adapter.PolicyRestAdapter;
31 import org.openecomp.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;
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;
47 public class CreatePolicyController extends RestrictedBaseController{
49 protected PolicyRestAdapter policyAdapter = null;
50 private ArrayList<Object> attributeList;
51 boolean isValidForm = false;
53 private String convertDate(String dateTTL, boolean portalType) {
54 String formateDate = null;
59 parts = dateTTL.split("-");
60 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0] + "T05:00:00.000Z";
62 date = dateTTL.split("T");
63 parts = date[0].split("-");
64 formateDate = parts[2] + "-" + parts[1] + "-" + parts[0];
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 = "";
81 description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
83 description = policy.getDescription();
85 policyAdapter.setPolicyDescription(description);
86 // Get the target data under policy.
87 TargetType target = policy.getTarget();
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();
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();
109 // Under the match we have attribute value and
110 // attributeDesignator. So,finally down to the actual attribute.
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 EcompName, so set that value.
117 if (attributeId.equals("ECOMPName")) {
118 policyAdapter.setEcompName(value);
120 if (attributeId.equals("RiskType")){
121 policyAdapter.setRiskType(value);
123 if (attributeId.equals("RiskLevel")){
124 policyAdapter.setRiskLevel(value);
126 if (attributeId.equals("guard")){
127 policyAdapter.setGuard(value);
129 if (attributeId.equals("TTLDate") && !value.contains("NA")){
130 String newDate = convertDate(value, true);
131 policyAdapter.setTtlDate(newDate);
133 if (attributeId.equals("ConfigName")){
134 policyAdapter.setConfigName(value);
136 // After Ecomp and Config it is optional to have attributes, so
137 // check weather dynamic values or there or not.
139 Map<String, String> attribute = new HashMap<>();
140 attribute.put("key", attributeId);
141 attribute.put("value", value);
142 attributeList.add(attribute);
152 policyAdapter.setAttributes(attributeList);
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());