Fix issues reported by sonar
[policy/engine.git] / POLICY-SDK-APP / src / main / java / org / onap / policy / admin / PolicyAdapter.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.admin;
23
24 import org.onap.policy.common.logging.flexlogger.FlexLogger;
25 import org.onap.policy.common.logging.flexlogger.Logger;
26 import org.onap.policy.controller.ActionPolicyController;
27 import org.onap.policy.controller.CreateBRMSParamController;
28 import org.onap.policy.controller.CreateBRMSRawController;
29 import org.onap.policy.controller.CreateClosedLoopFaultController;
30 import org.onap.policy.controller.CreateClosedLoopPMController;
31 import org.onap.policy.controller.CreateDcaeMicroServiceController;
32 import org.onap.policy.controller.CreateFirewallController;
33 import org.onap.policy.controller.CreateOptimizationController;
34 import org.onap.policy.controller.CreatePolicyController;
35 import org.onap.policy.controller.DecisionPolicyController;
36 import org.onap.policy.rest.adapter.PolicyRestAdapter;
37 import org.onap.policy.rest.jpa.PolicyEntity;
38
39 import com.att.research.xacml.util.XACMLProperties;
40
41 public class PolicyAdapter {
42
43     private static final Logger LOGGER  = FlexLogger.getLogger(PolicyAdapter.class);
44
45     public void configure(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
46         String policyNameValue = policyAdapter.getPolicyName().substring(0, policyAdapter.getPolicyName().indexOf('_'));
47         String configPolicyName = getConfigPolicyName(policyAdapter);
48         policyAdapter.setPolicyType(policyNameValue);
49
50         if (configPolicyName != null) {
51             policyAdapter.setConfigPolicyType(configPolicyName);
52         }
53
54         if("Action".equalsIgnoreCase(policyAdapter.getPolicyType())){
55             new ActionPolicyController().prePopulateActionPolicyData(policyAdapter, entity);
56         }
57         if("Decision".equalsIgnoreCase(policyAdapter.getPolicyType())){
58             new DecisionPolicyController().prePopulateDecisionPolicyData(policyAdapter, entity);
59         }
60         if("Config".equalsIgnoreCase(policyAdapter.getPolicyType())){
61             prePopulatePolicyData(policyAdapter, entity);
62         }
63     }
64
65     private String getConfigPolicyName(PolicyRestAdapter policyAdapter) {
66         String  configPolicyName = null ;
67         if(policyAdapter.getPolicyName().startsWith("Config_PM")){
68             configPolicyName = "ClosedLoop_PM";
69         }else if(policyAdapter.getPolicyName().startsWith("Config_Fault")){
70             configPolicyName = "ClosedLoop_Fault";
71         }else if(policyAdapter.getPolicyName().startsWith("Config_FW")){
72             configPolicyName = "Firewall Config";
73         }else if(policyAdapter.getPolicyName().startsWith("Config_BRMS_Raw")){
74             configPolicyName = "BRMS_Raw";
75         }else if(policyAdapter.getPolicyName().startsWith("Config_BRMS_Param")){
76             configPolicyName = "BRMS_Param";
77         }else if(policyAdapter.getPolicyName().startsWith("Config_MS")){
78             configPolicyName = "Micro Service";
79         }else if(policyAdapter.getPolicyName().startsWith("Config_OOF")){
80             configPolicyName = "Optimization";
81         }else if(policyAdapter.getPolicyName().startsWith("Action") || policyAdapter.getPolicyName().startsWith("Decision") ){
82             // No configPolicyName is applicable
83         }else{
84             configPolicyName = "Base";
85         }
86         return configPolicyName;
87     }
88
89     private void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
90         if("Base".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
91             new CreatePolicyController().prePopulateBaseConfigPolicyData(policyAdapter, entity);
92         }
93         else if("BRMS_Raw".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
94             new CreateBRMSRawController().prePopulateBRMSRawPolicyData(policyAdapter, entity);
95         }
96         else if("BRMS_Param".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
97             new CreateBRMSParamController().prePopulateBRMSParamPolicyData(policyAdapter, entity);
98         }
99         else if("ClosedLoop_Fault".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
100             new CreateClosedLoopFaultController().prePopulateClosedLoopFaultPolicyData(policyAdapter, entity);
101         }
102         else if("ClosedLoop_PM".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
103             new CreateClosedLoopPMController().prePopulateClosedLoopPMPolicyData(policyAdapter, entity);
104         }
105         else if("Micro Service".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
106             new CreateDcaeMicroServiceController().prePopulateDCAEMSPolicyData(policyAdapter, entity);
107         }
108         else if("Optimization".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
109             new CreateOptimizationController().prePopulatePolicyData(policyAdapter, entity);
110         }
111         else if("Firewall Config".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
112             new CreateFirewallController().prePopulateFWPolicyData(policyAdapter, entity);
113         }
114     }
115
116     public static PolicyAdapter getInstance() {
117         try {
118             Class<?> policyAdapter = Class.forName(XACMLProperties.getProperty("policyAdapter.impl.className", PolicyAdapter.class.getName()));
119             return (PolicyAdapter) policyAdapter.newInstance();
120         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException e) {
121             LOGGER.error("Exception Occured"+e);
122         }
123         return null;
124     }
125
126 }