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