6aa40dacf352b53043b4baba9a62605a30180ad4
[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  * Modifications Copyright (C) 2019 Bell Canada
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.admin;
24
25 import org.onap.policy.common.logging.flexlogger.FlexLogger;
26 import org.onap.policy.common.logging.flexlogger.Logger;
27 import org.onap.policy.controller.ActionPolicyController;
28 import org.onap.policy.controller.CreateBRMSParamController;
29 import org.onap.policy.controller.CreateBRMSRawController;
30 import org.onap.policy.controller.CreateClosedLoopFaultController;
31 import org.onap.policy.controller.CreateClosedLoopPMController;
32 import org.onap.policy.controller.CreateDcaeMicroServiceController;
33 import org.onap.policy.controller.CreateFirewallController;
34 import org.onap.policy.controller.CreateOptimizationController;
35 import org.onap.policy.controller.CreatePolicyController;
36 import org.onap.policy.controller.DecisionPolicyController;
37 import org.onap.policy.rest.adapter.PolicyRestAdapter;
38 import org.onap.policy.rest.jpa.PolicyEntity;
39
40 import com.att.research.xacml.util.XACMLProperties;
41
42 public class PolicyAdapter {
43
44     private static final Logger LOGGER = FlexLogger.getLogger(PolicyAdapter.class);
45
46     public void configure(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
47         String policyNameValue = policyAdapter.getPolicyName().substring(0, policyAdapter.getPolicyName().indexOf('_'));
48         String configPolicyName = getConfigPolicyName(policyAdapter);
49         policyAdapter.setPolicyType(policyNameValue);
50
51         if (configPolicyName != null) {
52             policyAdapter.setConfigPolicyType(configPolicyName);
53         }
54         if ("Action".equalsIgnoreCase(policyAdapter.getPolicyType())) {
55             new ActionPolicyController().prePopulateActionPolicyData(policyAdapter);
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()
82             .startsWith("Decision")) {
83             // No configPolicyName is applicable
84         } else {
85             configPolicyName = "Base";
86         }
87         return configPolicyName;
88     }
89
90     private void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
91         if ("Base".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
92             new CreatePolicyController().prePopulateBaseConfigPolicyData(policyAdapter, entity);
93         } else if ("BRMS_Raw".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
94             new CreateBRMSRawController().prePopulateBRMSRawPolicyData(policyAdapter, entity);
95         } else if ("BRMS_Param".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
96             new CreateBRMSParamController().prePopulateBRMSParamPolicyData(policyAdapter, entity);
97         } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
98             new CreateClosedLoopFaultController().prePopulateClosedLoopFaultPolicyData(policyAdapter, entity);
99         } else if ("ClosedLoop_PM".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
100             new CreateClosedLoopPMController().prePopulateClosedLoopPMPolicyData(policyAdapter, entity);
101         } else if ("Micro Service".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
102             new CreateDcaeMicroServiceController().prePopulateDCAEMSPolicyData(policyAdapter, entity);
103         } else if ("Optimization".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
104             new CreateOptimizationController().prePopulatePolicyData(policyAdapter, entity);
105         } else if ("Firewall Config".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
106             new CreateFirewallController().prePopulateFWPolicyData(policyAdapter, entity);
107         }
108     }
109
110     public static PolicyAdapter getInstance() {
111         try {
112             Class<?> policyAdapter = Class
113                 .forName(XACMLProperties.getProperty("policyAdapter.impl.className", PolicyAdapter.class.getName()));
114             return (PolicyAdapter) policyAdapter.newInstance();
115         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | IllegalArgumentException e) {
116             LOGGER.error("Exception Occurred" + e);
117         }
118         return null;
119     }
120 }