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