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