Remove useless imports and vars
[policy/engine.git] / ONAP-PAP-REST / src / main / java / org / onap / policy / pap / xacml / rest / components / FirewallConfigPolicy.java
index 6cadc77..228dc83 100644 (file)
@@ -2,14 +2,15 @@
  * ============LICENSE_START=======================================================
  * ONAP-PAP-REST
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017,2019 AT&T Intellectual Property. All rights reserved.
+ * Modified Copyright (C) 2019 Bell Canada.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
 package org.onap.policy.pap.xacml.rest.components;
 
+import com.att.research.xacml.api.pap.PAPException;
+import com.att.research.xacml.std.IdentifierImpl;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.github.fge.jackson.JsonLoader;
+import com.github.fge.jsonpatch.diff.JsonDiff;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileWriter;
@@ -35,22 +41,19 @@ import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-
 import javax.json.Json;
 import javax.json.JsonArray;
 import javax.json.JsonObject;
 import javax.json.JsonReader;
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-
+import javax.script.SimpleBindings;
 import org.apache.commons.io.FilenameUtils;
 import org.onap.policy.common.logging.eelf.MessageCodes;
 import org.onap.policy.common.logging.eelf.PolicyLogger;
 import org.onap.policy.common.logging.flexlogger.FlexLogger;
 import org.onap.policy.common.logging.flexlogger.Logger;
-import org.onap.policy.pap.xacml.rest.XACMLPapServlet;
 import org.onap.policy.pap.xacml.rest.daoimpl.CommonClassDaoImpl;
 import org.onap.policy.rest.adapter.PolicyRestAdapter;
+import org.onap.policy.rest.dao.CommonClassDao;
 import org.onap.policy.rest.jpa.ActionList;
 import org.onap.policy.rest.jpa.AddressGroup;
 import org.onap.policy.rest.jpa.GroupServiceList;
@@ -61,13 +64,8 @@ import org.onap.policy.rest.jpa.ProtocolList;
 import org.onap.policy.rest.jpa.ServiceList;
 import org.onap.policy.rest.jpa.TermList;
 import org.onap.policy.rest.jpa.UserInfo;
-
-import com.att.research.xacml.api.pap.PAPException;
-import com.att.research.xacml.std.IdentifierImpl;
-import com.fasterxml.jackson.databind.JsonNode;
-import com.github.fge.jackson.JsonLoader;
-import com.github.fge.jsonpatch.diff.JsonDiff;
-
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AdviceExpressionsType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
@@ -82,6 +80,7 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
 
+@Component
 public class FirewallConfigPolicy extends Policy {
 
     private static final Logger LOGGER = FlexLogger.getLogger(FirewallConfigPolicy.class);
@@ -90,6 +89,13 @@ public class FirewallConfigPolicy extends Policy {
         super();
     }
 
+    private static CommonClassDao commonClassDao;
+
+    @Autowired
+    public FirewallConfigPolicy(CommonClassDao commonClassDao) {
+        FirewallConfigPolicy.commonClassDao = commonClassDao;
+    }
+
     public FirewallConfigPolicy(PolicyRestAdapter policyAdapter) {
         this.policyAdapter = policyAdapter;
         this.policyAdapter.setConfigType(policyAdapter.getConfigType());
@@ -98,11 +104,11 @@ public class FirewallConfigPolicy extends Policy {
     // Saving the Configurations file at server location for config policy.
     protected void saveConfigurations(String policyName, String jsonBody) {
         String configurationName = policyName;
-        if(configurationName.endsWith(".xml")){
+        if (configurationName.endsWith(".xml")) {
             configurationName = configurationName.replace(".xml", "");
         }
         String fileName = CONFIG_HOME + File.separator + configurationName + ".json";
-        try(BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))){
+        try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName))) {
             bw.write(jsonBody);
             if (LOGGER.isDebugEnabled()) {
                 LOGGER.debug("Configuration is succesfully saved");
@@ -200,16 +206,15 @@ public class FirewallConfigPolicy extends Policy {
             oldversion = oldversion - 1;
             dbPolicyName = dbPolicyName + oldversion + ".xml";
         }
-        EntityManager em = XACMLPapServlet.getEmf().createEntityManager();
-        Query createPolicyQuery = em.createQuery("SELECT p FROM PolicyEntity p WHERE p.scope=:scope AND p.policyName=:policyName");
-        createPolicyQuery.setParameter("scope", scope);
-        createPolicyQuery.setParameter("policyName", dbPolicyName);
-        List<?> createPolicyQueryList = createPolicyQuery.getResultList();
-        if(!createPolicyQueryList.isEmpty()){
+        String createPolicyQuery = "SELECT p FROM PolicyEntity p WHERE p.scope=:scope AND p.policyName=:policyName";
+        SimpleBindings params = new SimpleBindings();
+        params.put("scope", scope);
+        params.put("policyName", dbPolicyName);
+        List<?> createPolicyQueryList = commonClassDao.getDataByQuery(createPolicyQuery, params);
+        if (!createPolicyQueryList.isEmpty()) {
             PolicyEntity entitydata = (PolicyEntity) createPolicyQueryList.get(0);
             policyAdapter.setPrevJsonBody(entitydata.getConfigurationData().getConfigBody());
         }
-        em.close();
         if (policyAdapter.getData() != null) {
             String jsonBody = policyAdapter.getJsonBody();
             saveConfigurations(policyName, jsonBody);
@@ -252,7 +257,7 @@ public class FirewallConfigPolicy extends Policy {
             target.getAnyOf().add(anyOf);
 
             // Adding the target to the policy element
-            configPolicy.setTarget((TargetType) target);
+            configPolicy.setTarget(target);
 
             RuleType rule = new RuleType();
             rule.setRuleId(policyAdapter.getRuleID());
@@ -544,16 +549,7 @@ public class FirewallConfigPolicy extends Policy {
                                 value = srcListObj.get("value").toString();
                             }
 
-                            if (value!=null){
-                                value = value.replace("\"", "");
-                            }
-
-                            if (srcListString != null) {
-                                srcListString = srcListString.concat(",").concat(value);
-
-                            } else {
-                                srcListString = value;
-                            }
+                            srcListString = getLeftOrRight(srcListString, value);
 
                         }
                         String srcListInsert = "'"+srcListString+"'";
@@ -574,15 +570,7 @@ public class FirewallConfigPolicy extends Policy {
                                 value = destListObj.get("value").toString();
                             }
 
-                            if (value!=null){
-                                value = value.replace("\"", "");
-                            }
-
-                            if (destListString != null) {
-                                destListString = destListString.concat(",").concat(value);
-                            } else {
-                                destListString = value;
-                            }
+                            destListString = getLeftOrRight(destListString, value);
                         }
                         String destListInsert = "'"+destListString+"'";
 
@@ -602,15 +590,7 @@ public class FirewallConfigPolicy extends Policy {
                                 value = destServicesObj.get("value").toString();
                             }
 
-                            if (value!=null){
-                                value = value.replace("\"", "");
-                            }
-
-                            if (destPortListString != null) {
-                                destPortListString = destPortListString.concat(",").concat(value);
-                            } else {
-                                destPortListString = value;
-                            }
+                            destPortListString = getLeftOrRight(destPortListString, value);
                         }
                         String destPortListInsert = "'"+destPortListString+"'";
 
@@ -636,10 +616,7 @@ public class FirewallConfigPolicy extends Policy {
                         termEntry.setUserCreatedBy(userInfo);
                         dbConnection.save(termEntry);
 
-                        ActionList actionEntry = new ActionList();
-                        actionEntry.setActionName(action);
-                        actionEntry.setDescription(action);
-                        dbConnection.save(actionEntry);
+                        saveActionListToDb(dbConnection, action);
                     }
                 }
 
@@ -674,36 +651,7 @@ public class FirewallConfigPolicy extends Policy {
 
                         //Insert values into GROUPSERVICELIST table if name begins with Group
                         if (isServiceGroup) {
-                            String name = null;
-                            for (int membersIndex = 0; membersIndex< membersArray.size(); membersIndex++) {
-                                JsonObject membersObj = membersArray.getJsonObject(membersIndex);
-                                //String value = membersObj.get("name").toString();
-                                String type = membersObj.get("type").toString().replace("\"", "");
-
-                                String value = null;
-                                if(type.equals("REFERENCE")||type.equals("GROUP")||type.equals("SERVICE")){
-                                    value = membersObj.get("name").toString();
-                                } else if (type.equalsIgnoreCase("ANY")){
-                                    value = null;
-                                } else {
-                                    value = membersObj.get("value").toString();
-                                }
-
-                                if(value != null){
-                                    value = value.replace("\"", "");
-                                }
-
-                                if (name != null) {
-                                    name = name.concat(",").concat(value);
-                                } else {
-                                    name = value;
-                                }
-                            }
-                            String nameInsert = "'"+name+"'";
-                            GroupServiceList groupServiceEntry = new GroupServiceList();
-                            groupServiceEntry.setGroupName(serviceListName);
-                            groupServiceEntry.setServiceList(nameInsert);
-                            dbConnection.save(groupServiceEntry);
+                            saveGroupServiceListTableToDb(dbConnection, serviceListName, membersArray);
                         } else { //Insert JSON data serviceList table, protollist table, and portlist table
                             String type = svcGroupListobj.get("type").toString();
                             String transportProtocol = svcGroupListobj.get("transportProtocol").toString();
@@ -712,24 +660,11 @@ public class FirewallConfigPolicy extends Policy {
                             /*
                              * Create Queries to INSERT data into database table and execute
                              */
-                            ServiceList serviceListEntry = new ServiceList();
-                            serviceListEntry.setServiceName(serviceListName);
-                            serviceListEntry.setServiceDescription(description);
-                            serviceListEntry.setServiceType(type);
-                            serviceListEntry.setServiceTransProtocol(transportProtocol);
-                            serviceListEntry.setServiceAppProtocol("null");
-                            serviceListEntry.setServicePorts(ports);
-                            dbConnection.save(serviceListEntry);
-
-                            ProtocolList protocolEntry = new ProtocolList();
-                            protocolEntry.setProtocolName(transportProtocol);
-                            protocolEntry.setDescription(transportProtocol);
-                            dbConnection.save(protocolEntry);
-
-                            PortList portListEntry = new PortList();
-                            portListEntry.setPortName(ports);
-                            portListEntry.setDescription(ports);
-                            dbConnection.save(portListEntry);
+                            saveServiceListToDb(dbConnection, serviceListName, description, type, transportProtocol, ports);
+
+                            saveProtocolListToDb(dbConnection, transportProtocol);
+
+                            savePortListToDb(dbConnection, ports);
                         }
                     }
                 }
@@ -758,72 +693,22 @@ public class FirewallConfigPolicy extends Policy {
                         String type = null;
                         for (int membersIndex = 0; membersIndex < membersArray.size(); membersIndex++) {
                             JsonObject membersObj = membersArray.getJsonObject(membersIndex);
-                            //String value = membersObj.get("value").toString();
                             type = membersObj.get("type").toString().replace("\"", "");
 
-                            String value = null;
-                            if(type.equals("REFERENCE")||type.equals("GROUP")||type.equals("SERVICE")){
-                                value = membersObj.get("name").toString();
-                            } else if (type.equalsIgnoreCase("ANY")){
-                                value = null;
-                            } else {
-                                value = membersObj.get("value").toString();
-                            }
-
-                            if(value != null){
-                                value = value.replace("\"", "");
-                            }
-
-                            if (prefixIP != null) {
-                                prefixIP = prefixIP.concat(",").concat(value);
-                            } else {
-                                prefixIP = value;
-                            }
+                            prefixIP = getName(prefixIP, membersObj, type);
                         }
                         String prefixList = "'"+prefixIP+"'";
 
                         Boolean isAddressGroup = type.contains("REFERENCE");
 
                         if (isAddressGroup) {
-                            AddressGroup addressGroupEntry = new AddressGroup();
-                            addressGroupEntry.setGroupName(addressGroupName);
-                            addressGroupEntry.setDescription(description);
-                            addressGroupEntry.setServiceList(prefixList);
-                            dbConnection.save(addressGroupEntry);
+                            saveAddressGroupToDb(dbConnection, addressGroupName, description, prefixList);
                         } else {
-                            PrefixList prefixListEntry = new PrefixList();
-                            prefixListEntry.setPrefixListName(addressGroupName);
-                            prefixListEntry.setDescription(description);
-                            prefixListEntry.setPrefixListValue(prefixList);
-                            dbConnection.save(prefixListEntry);
+                            savePrefixListToDb(dbConnection, addressGroupName, description, prefixList);
                         }
                     }
                 }
-
-                /*
-                 * Remove duplicate values from 'lookup' dictionary tables
-                 */
-                //ProtocolList Table
-                String protoDelete = "DELETE FROM protocollist USING protocollist, protocollist p1 "
-                        + "WHERE protocollist.id > p1.id AND protocollist.protocolname = p1.protocolname;";
-                dbConnection.updateQuery(protoDelete);
-
-                //PortList Table
-                String portListDelete = "DELETE FROM portlist USING portlist, portlist p1 "
-                        + "WHERE portlist.id > p1.id AND portlist.portname = p1.portname; ";
-                dbConnection.updateQuery(portListDelete);
-
-                //PrefixList Table
-                String prefixListDelete = "DELETE FROM prefixlist USING prefixlist, prefixlist p1 "
-                        + "WHERE prefixlist.id > p1.id AND prefixlist.pl_name = p1.pl_name AND "
-                        + "prefixlist.pl_value = p1.pl_value AND prefixlist.description = p1.description; ";
-                dbConnection.updateQuery(prefixListDelete);
-
-                //GroupServiceList
-                String groupServiceDelete = "DELETE FROM groupservicelist USING groupservicelist, groupservicelist g1 "
-                        + "WHERE groupservicelist.id > g1.id AND groupservicelist.name = g1.name AND "
-                        + "groupservicelist.serviceList = g1.serviceList; ";
-                dbConnection.updateQuery(groupServiceDelete);
+                removeDuplicateValuesFromLookup(dbConnection);
             }catch (Exception e) {
                 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "FirewallConfigPolicy", "Exception getting Json values");
                 return false;
@@ -836,6 +721,74 @@ public class FirewallConfigPolicy extends Policy {
 
     }
 
+    /*
+     * Remove duplicate values from 'lookup' dictionary tables
+     */
+    private void removeDuplicateValuesFromLookup(CommonClassDaoImpl dbConnection) {
+        String protoDelete = "DELETE FROM protocollist USING protocollist, protocollist p1 "
+                + "WHERE protocollist.id > p1.id AND protocollist.protocolname = p1.protocolname;";
+        dbConnection.updateQuery(protoDelete);
+
+        //PortList Table
+        String portListDelete = "DELETE FROM portlist USING portlist, portlist p1 "
+                + "WHERE portlist.id > p1.id AND portlist.portname = p1.portname; ";
+        dbConnection.updateQuery(portListDelete);
+
+        //PrefixList Table
+        String prefixListDelete = "DELETE FROM prefixlist USING prefixlist, prefixlist p1 "
+                + "WHERE prefixlist.id > p1.id AND prefixlist.pl_name = p1.pl_name AND "
+                + "prefixlist.pl_value = p1.pl_value AND prefixlist.description = p1.description; ";
+        dbConnection.updateQuery(prefixListDelete);
+
+        //GroupServiceList
+        String groupServiceDelete = "DELETE FROM groupservicelist USING groupservicelist, groupservicelist g1 "
+                + "WHERE groupservicelist.id > g1.id AND groupservicelist.name = g1.name AND "
+                + "groupservicelist.serviceList = g1.serviceList; ";
+        dbConnection.updateQuery(groupServiceDelete);
+    }
+
+    private void saveGroupServiceListTableToDb(CommonClassDaoImpl dbConnection, String serviceListName, JsonArray membersArray) {
+        String name = null;
+        for (int membersIndex = 0; membersIndex< membersArray.size(); membersIndex++) {
+            JsonObject membersObj = membersArray.getJsonObject(membersIndex);
+            String type = membersObj.get("type").toString().replace("\"", "");
+
+            name = getName(name, membersObj, type);
+        }
+        String nameInsert = "'"+name+"'";
+        GroupServiceList groupServiceEntry = new GroupServiceList();
+        groupServiceEntry.setGroupName(serviceListName);
+        groupServiceEntry.setServiceList(nameInsert);
+        dbConnection.save(groupServiceEntry);
+    }
+
+    private String getName(String name, JsonObject membersObj, String type) {
+        String value;
+        if(type.equals("REFERENCE")||type.equals("GROUP")||type.equals("SERVICE")){
+            value = membersObj.get("name").toString();
+        } else if (type.equalsIgnoreCase("ANY")){
+            value = null;
+        } else {
+            value = membersObj.get("value").toString();
+        }
+
+        name = getLeftOrRight(name, value);
+        return name;
+    }
+
+    private String getLeftOrRight(String name, String value) {
+        if (value != null) {
+            value = value.replace("\"", "");
+        }
+
+        if (name != null) {
+            name = name.concat(",").concat(value);
+        } else {
+            name = value.replace("\"", "");;
+        }
+        return name;
+    }
+
 
     private Boolean updateFirewallDictionaryData(String jsonBody, String prevJsonBody) {
         CommonClassDaoImpl dbConnection = new CommonClassDaoImpl();
@@ -946,16 +899,7 @@ public class FirewallConfigPolicy extends Policy {
                                     value = srcListObj.get("value").toString();
                                 }
 
-                                if(value != null){
-                                    value = value.replace("\"", "");
-                                }
-
-                                if (srcListString != null) {
-                                    srcListString = srcListString.concat(",").concat(value);
-
-                                } else {
-                                    srcListString = value;
-                                }
+                                srcListString = getLeftOrRight(srcListString, value);
 
                             }
                             String srcListInsert = "'"+srcListString+"'";
@@ -976,15 +920,7 @@ public class FirewallConfigPolicy extends Policy {
                                     value = destListObj.get("value").toString();
                                 }
 
-                                if(value != null){
-                                    value = value.replace("\"", "");
-                                }
-
-                                if (destListString != null) {
-                                    destListString = destListString.concat(",").concat(value);
-                                } else {
-                                    destListString = value;
-                                }
+                                destListString = getLeftOrRight(destListString, value);
                             }
                             String destListInsert = "'"+destListString+"'";
 
@@ -1004,15 +940,7 @@ public class FirewallConfigPolicy extends Policy {
                                     value = destServicesObj.get("value").toString();
                                 }
 
-                                if(value != null){
-                                    value = value.replace("\"", "");
-                                }
-
-                                if (destPortListString != null) {
-                                    destPortListString = destPortListString.concat(",").concat(value);
-                                } else {
-                                    destPortListString = value;
-                                }
+                                destPortListString = getLeftOrRight(destPortListString, value);
                             }
                             String destPortListInsert = "'"+destPortListString+"'";
 
@@ -1040,10 +968,7 @@ public class FirewallConfigPolicy extends Policy {
 
                             List<Object> actionResult = dbConnection.getDataById(ActionList.class, "actionName", action);
                             if(actionResult == null || actionResult.isEmpty()){
-                                ActionList actionEntry = new ActionList();
-                                actionEntry.setActionName(action);
-                                actionEntry.setDescription(action);
-                                dbConnection.save(actionEntry);
+                                saveActionListToDb(dbConnection, action);
                             }
                         }
                     }
@@ -1084,35 +1009,7 @@ public class FirewallConfigPolicy extends Policy {
                                     dbConnection.delete(groupEntry);
                                 }
 
-                                String name = null;
-                                for (int membersIndex = 0; membersIndex < membersArray.size(); membersIndex++) {
-                                    JsonObject membersObj = membersArray.getJsonObject(membersIndex);
-                                    String type = membersObj.get("type").toString().replace("\"", "");
-
-                                    String value = null;
-                                    if(type.equals("REFERENCE")||type.equals("GROUP")||type.equals("SERVICE")){
-                                        value = membersObj.get("name").toString();
-                                    } else if (type.equalsIgnoreCase("ANY")){
-                                        value = null;
-                                    } else {
-                                        value = membersObj.get("value").toString();
-                                    }
-
-                                    if(value != null){
-                                        value = value.replace("\"", "");
-                                    }
-
-                                    if (name != null) {
-                                        name = name.concat(",").concat(value);
-                                    } else {
-                                        name = value;
-                                    }
-                                }
-                                String nameInsert = "'"+name+"'";
-                                GroupServiceList groupServiceEntry = new GroupServiceList();
-                                groupServiceEntry.setGroupName(groupName);
-                                groupServiceEntry.setServiceList(nameInsert);
-                                dbConnection.save(groupServiceEntry);
+                                saveGroupServiceListTableToDb(dbConnection, groupName, membersArray);
                             } else { //Insert JSON data serviceGroup table, protocollist table, and portlist table
                                 String type = svcGroupListobj.get("type").toString().replace('"', '\'');
                                 String transportProtocol = svcGroupListobj.get("transportProtocol").toString().replace('"', '\'');
@@ -1124,29 +1021,16 @@ public class FirewallConfigPolicy extends Policy {
                                     dbConnection.delete(serviceEntry);
                                 }
 
-                                ServiceList serviceListEntry = new ServiceList();
-                                serviceListEntry.setServiceName(groupName);
-                                serviceListEntry.setServiceDescription(description);
-                                serviceListEntry.setServiceType(type);
-                                serviceListEntry.setServiceTransProtocol(transportProtocol);
-                                serviceListEntry.setServiceAppProtocol("null");
-                                serviceListEntry.setServicePorts(ports);
-                                dbConnection.save(serviceListEntry);
+                                saveServiceListToDb(dbConnection, groupName, description, type, transportProtocol, ports);
 
                                 List<Object> protocolResult = dbConnection.getDataById(ProtocolList.class, "protocolName", transportProtocol);
                                 if(protocolResult == null || protocolResult.isEmpty()){
-                                    ProtocolList protocolEntry = new ProtocolList();
-                                    protocolEntry.setProtocolName(transportProtocol);
-                                    protocolEntry.setDescription(transportProtocol);
-                                    dbConnection.save(protocolEntry);
+                                    saveProtocolListToDb(dbConnection, transportProtocol);
                                 }
 
                                 List<Object> portResult = dbConnection.getDataById(PortList.class, "portName", ports);
                                 if(portResult == null || portResult.isEmpty()){
-                                    PortList portEntry = new PortList();
-                                    portEntry.setPortName(ports);
-                                    portEntry.setDescription(ports);
-                                    dbConnection.save(portEntry);
+                                    savePortListToDb(dbConnection, ports);
                                 }
                             }
                         }
@@ -1179,24 +1063,7 @@ public class FirewallConfigPolicy extends Policy {
                                 JsonObject membersObj = membersArray.getJsonObject(membersIndex);
                                 type = membersObj.get("type").toString().replace("\"", "");
 
-                                String value = null;
-                                if(type.equals("REFERENCE")||type.equals("GROUP")||type.equals("SERVICE")){
-                                    value = membersObj.get("name").toString();
-                                } else if (type.equalsIgnoreCase("ANY")){
-                                    value = null;
-                                } else {
-                                    value = membersObj.get("value").toString();
-                                }
-
-                                if(value != null){
-                                    value = value.replace("\"", "");
-                                }
-
-                                if (prefixIP != null) {
-                                    prefixIP = prefixIP.concat(",").concat(value);
-                                } else {
-                                    prefixIP = value;
-                                }
+                                prefixIP = getName(prefixIP, membersObj, type);
                             }
 
                             String prefixList = "'"+prefixIP+"'";
@@ -1208,51 +1075,19 @@ public class FirewallConfigPolicy extends Policy {
                                     AddressGroup addressGroupEntry = (AddressGroup) result.get(0);
                                     dbConnection.delete(addressGroupEntry);
                                 }
-                                AddressGroup newAddressGroup = new AddressGroup();
-                                newAddressGroup.setGroupName(addressGroupName);
-                                newAddressGroup.setDescription(description);
-                                newAddressGroup.setServiceList(prefixList);
-                                dbConnection.save(newAddressGroup);
+                                saveAddressGroupToDb(dbConnection, addressGroupName, description, prefixList);
                             } else {
                                 List<Object> result = dbConnection.getDataById(PrefixList.class, "prefixListName", addressGroupName);
                                 if(result != null && !result.isEmpty()){
                                     PrefixList prefixListEntry = (PrefixList) result.get(0);
                                     dbConnection.delete(prefixListEntry);
                                 }
-                                PrefixList newPrefixList = new PrefixList();
-                                newPrefixList.setPrefixListName(addressGroupName);
-                                newPrefixList.setDescription(description);
-                                newPrefixList.setPrefixListValue(prefixList);
-                                dbConnection.save(newPrefixList);
+                                savePrefixListToDb(dbConnection, addressGroupName, description, prefixList);
                             }
                         }
                     }
                 }
-
-                /*
-                 * Remove duplicate values from 'lookup' dictionary tables
-                 */
-                //ProtocolList Table
-                String protoDelete = "DELETE FROM protocollist USING protocollist, protocollist p1 "
-                        + "WHERE protocollist.id > p1.id AND protocollist.protocolname = p1.protocolname;";
-                dbConnection.updateQuery(protoDelete);
-
-                //PortList Table
-                String portListDelete = "DELETE FROM portlist USING portlist, portlist p1 "
-                        + "WHERE portlist.id > p1.id AND portlist.portname = p1.portname; ";
-                dbConnection.updateQuery(portListDelete);
-
-                //PrefixList Table
-                String prefixListDelete = "DELETE FROM prefixlist USING prefixlist, prefixlist p1 "
-                        + "WHERE prefixlist.id > p1.id AND prefixlist.pl_name = p1.pl_name AND "
-                        + "prefixlist.pl_value = p1.pl_value AND prefixlist.description = p1.description; ";
-                dbConnection.updateQuery(prefixListDelete);
-
-                //GroupServiceList
-                String groupServiceDelete = "DELETE FROM groupservicelist USING groupservicelist, groupservicelist g1 "
-                        + "WHERE groupservicelist.id > g1.id AND groupservicelist.name = g1.name AND "
-                        + "groupservicelist.serviceList = g1.serviceList; ";
-                dbConnection.updateQuery(groupServiceDelete);
+                removeDuplicateValuesFromLookup(dbConnection);
             }catch (Exception e) {
                 PolicyLogger.error(MessageCodes.EXCEPTION_ERROR, e, "FirewallConfigPolicy", "Exception executing Firewall queries");
                 return false;
@@ -1264,6 +1099,54 @@ public class FirewallConfigPolicy extends Policy {
 
     }
 
+    private void saveActionListToDb(CommonClassDaoImpl dbConnection, String action) {
+        ActionList actionEntry = new ActionList();
+        actionEntry.setActionName(action);
+        actionEntry.setDescription(action);
+        dbConnection.save(actionEntry);
+    }
+
+    private void savePortListToDb(CommonClassDaoImpl dbConnection, String ports) {
+        PortList portEntry = new PortList();
+        portEntry.setPortName(ports);
+        portEntry.setDescription(ports);
+        dbConnection.save(portEntry);
+    }
+
+    private void saveProtocolListToDb(CommonClassDaoImpl dbConnection, String transportProtocol) {
+        ProtocolList protocolEntry = new ProtocolList();
+        protocolEntry.setProtocolName(transportProtocol);
+        protocolEntry.setDescription(transportProtocol);
+        dbConnection.save(protocolEntry);
+    }
+
+    private void saveServiceListToDb(CommonClassDaoImpl dbConnection, String groupName, String description, String type, String transportProtocol, String ports) {
+        ServiceList serviceListEntry = new ServiceList();
+        serviceListEntry.setServiceName(groupName);
+        serviceListEntry.setServiceDescription(description);
+        serviceListEntry.setServiceType(type);
+        serviceListEntry.setServiceTransProtocol(transportProtocol);
+        serviceListEntry.setServiceAppProtocol("null");
+        serviceListEntry.setServicePorts(ports);
+        dbConnection.save(serviceListEntry);
+    }
+
+    private void savePrefixListToDb(CommonClassDaoImpl dbConnection, String addressGroupName, String description, String prefixList) {
+        PrefixList newPrefixList = new PrefixList();
+        newPrefixList.setPrefixListName(addressGroupName);
+        newPrefixList.setDescription(description);
+        newPrefixList.setPrefixListValue(prefixList);
+        dbConnection.save(newPrefixList);
+    }
+
+    private void saveAddressGroupToDb(CommonClassDaoImpl dbConnection, String addressGroupName, String description, String prefixList) {
+        AddressGroup newAddressGroup = new AddressGroup();
+        newAddressGroup.setGroupName(addressGroupName);
+        newAddressGroup.setDescription(description);
+        newAddressGroup.setServiceList(prefixList);
+        dbConnection.save(newAddressGroup);
+    }
+
     private JsonObject stringToJson(String jsonString) {
         //Read jsonBody to JsonObject
         StringReader in = new StringReader(jsonString);
@@ -1291,4 +1174,4 @@ public class FirewallConfigPolicy extends Policy {
         return policyAdapter.getPolicyData();
     }
 
-}
\ No newline at end of file
+}