c1d1e9ce5e22a9037e905893e4ec66eb2fbf8c56
[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 = null ;
50                 if(policyAdapter.getPolicyName().startsWith("Config_PM")){
51                         configPolicyName = "ClosedLoop_PM";
52                 }else if(policyAdapter.getPolicyName().startsWith("Config_Fault")){
53                         configPolicyName = "ClosedLoop_Fault";
54                 }else if(policyAdapter.getPolicyName().startsWith("Config_FW")){
55                         configPolicyName = "Firewall Config";
56                 }else if(policyAdapter.getPolicyName().startsWith("Config_BRMS_Raw")){
57                         configPolicyName = "BRMS_Raw";
58                 }else if(policyAdapter.getPolicyName().startsWith("Config_BRMS_Param")){
59                         configPolicyName = "BRMS_Param";
60                 }else if(policyAdapter.getPolicyName().startsWith("Config_MS")){
61                         configPolicyName = "Micro Service";
62                 }else if(policyAdapter.getPolicyName().startsWith("Config_OOF")){
63                         configPolicyName = "Optimization";
64                 }else if(policyAdapter.getPolicyName().startsWith("Action") || policyAdapter.getPolicyName().startsWith("Decision") ){
65                         // No configPolicyName is applicable
66                 }else{
67                         configPolicyName = "Base";
68                 }
69                 if (policyNameValue != null) {
70                         policyAdapter.setPolicyType(policyNameValue);
71                 }
72                 if (configPolicyName != null) {
73                         policyAdapter.setConfigPolicyType(configPolicyName);
74                 }
75
76                 if("Action".equalsIgnoreCase(policyAdapter.getPolicyType())){
77                         new ActionPolicyController().prePopulateActionPolicyData(policyAdapter, entity);
78                 }
79                 if("Decision".equalsIgnoreCase(policyAdapter.getPolicyType())){
80                         new DecisionPolicyController().prePopulateDecisionPolicyData(policyAdapter, entity);
81                 }
82                 if("Config".equalsIgnoreCase(policyAdapter.getPolicyType())){
83                         if("Base".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
84                                 new CreatePolicyController().prePopulateBaseConfigPolicyData(policyAdapter, entity);
85                         }
86                         else if("BRMS_Raw".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
87                                 new CreateBRMSRawController().prePopulateBRMSRawPolicyData(policyAdapter, entity);
88                         }
89                         else if("BRMS_Param".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
90                                 new CreateBRMSParamController().prePopulateBRMSParamPolicyData(policyAdapter, entity);
91                         }
92                         else if("ClosedLoop_Fault".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
93                                 new CreateClosedLoopFaultController().prePopulateClosedLoopFaultPolicyData(policyAdapter, entity);
94                         }
95                         else if("ClosedLoop_PM".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
96                                 new CreateClosedLoopPMController().prePopulateClosedLoopPMPolicyData(policyAdapter, entity);
97                         }
98                         else if("Micro Service".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
99                                 new CreateDcaeMicroServiceController().prePopulateDCAEMSPolicyData(policyAdapter, entity);
100                         }
101                         else if("Optimization".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
102                                 new CreateOptimizationController().prePopulatePolicyData(policyAdapter, entity);
103                         }
104                         else if("Firewall Config".equalsIgnoreCase(policyAdapter.getConfigPolicyType())){
105                                 new CreateFirewallController().prePopulateFWPolicyData(policyAdapter, entity);
106                         }
107                 }
108         }
109         
110         public boolean extendedOptions(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
111                 return false;
112         }
113
114         public static PolicyAdapter getInstance() {
115                 try {
116                         Class<?> policyAdapter = Class.forName(XACMLProperties.getProperty("policyAdapter.impl.className", PolicyAdapter.class.getName()));
117                         return (PolicyAdapter) policyAdapter.newInstance();
118                 } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException e) {
119                         LOGGER.error("Exception Occured"+e);
120                 }
121                 return null;
122         }
123
124 }