java - How to properly refresh a list while using it for iterating -
i have following problem. deleting firewall rules sonicwall. way rules sonic wall find matching ones, store them in list , use list in order issue command delete them. way rules delete using ssh , command id. problem having every time id deleted id's changed leading second command try , delete access rule not exist (or worse wrong one). order overcome this, after each delete process refresh list order renewed elements , new id's. problem facing update of list not correct (it happens within each loop) since list used iterator (leading wrong commands being issued).
what best , safest way update list new elements?
below part of code in question:
multiresult = expect.expect(anyof(regexp("(.*)" + promptpattern))); accessrules = getaccessrules(multiresult, accessrule); if(accessrules == null){ trc.trc("no access rules found delete"); return false; } numberofrulestodelete = accessrules.size(); int size = numberofrulestodelete; intlist deleterulesid = new intlist(); while(numberofrulestodelete != 0){ for(accessrule artodelete : accessrules){ expect.sendline("configure"); multiresult = expect.expect(anyof(regexp("(.*)" + confmodepromptpattern))); result showcmdresult = multiresult.getresults().get(0); genericcmddto output = null; objectmapper objectmapper = new objectmapper(); genericfailedcmddto failedoutput = null; string swresponse = null; swresponse = handleswconfmoderesponse(showcmdresult); output = objectmapper.readvalue(createsuccessfulljsonresponse(swresponse.getbytes()), genericcmddto.class); if(output.getsuccess()){//if change configure command successfull result confcmdresult = null; trc.trc("configure command: " + output.getsuccess()); expect.sendline("no access-rule id " + artodelete.getindex()); multiresult confresult = expect.expect(anyof(regexp("(.*)" + confmodepromptpattern))); confcmdresult = confresult.getresults().get(0); swresponse = handleswconfmoderesponse(confcmdresult); output = objectmapper.readvalue(createsuccessfulljsonresponse(swresponse.getbytes()), genericcmddto.class); if(output.getsuccess()){//if delete command successfull trc.trc("delete command issued successfully"); expect.sendline("commit"); confcmdresult = expect.expect(anyof(regexp("(.*)" + confmodepromptpattern))); swresponse = handleswconfmoderesponse(confcmdresult); output = objectmapper.readvalue(createsuccessfulljsonresponse(swresponse.getbytes()), genericcmddto.class); if(output.getsuccess()){//if commit command successfull trc.trc("commit command issued succesfully!"); trc.trc("deleted access rule: " + artodelete.getindex()); deleterulesid.addelement(artodelete.getindex()); expect.sendline("exit");//change no conf mode confcmdresult = expect.expect(anyof(regexp("(.*)" + promptpattern))); swresponse = handleswpromptmoderesponse(confcmdresult); output = objectmapper.readvalue(createsuccessfulljsonresponse(swresponse.getbytes()), genericcmddto.class); if(output.getsuccess()){//if exit command successfull trc.trc("exit command issued successfully!"); operationsuccess = output.getsuccess(); }else{//if exit unsucessfull failedoutput = objectmapper.readvalue(createfailedjsonresponse(swresponse.getbytes()), genericfailedcmddto.class); operationsuccess = output.getsuccess(); trc.trc("commit command has failed! message: " + failedoutput.getmessage() + ", code: " + failedoutput.getcode()); trc.trc("changing show mode...."); expect.sendline("exit"); multiresult = expect.expect(anyof(regexp("(.*)" + promptpattern))); showcmdresult = multiresult.getresults().get(0); swresponse = handleswpromptmoderesponse(confcmdresult); output = objectmapper.readvalue(createsuccessfulljsonresponse(swresponse.getbytes()), genericcmddto.class); if(output.getsuccess()){ trc.trc("exiting conf mode.."); trc.trc("exit command: " + output.getsuccess()); } return operationsuccess; } }else{//if commit unsucessfull failedoutput = objectmapper.readvalue(createfailedjsonresponse(swresponse.getbytes()), genericfailedcmddto.class); operationsuccess = output.getsuccess(); trc.trc("commit command has failed! message: " + failedoutput.getmessage() + ", code: " + failedoutput.getcode()); trc.trc("changing show mode...."); expect.sendline("exit"); multiresult = expect.expect(anyof(regexp("(.*)" + promptpattern))); showcmdresult = multiresult.getresults().get(0); swresponse = handleswpromptmoderesponse(showcmdresult); output = objectmapper.readvalue(createsuccessfulljsonresponse(swresponse.getbytes()), genericcmddto.class); if(output.getsuccess()){ trc.trc("exiting conf mode.."); trc.trc("exit command: " + output.getsuccess()); } return operationsuccess; } }else{//if delete unsucessfull failedoutput = objectmapper.readvalue(createfailedjsonresponse(swresponse.getbytes()), genericfailedcmddto.class); operationsuccess = output.getsuccess(); trc.trc("commit command has failed! message: " + failedoutput.getmessage() + ", code: " + failedoutput.getcode()); trc.trc("changing show mode...."); expect.sendline("exit"); multiresult = expect.expect(anyof(regexp("(.*)" + promptpattern))); showcmdresult = multiresult.getresults().get(0); swresponse = handleswpromptmoderesponse(showcmdresult); output = objectmapper.readvalue(createsuccessfulljsonresponse(swresponse.getbytes()), genericcmddto.class); if(output.getsuccess()){ trc.trc("exiting conf mode.."); trc.trc("exit command: " + output.getsuccess()); } return operationsuccess; } } else{//if configure unsuccessfull failedoutput = objectmapper.readvalue(createfailedjsonresponse(swresponse.getbytes()), genericfailedcmddto.class); operationsuccess = output.getsuccess(); trc.trc("commit command has failed! message: " + failedoutput.getmessage() + ", code: " + failedoutput.getcode()); trc.trc("changing show mode...."); expect.sendline("exit"); multiresult = expect.expect(anyof(regexp("(.*)" + promptpattern))); showcmdresult = multiresult.getresults().get(0); swresponse = handleswpromptmoderesponse(showcmdresult); output = objectmapper.readvalue(createsuccessfulljsonresponse(swresponse.getbytes()), genericcmddto.class); if(output.getsuccess()){ trc.trc("exiting conf mode.."); trc.trc("exit command: " + output.getsuccess()); } return operationsuccess; } expect.sendline("show access-rules " + accessrule.getfromzone() + " " + accessrule.gettozone()); multiresult = expect.expect(anyof(regexp("(.*)" + promptpattern))); accessrules = getaccessrules(multiresult, accessrule); if(accessrules != null){ numberofrulestodelete = accessrules.size(); }else{ trc.trc("deleting process complete!"); numberofrulestodelete = 0; for(int = 0; < deleterulesid.size(); i++){ trc.trc("deleted " + size + " rules: " + deleterulesid.getelementat(i)); } } } } return operationsuccess;
you should not update list while iterate through it.
use copy instead.
Comments
Post a Comment