2 * ============LICENSE_START=======================================================
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
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.onap.policy.controller;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.List;
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;
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;
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;
55 * prePopulateBaseConfigPolicyData.
57 * @param policyAdapter PolicyRestAdapter
58 * @param entity PolicyEntity
60 public void prePopulateBaseConfigPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
61 attributeList = new ArrayList<>();
62 if (! (policyAdapter.getPolicyData() instanceof PolicyType)) {
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 = "";
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();
80 policyAdapter.setPolicyDescription(description);
81 // Get the target data under policy.
82 TargetType target = policy.getTarget();
83 if (target != null && target.getAnyOf() != null) {
84 // Under target we have AnyOFType
85 List<AnyOfType> anyOfList = target.getAnyOf();
86 Iterator<AnyOfType> iterAnyOf = anyOfList.iterator();
87 while (iterAnyOf.hasNext()) {
88 AnyOfType anyOf = iterAnyOf.next();
89 // Under AnyOFType we have AllOFType
90 List<AllOfType> allOfList = anyOf.getAllOf();
91 if (allOfList == null) {
94 Iterator<AllOfType> iterAllOf = allOfList.iterator();
96 while (iterAllOf.hasNext()) {
97 AllOfType allOf = iterAllOf.next();
98 // Under AllOFType we have Match
99 List<MatchType> matchList = allOf.getMatch();
100 if (matchList == null) {
103 Iterator<MatchType> iterMatch = matchList.iterator();
104 while (iterMatch.hasNext()) {
105 MatchType match = iterMatch.next();
107 // Under the match we have attribute value and
108 // attributeDesignator. So,finally down to the actual attribute.
110 AttributeValueType attributeValue = match.getAttributeValue();
111 String value = (String) attributeValue.getContent().get(0);
112 AttributeDesignatorType designator = match.getAttributeDesignator();
113 String attributeId = designator.getAttributeId();
114 // First match in the target is OnapName, so set that value.
115 if ("ONAPName".equals(attributeId)) {
116 policyAdapter.setOnapName(value);
118 if ("RiskType".equals(attributeId)) {
119 policyAdapter.setRiskType(value);
121 if ("RiskLevel".equals(attributeId)) {
122 policyAdapter.setRiskLevel(value);
124 if ("guard".equals(attributeId)) {
125 policyAdapter.setGuard(value);
127 if ("TTLDate".equals(attributeId) && !value.contains("NA")) {
128 PolicyController controller = new PolicyController();
129 String newDate = controller.convertDate(value);
130 policyAdapter.setTtlDate(newDate);
132 if ("ConfigName".equals(attributeId)) {
133 policyAdapter.setConfigName(value);
135 // After Onap and Config it is optional to have attributes, so
136 // check weather dynamic values or there or not.
138 Map<String, String> attribute = new HashMap<>();
139 attribute.put("key", attributeId);
140 attribute.put("value", value);
141 attributeList.add(attribute);
147 policyAdapter.setAttributes(attributeList);
149 List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
150 for (Object o : ruleList) {
151 if (o instanceof RuleType) {
152 // get the condition data under the rule for rule Algorithms.
153 policyAdapter.setRuleID(((RuleType) o).getRuleId());