More sonar cleanup and line consolidation
[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         if (policyAdapter.getPolicyName().startsWith("Config_PM")) {
73             return "ClosedLoop_PM";
74         } else if (policyAdapter.getPolicyName().startsWith("Config_Fault")) {
75             return "ClosedLoop_Fault";
76         } else if (policyAdapter.getPolicyName().startsWith("Config_FW")) {
77             return "Firewall Config";
78         } else if (policyAdapter.getPolicyName().startsWith("Config_BRMS_Raw")) {
79             return "BRMS_Raw";
80         } else if (policyAdapter.getPolicyName().startsWith("Config_BRMS_Param")) {
81             return "BRMS_Param";
82         } else if (policyAdapter.getPolicyName().startsWith("Config_MS")) {
83             return "Micro Service";
84         } else if (policyAdapter.getPolicyName().startsWith("Config_OOF")) {
85             return "Optimization";
86         } else if (policyAdapter.getPolicyName().startsWith("Action")
87                 || policyAdapter.getPolicyName().startsWith("Decision")) {
88             return null;
89         }
90         return "Base";
91     }
92
93     private void prePopulatePolicyData(PolicyRestAdapter policyAdapter, PolicyEntity entity) {
94         if ("Base".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
95             new CreatePolicyController().prePopulateBaseConfigPolicyData(policyAdapter, entity);
96         } else if ("BRMS_Raw".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
97             new CreateBRMSRawController().prePopulateBRMSRawPolicyData(policyAdapter, entity);
98         } else if ("BRMS_Param".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
99             new CreateBRMSParamController().prePopulateBRMSParamPolicyData(policyAdapter, entity);
100         } else if ("ClosedLoop_Fault".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
101             new CreateClosedLoopFaultController().prePopulateClosedLoopFaultPolicyData(policyAdapter, entity);
102         } else if ("ClosedLoop_PM".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
103             new CreateClosedLoopPMController().prePopulateClosedLoopPMPolicyData(policyAdapter, entity);
104         } else if ("Micro Service".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
105             new CreateDcaeMicroServiceController().prePopulateDCAEMSPolicyData(policyAdapter, entity);
106         } else if ("Optimization".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
107             new CreateOptimizationController().prePopulatePolicyData(policyAdapter, entity);
108         } else if ("Firewall Config".equalsIgnoreCase(policyAdapter.getConfigPolicyType())) {
109             new CreateFirewallController().prePopulateFWPolicyData(policyAdapter, entity);
110         }
111     }
112
113     /**
114      * getInstance.
115      *
116      * @return Returns a PolicyAdapter instance
117      */
118     public static PolicyAdapter getInstance() {
119         try {
120             Class<?> policyAdapter = Class.forName(
121                     XACMLProperties.getProperty("policyAdapter.impl.className", PolicyAdapter.class.getName()));
122             return (PolicyAdapter) policyAdapter.newInstance();
123         } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
124                 | IllegalArgumentException e) {
125             LOGGER.error("Exception Occurred" + e);
126         }
127         return null;
128     }
129 }