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