FIX SONAR ISSUE NESTED TRY BLOCKS
[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  * ================================================================================
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.admin;
22
23 import org.onap.policy.common.logging.flexlogger.FlexLogger;
24 import org.onap.policy.common.logging.flexlogger.Logger;
25 import org.onap.policy.controller.ActionPolicyController;
26 import org.onap.policy.controller.CreateBRMSParamController;
27 import org.onap.policy.controller.CreateBRMSRawController;
28 import org.onap.policy.controller.CreateClosedLoopFaultController;
29 import org.onap.policy.controller.CreateClosedLoopPMController;
30 import org.onap.policy.controller.CreateDcaeMicroServiceController;
31 import org.onap.policy.controller.CreateFirewallController;
32 import org.onap.policy.controller.CreateOptimizationController;
33 import org.onap.policy.controller.CreatePolicyController;
34 import org.onap.policy.controller.DecisionPolicyController;
35 import org.onap.policy.rest.adapter.PolicyRestAdapter;
36 import org.onap.policy.rest.jpa.PolicyEntity;
37
38 import com.att.research.xacml.util.XACMLProperties;
39
40 public class PolicyAdapter {
41
42         private static final Logger LOGGER      = FlexLogger.getLogger(PolicyAdapter.class);
43
44         public void configure(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
45                 if(extendedOptions(policyAdapter, entity)){
46                         return;
47                 }
48                 String policyNameValue = policyAdapter.getPolicyName().substring(0, policyAdapter.getPolicyName().indexOf('_'));
49                 String configPolicyName = getConfigPolicyName(policyAdapter);
50                 policyAdapter.setPolicyType(policyNameValue);
51
52                 if (configPolicyName != null) {
53                         policyAdapter.setConfigPolicyType(configPolicyName);
54                 }
55
56                 if("Action".equalsIgnoreCase(policyAdapter.getPolicyType())){
57                         new ActionPolicyController().prePopulateActionPolicyData(policyAdapter, entity);
58                 }
59                 if("Decision".equalsIgnoreCase(policyAdapter.getPolicyType())){
60                         new DecisionPolicyController().prePopulateDecisionPolicyData(policyAdapter, entity);
61                 }
62                 if("Config".equalsIgnoreCase(policyAdapter.getPolicyType())){
63                         prePopulatePolicyData(policyAdapter, entity);
64                 }
65         }
66
67         private String getConfigPolicyName(PolicyRestAdapter policyAdapter) {
68                 String  configPolicyName = null ;
69                 if(policyAdapter.getPolicyName().startsWith("Config_PM")){
70                         configPolicyName = "ClosedLoop_PM";
71                 }else if(policyAdapter.getPolicyName().startsWith("Config_Fault")){
72                         configPolicyName = "ClosedLoop_Fault";
73                 }else if(policyAdapter.getPolicyName().startsWith("Config_FW")){
74                         configPolicyName = "Firewall Config";
75                 }else if(policyAdapter.getPolicyName().startsWith("Config_BRMS_Raw")){
76                         configPolicyName = "BRMS_Raw";
77                 }else if(policyAdapter.getPolicyName().startsWith("Config_BRMS_Param")){
78                         configPolicyName = "BRMS_Param";
79                 }else if(policyAdapter.getPolicyName().startsWith("Config_MS")){
80                         configPolicyName = "Micro Service";
81                 }else if(policyAdapter.getPolicyName().startsWith("Config_OOF")){
82                         configPolicyName = "Optimization";
83                 }else if(policyAdapter.getPolicyName().startsWith("Action") || policyAdapter.getPolicyName().startsWith("Decision") ){
84                         // No configPolicyName is applicable
85                 }else{
86                         configPolicyName = "Base";
87                 }
88                 return configPolicyName;
89         }
90
91         private void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
92                 if("Base".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
93                         new CreatePolicyController().prePopulateBaseConfigPolicyData(policyAdapter, entity);
94                 }
95                 else if("BRMS_Raw".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
96                         new CreateBRMSRawController().prePopulateBRMSRawPolicyData(policyAdapter, entity);
97                 }
98                 else if("BRMS_Param".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
99                         new CreateBRMSParamController().prePopulateBRMSParamPolicyData(policyAdapter, entity);
100                 }
101                 else if("ClosedLoop_Fault".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
102                         new CreateClosedLoopFaultController().prePopulateClosedLoopFaultPolicyData(policyAdapter, entity);
103                 }
104                 else if("ClosedLoop_PM".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
105                         new CreateClosedLoopPMController().prePopulateClosedLoopPMPolicyData(policyAdapter, entity);
106                 }
107                 else if("Micro Service".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
108                         new CreateDcaeMicroServiceController().prePopulateDCAEMSPolicyData(policyAdapter, entity);
109                 }
110                 else if("Optimization".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
111                         new CreateOptimizationController().prePopulatePolicyData(policyAdapter, entity);
112                 }
113                 else if("Firewall Config".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
114                         new CreateFirewallController().prePopulateFWPolicyData(policyAdapter, entity);
115                 }
116         }
117
118         private boolean extendedOptions(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
119                 return false;
120         }
121
122         public static PolicyAdapter getInstance() {
123                 try {
124                         Class<?> policyAdapter = Class.forName(XACMLProperties.getProperty("policyAdapter.impl.className", PolicyAdapter.class.getName()));
125                         return (PolicyAdapter) policyAdapter.newInstance();
126                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException e) {
127                         LOGGER.error("Exception Occured"+e);
128                 }
129                 return null;
130         }
131
132 }