Refactor code for nested stmts in policy std
[policy/engine.git] / PolicyEngineAPI / src / main / java / org / onap / policy / std / StdPolicyEngine.java
index 3d3ecee..8f3cf04 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * PolicyEngineAPI
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
+ * Modified Copyright (C) 2018 Samsung Electronics Co., Ltd.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -175,6 +176,13 @@ public class StdPolicyEngine {
     public StdPolicyEngine(final String propertyFilePath, final String clientKey) throws PolicyEngineException {
         setProperty(propertyFilePath, clientKey);
     }
+    
+    /*
+     * Taking the Property structure even if it null.
+     */
+    public StdPolicyEngine(final Properties properties, final String clientKey) throws PolicyEngineException {
+        setProperty(properties, clientKey);
+    }
 
     /*
      * Taking the Notification Constructor.
@@ -341,7 +349,7 @@ public class StdPolicyEngine {
         return deletePolicyImpl(parameters);
     }
 
-    public PolicyChangeResponse deletePolicyImpl(final DeletePolicyParameters parameters) throws PolicyException {
+    private PolicyChangeResponse deletePolicyImpl(final DeletePolicyParameters parameters) throws PolicyException {
         final StdPolicyChangeResponse response = new StdPolicyChangeResponse();
         String body = null;
         // Create Request.
@@ -372,7 +380,7 @@ public class StdPolicyEngine {
         return getDictionaryItemImpl(parameters);
     }
 
-    public DictionaryResponse getDictionaryItemImpl(final DictionaryParameters parameters) throws PolicyException {
+    private DictionaryResponse getDictionaryItemImpl(final DictionaryParameters parameters) throws PolicyException {
         final StdDictionaryResponse response = new StdDictionaryResponse();
         String body = "{}";
         // Create Request.
@@ -443,7 +451,7 @@ public class StdPolicyEngine {
         return createUpdateDictionaryItemImpl(parameters, true);
     }
 
-    public PolicyChangeResponse createUpdateDictionaryItemImpl(final DictionaryParameters parameters,
+    private PolicyChangeResponse createUpdateDictionaryItemImpl(final DictionaryParameters parameters,
             final boolean updateFlag) throws PolicyException {
 
         final String resource = getDictionaryResouceName(updateFlag);
@@ -480,7 +488,7 @@ public class StdPolicyEngine {
         return policyEngineImportImpl(importParameters);
     }
 
-    public PolicyChangeResponse policyEngineImportImpl(final ImportParameters importParameters) throws PolicyException {
+    private PolicyChangeResponse policyEngineImportImpl(final ImportParameters importParameters) throws PolicyException {
         final LinkedMultiValueMap<String, Object> parameters = new LinkedMultiValueMap<>();
         // Create Request.
         try {
@@ -523,7 +531,7 @@ public class StdPolicyEngine {
         return createUpdatePolicyImpl(policyParameters, true);
     }
 
-    public PolicyChangeResponse createUpdatePolicyImpl(final PolicyParameters policyParameters,
+    private PolicyChangeResponse createUpdatePolicyImpl(final PolicyParameters policyParameters,
             final boolean updateFlag) throws PolicyException {
         final String resource = getPolicyResourceName(updateFlag);
         String body = null;
@@ -568,7 +576,7 @@ public class StdPolicyEngine {
         }
     }
 
-    public DecisionResponse getDecisionImpl(final String onapName, final Map<String, String> decisionAttributes,
+    private DecisionResponse getDecisionImpl(final String onapName, final Map<String, String> decisionAttributes,
             final UUID requestID) throws PolicyDecisionException {
         String body = null;
         // Create Request.
@@ -597,7 +605,7 @@ public class StdPolicyEngine {
         }
     }
 
-    public Collection<PolicyConfig> getConfigImpl(final ConfigRequestParameters configRequestParameters)
+    private Collection<PolicyConfig> getConfigImpl(final ConfigRequestParameters configRequestParameters)
             throws PolicyConfigException {
         String body = null;
         // Create Request.
@@ -625,51 +633,53 @@ public class StdPolicyEngine {
     private ArrayList<PolicyConfig> configResult(final APIPolicyConfigResponse[] response)
             throws PolicyConfigException {
         final ArrayList<PolicyConfig> result = new ArrayList<>();
-        if (response != null) {
-            for (final APIPolicyConfigResponse policyConfigResponse : response) {
-                final StdPolicyConfig policyConfig = new StdPolicyConfig();
-                policyConfig.setConfigStatus(policyConfigResponse.getPolicyConfigMessage());
-                policyConfig.setMatchingConditions(policyConfigResponse.getMatchingConditions());
-                policyConfig.setPolicyConfigStatus(policyConfigResponse.getPolicyConfigStatus());
-                policyConfig.setPolicyName(policyConfigResponse.getPolicyName());
-                policyConfig.setPolicyType(policyConfigResponse.getType());
-                policyConfig.setPolicyVersion(policyConfigResponse.getPolicyVersion());
-                policyConfig.setPolicyType(policyConfigResponse.getPolicyType());
-                policyConfig.setResponseAttributes(policyConfigResponse.getResponseAttributes());
-                setMatches(policyConfig.getMatchingConditions());
-                if (policyConfigResponse.getType() != null) {
-                    try {
-                        switch (policyConfigResponse.getType()) {
-                            case JSON:
-                                final StringReader reader = new StringReader(policyConfigResponse.getConfig());
-                                try (final JsonReader jsonReader = Json.createReader(reader)) {
-                                    final JsonObject object = jsonReader.readObject();
-                                    policyConfig.setJsonObject(object);
-                                }
-                                break;
-                            case OTHER:
-                                policyConfig.setOther(policyConfigResponse.getConfig());
-                                break;
-                            case PROPERTIES:
-                                final Properties props = new Properties();
-                                props.putAll(policyConfigResponse.getProperty());
-                                policyConfig.setProperties(props);
-                                break;
-                            case XML:
-                                final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
-                                final DocumentBuilder builder = factory.newDocumentBuilder();
-                                final StringReader stringReader = new StringReader(policyConfigResponse.getConfig());
-                                policyConfig.setDocument(builder.parse(new InputSource(stringReader)));
-                                break;
-                        }
-                    } catch (final Exception exception) {
-                        LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID + exception);
-                        throw new PolicyConfigException(
-                                XACMLErrorConstants.ERROR_SCHEMA_INVALID + "Unable to parse the config", exception);
+        if (response == null) {
+            return result;
+        }
+
+        for (final APIPolicyConfigResponse policyConfigResponse : response) {
+            final StdPolicyConfig policyConfig = new StdPolicyConfig();
+            policyConfig.setConfigStatus(policyConfigResponse.getPolicyConfigMessage());
+            policyConfig.setMatchingConditions(policyConfigResponse.getMatchingConditions());
+            policyConfig.setPolicyConfigStatus(policyConfigResponse.getPolicyConfigStatus());
+            policyConfig.setPolicyName(policyConfigResponse.getPolicyName());
+            policyConfig.setPolicyType(policyConfigResponse.getType());
+            policyConfig.setPolicyVersion(policyConfigResponse.getPolicyVersion());
+            policyConfig.setPolicyType(policyConfigResponse.getPolicyType());
+            policyConfig.setResponseAttributes(policyConfigResponse.getResponseAttributes());
+            setMatches(policyConfig.getMatchingConditions());
+            if (policyConfigResponse.getType() != null) {
+                try {
+                    switch (policyConfigResponse.getType()) {
+                        case JSON:
+                            final StringReader reader = new StringReader(policyConfigResponse.getConfig());
+                            try (final JsonReader jsonReader = Json.createReader(reader)) {
+                                final JsonObject object = jsonReader.readObject();
+                                policyConfig.setJsonObject(object);
+                            }
+                            break;
+                        case OTHER:
+                            policyConfig.setOther(policyConfigResponse.getConfig());
+                            break;
+                        case PROPERTIES:
+                            final Properties props = new Properties();
+                            props.putAll(policyConfigResponse.getProperty());
+                            policyConfig.setProperties(props);
+                            break;
+                        case XML:
+                            final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+                            final DocumentBuilder builder = factory.newDocumentBuilder();
+                            final StringReader stringReader = new StringReader(policyConfigResponse.getConfig());
+                            policyConfig.setDocument(builder.parse(new InputSource(stringReader)));
+                            break;
                     }
+                } catch (final Exception exception) {
+                    LOGGER.error(XACMLErrorConstants.ERROR_SCHEMA_INVALID + exception);
+                    throw new PolicyConfigException(
+                            XACMLErrorConstants.ERROR_SCHEMA_INVALID + "Unable to parse the config", exception);
                 }
-                result.add(policyConfig);
             }
+            result.add(policyConfig);
         }
         return result;
     }
@@ -767,7 +777,7 @@ public class StdPolicyEngine {
         clientEncoding = encoder.encodeToString((userName + ":" + pass).getBytes(StandardCharsets.UTF_8));
     }
 
-    public Collection<String> listConfigImpl(final ConfigRequestParameters listRequestParameters)
+    private Collection<String> listConfigImpl(final ConfigRequestParameters listRequestParameters)
             throws PolicyConfigException {
         final Collection<String> policyList = new ArrayList<>();
         if (junit) {
@@ -785,7 +795,7 @@ public class StdPolicyEngine {
         return policyList;
     }
 
-    public Collection<PolicyResponse> sendEventImpl(final Map<String, String> eventAttributes, final UUID requestID)
+    private Collection<PolicyResponse> sendEventImpl(final Map<String, String> eventAttributes, final UUID requestID)
             throws PolicyEventException {
         String body = null;
         // Create Request.
@@ -840,16 +850,24 @@ public class StdPolicyEngine {
             throw new PolicyEngineException(
                     XACMLErrorConstants.ERROR_DATA_ISSUE + "Error NO PropertyFile Path provided");
         }
-
         final Properties prop = getProperties(propertyFilePath);
+        setProperty(prop,clientKey);
+    }
+    
+    private void setProperty(final Properties properties, String clientKey) throws PolicyEngineException {
+        if (properties == null) {
+            throw new PolicyEngineException(
+                    XACMLErrorConstants.ERROR_DATA_ISSUE + "NO properties provided, the value is NULL");
+        }
+
         // UEB and DMAAP Settings
-        final String notificationTypeValue = prop.getProperty(NOTIFICATION_TYPE_PROP_NAME);
-        final String serverList = prop.getProperty(NOTIFICATION_SERVERS_PROP_NAME);
-        topic = prop.getProperty(NOTIFICATION_TOPIC_PROP_NAME);
-        apiKey = prop.getProperty(UEB_API_KEY_PROP_NAME);
-        apiSecret = prop.getProperty(UEB_API_SECRET_PROP_NAME);
+        final String notificationTypeValue = properties.getProperty(NOTIFICATION_TYPE_PROP_NAME);
+        final String serverList = properties.getProperty(NOTIFICATION_SERVERS_PROP_NAME);
+        topic = properties.getProperty(NOTIFICATION_TOPIC_PROP_NAME);
+        apiKey = properties.getProperty(UEB_API_KEY_PROP_NAME);
+        apiSecret = properties.getProperty(UEB_API_SECRET_PROP_NAME);
 
-        setNotificationType(notificationTypeValue, DEFAULT_NOTIFICATION);
+        setNotificationType(notificationTypeValue);
 
         if (serverList == null) {
             notificationType.clear();
@@ -857,7 +875,7 @@ public class StdPolicyEngine {
             LOGGER.info(
                     "Properties file doesn't have the NOTIFICATION_SERVERS parameter system will use defualt websockets");
         } else {
-            notificationURLList = getPropertyValueAsList(serverList.trim(), COMMA);
+            notificationURLList = getPropertyValueAsList(serverList.trim());
         }
 
         if (topic != null) {
@@ -867,9 +885,9 @@ public class StdPolicyEngine {
         }
 
         // Client ID Authorization Settings.
-        final String clientID = prop.getProperty(CLIENT_ID_PROP_NAME);
+        final String clientID = properties.getProperty(CLIENT_ID_PROP_NAME);
         if (clientKey == null) {
-            clientKey = getClientKeyFromProperties(prop);
+            clientKey = getClientKeyFromProperties(properties);
         }
         if (clientID == null || clientKey == null || clientID.isEmpty() || clientKey.isEmpty()) {
             LOGGER.error(XACMLErrorConstants.ERROR_PERMISSIONS
@@ -880,31 +898,19 @@ public class StdPolicyEngine {
             setClientId(clientID.trim());
             setClientKey(clientKey.trim());
         }
-        setEnvironment(prop);
+        setEnvironment(properties);
         // Initializing the values.
         init();
-        readPdpProperites(prop);
+        readPdpProperites(properties);
         // Get JUNIT property from properties file when running tests
-        checkJunit(prop);
+        checkJunit(properties);
     }
 
     private void readPdpProperites(final Properties prop) throws PolicyEngineException {
         // Check the Keys for PDP_URLs
         for (final String propertyKey : prop.stringPropertyNames()) {
             if (propertyKey.startsWith(PDP_URL_PROP_NAME)) {
-                final String propertyValue = prop.getProperty(propertyKey);
-                if (propertyValue == null) {
-                    throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE
-                            + "Properties file doesn't have the PDP_URL parameter");
-                }
-                if (propertyValue.contains(SEMICOLLON)) {
-                    final List<String> pdpDefault = Arrays.asList(propertyValue.split(REGEX));
-                    for (final String pdpVal : pdpDefault) {
-                        readPDPParam(pdpVal);
-                    }
-                } else {
-                    readPDPParam(propertyValue);
-                }
+                readPDPPropertyURL(prop, propertyKey);
             }
         }
         if (pdps == null || pdps.isEmpty()) {
@@ -913,13 +919,29 @@ public class StdPolicyEngine {
         }
     }
 
-    private void setNotificationType(final String propertyValue, final String defaultValue) {
+    private void readPDPPropertyURL(Properties prop, String propertyKey) throws PolicyEngineException {
+        final String propertyValue = prop.getProperty(propertyKey);
+        if (propertyValue == null) {
+            throw new PolicyEngineException(XACMLErrorConstants.ERROR_DATA_ISSUE
+                    + "Properties file doesn't have the PDP_URL parameter");
+        }
+        if (propertyValue.contains(SEMICOLLON)) {
+            final List<String> pdpDefault = Arrays.asList(propertyValue.split(REGEX));
+            for (final String pdpVal : pdpDefault) {
+                readPDPParam(pdpVal);
+            }
+        } else {
+            readPDPParam(propertyValue);
+        }
+    }
+
+    private void setNotificationType(final String propertyValue) {
         if (propertyValue == null) {
-            notificationType.add(defaultValue);
+            notificationType.add(DEFAULT_NOTIFICATION);
             LOGGER.info(
                     "Properties file doesn't have the NOTIFICATION_TYPE parameter system will use defualt websockets");
         } else {
-            notificationType = getPropertyValueAsList(propertyValue.trim(), COMMA);
+            notificationType = getPropertyValueAsList(propertyValue.trim());
         }
     }
 
@@ -977,9 +999,9 @@ public class StdPolicyEngine {
         }
     }
 
-    private List<String> getPropertyValueAsList(final String propertyValue, final String split) {
-        if (propertyValue.contains(split)) {
-            return Arrays.asList(propertyValue.split(split));
+    private List<String> getPropertyValueAsList(final String propertyValue) {
+        if (propertyValue.contains(COMMA)) {
+            return Arrays.asList(propertyValue.split(COMMA));
         }
         final List<String> valuesList = new ArrayList<>();
         valuesList.add(propertyValue);
@@ -1072,28 +1094,29 @@ public class StdPolicyEngine {
         if (junit) {
             return;
         }
+        if (pdps == null) {
+            return;
+        }
 
-        if (pdps != null) {
-            if (UEB.equals(notificationType.get(0)) && !this.uebThread) {
-                this.uebClientThread = new AutoClientUEB(pdps.get(0), notificationURLList, apiKey, apiSecret);
-                AutoClientUEB.setAuto(scheme, handler);
-                this.registerUEBThread = new Thread(this.uebClientThread);
-                this.registerUEBThread.start();
-                this.uebThread = true;
-            } else if (notificationType.get(0).equals(DMAAP) && !this.dmaapThread) {
-                this.dmaapClientThread = new AutoClientDMAAP(notificationURLList, topic, userName, pass);
-                AutoClientDMAAP.setAuto(scheme, handler);
-                this.registerDMAAPThread = new Thread(this.dmaapClientThread);
-                this.registerDMAAPThread.start();
-                this.dmaapThread = true;
-            } else {
-                if (pdps.get(0) != null) {
-                    if (AutoClientEnd.getURL() == null) {
-                        AutoClientEnd.start(pdps.get(0));
-                    } else {
-                        AutoClientEnd.stop();
-                        AutoClientEnd.start(pdps.get(0));
-                    }
+        if (UEB.equals(notificationType.get(0)) && !this.uebThread) {
+            this.uebClientThread = new AutoClientUEB(pdps.get(0), notificationURLList, apiKey, apiSecret);
+            AutoClientUEB.setAuto(scheme, handler);
+            this.registerUEBThread = new Thread(this.uebClientThread);
+            this.registerUEBThread.start();
+            this.uebThread = true;
+        } else if (notificationType.get(0).equals(DMAAP) && !this.dmaapThread) {
+            this.dmaapClientThread = new AutoClientDMAAP(notificationURLList, topic, userName, pass);
+            AutoClientDMAAP.setAuto(scheme, handler);
+            this.registerDMAAPThread = new Thread(this.dmaapClientThread);
+            this.registerDMAAPThread.start();
+            this.dmaapThread = true;
+        } else {
+            if (pdps.get(0) != null) {
+                if (AutoClientEnd.getUrl() == null) {
+                    AutoClientEnd.start(pdps.get(0));
+                } else {
+                    AutoClientEnd.stop();
+                    AutoClientEnd.start(pdps.get(0));
                 }
             }
         }
@@ -1212,7 +1235,7 @@ public class StdPolicyEngine {
     /*
      * Create Config Policy API Implementation
      */
-    public String createUpdateConfigPolicyImpl(final String policyName, final String policyDescription,
+    private String createUpdateConfigPolicyImpl(final String policyName, final String policyDescription,
             final String onapName, final String configName, final Map<String, String> configAttributes,
             final String configType, final String body, final String policyScope, final UUID requestID,
             final String riskLevel, final String riskType, final String guard, final String ttlDate,
@@ -1261,7 +1284,7 @@ public class StdPolicyEngine {
     /*
      * Create Update Config Firewall Policy API implementation
      */
-    public String createUpdateConfigFirewallPolicyImpl(final String policyName, final JsonObject firewallJson,
+    private String createUpdateConfigFirewallPolicyImpl(final String policyName, final JsonObject firewallJson,
             final String policyScope, final UUID requestID, final String riskLevel, final String riskType,
             final String guard, final String ttlDate, final boolean updateFlag) throws PolicyException {
         validateParameters(policyName, policyScope);