Update css file name in conf.py
[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.List;
26 import java.util.Map;
27
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.MatchType;
31 import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
32 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
33 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
34
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.rest.adapter.PolicyRestAdapter;
38 import org.onap.policy.rest.jpa.PolicyEntity;
39 import org.onap.portalsdk.core.controller.RestrictedBaseController;
40 import org.springframework.stereotype.Controller;
41 import org.springframework.web.bind.annotation.RequestMapping;
42
43 @Controller
44 @RequestMapping("/")
45 public class CreatePolicyController extends RestrictedBaseController {
46     private static Logger policyLogger = FlexLogger.getLogger(CreatePolicyController.class);
47     protected PolicyRestAdapter policyAdapter = null;
48     private ArrayList<Object> attributeList;
49     boolean isValidForm = false;
50
51     /**
52      * prePopulateBaseConfigPolicyData.
53      *
54      * @param policyAdapter PolicyRestAdapter
55      * @param entity PolicyEntity
56      */
57     public void prePopulateBaseConfigPolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
58         attributeList = new ArrayList<>();
59         if (! (policyAdapter.getPolicyData() instanceof PolicyType)) {
60             return;
61         }
62         Object policyData = policyAdapter.getPolicyData();
63         PolicyType policy = (PolicyType) policyData;
64         policyAdapter.setOldPolicyFileName(policyAdapter.getPolicyName());
65         policyAdapter.setConfigType(entity.getConfigurationData().getConfigType());
66         policyAdapter.setConfigBodyData(entity.getConfigurationData().getConfigBody());
67         String policyNameValue =
68                 policyAdapter.getPolicyName().substring(policyAdapter.getPolicyName().indexOf('_') + 1);
69         policyAdapter.setPolicyName(policyNameValue);
70         String description = "";
71         try {
72             description = policy.getDescription().substring(0, policy.getDescription().indexOf("@CreatedBy:"));
73         } catch (Exception e) {
74             policyLogger.error("Error while collecting the desciption tag in ActionPolicy " + policyNameValue, e);
75             description = policy.getDescription();
76         }
77         policyAdapter.setPolicyDescription(description);
78         // Get the target data under policy.
79         TargetType target = policy.getTarget();
80         //
81         // NOTE: target.getAnyOf() will NEVER return null
82         //
83         if (target != null) {
84             // Under target we have AnyOFType
85             for (AnyOfType anyOf : target.getAnyOf()) {
86                 // Under AnyOFType we have AllOFType
87                 //
88                 // NOTE: anyOf.getAllOf() will NEVER return null
89                 //
90                 int index = 0;
91                 for (AllOfType allOf : anyOf.getAllOf()) {
92                     // Under AllOFType we have Match
93                     // NOTE: allOf.getMatch() will NEVER be NULL
94                     //
95                     for (MatchType match : allOf.getMatch()) {
96                         //
97                         // Under the match we have attribute value and
98                         // attributeDesignator. So,finally down to the actual attribute.
99                         //
100                         String value = (String) match.getAttributeValue().getContent().get(0);
101                         String attributeId = match.getAttributeDesignator().getAttributeId();
102                         // First match in the target is OnapName, so set that value.
103                         policyAdapter.setupUsingAttribute(attributeId, value);
104                         // After Onap and Config it is optional to have attributes, so
105                         // check weather dynamic values or there or not.
106                         if (index >= 7) {
107                             Map<String, String> attribute = new HashMap<>();
108                             attribute.put("key", attributeId);
109                             attribute.put("value", value);
110                             attributeList.add(attribute);
111                         }
112                         index++;
113                     }
114                 }
115             }
116             policyAdapter.setAttributes(attributeList);
117         }
118         List<Object> ruleList = policy.getCombinerParametersOrRuleCombinerParametersOrVariableDefinition();
119         for (Object object : ruleList) {
120             if (object instanceof RuleType) {
121                 // get the condition data under the rule for rule Algorithms.
122                 policyAdapter.setRuleID(((RuleType) object).getRuleId());
123             }
124         }
125     }
126 }