def redistributeTargets(oldTargetRules, oldAlphas, newAlphas, oldTargetList, newTargetList): totalTraffic = sumTraffic(oldTargetRules) for i, oldAlpha in enumerate(oldAlphas): for j, newAlpha in enumerate(newAlphas): if oldAlpha['replica'] == newAlpha['replica']: overAssigns = 0 if oldAlpha['alphaTarget'] == 0: overAssigns = oldAlpha['alphaAssign'] - (Globals.MAXTGTTHRESH * totalTraffic * newAlpha['alphaTarget']) else: overAssigns = oldAlpha['alphaAssign'] - (Globals.MAXTGTTHRESH * totalTraffic * newAlpha['alphaTarget'] / oldAlpha['alphaTarget']) if overAssigns > 0: for k, target in enumerate(oldTargetRules): parentRule = IPRules.getParentRule(target) matchParentRule = IPRules.findMatch(newTargetList, parentRule['ip'], parentRule['wild']) matchTargetRule = IPRules.findMatch(newTargetList, target['ip'], target['wild']) childrenRule = IPRules.genChildrenRules(target) matchChild1Rule = IPRules.findMatch(newTargetList, childrenRule[0]['ip'], childrenRule[0]['wild']) matchChild2Rule = IPRules.findMatch(newTargetList, childrenRule[1]['ip'], childrenRule[1]['wild']) if target['replica'] == oldAlpha['replica'] and overAssigns > 0 and matchTargetRule == [] and matchChild1Rule == [] and matchChild2Rule == [] and matchParentRule == [] and target['traffic'] != 0: Globals.ASSIGNLOG.write("Redistributing: " + str(target['ip']) + '/' + str(target['wild']) + ':' + str(target['replica']) + '\n') oldTargetList.append(target) newTargetList.append(target) break return (oldTargetList, newTargetList)
def determineTargets(targetList, alphas, deeperList): totalTraffic = long(sumTraffic(targetList) / numNonZero(targetList)) newTargetList = [] oldTargetList = [] Globals.ASSIGNLOG.write('Threshold: ' + str(totalTraffic * Globals.MINTGTTHRESH) + " < x < " + str(totalTraffic * Globals.MAXTGTTHRESH) + '\n') for i in range(0, len(targetList)): if targetList[i]['traffic'] > (totalTraffic * Globals.MAXTGTTHRESH) or Alphas.sumAllAlphaAssign(alphas) < Alphas.sumAllAlphaTarget(alphas): Globals.ASSIGNLOG.write('\t' + str(targetList[i]['ip']) + " Beyond Threshold " + str(targetList[i]['traffic']) + '\n') # Beyond Threshold: Need more rules childList = IPRules.findChildRules(deeperList, targetList[i]['ip'], targetList[i]['wild']) for j, child in enumerate(childList): newTargetList.append(child) if childList != []: oldTargetList.append(targetList[i]) elif targetList[i]['traffic'] < (totalTraffic * Globals.MINTGTTHRESH): Globals.ASSIGNLOG.write('\t' + str(targetList[i]['ip']) + " Under Threshold " + str(targetList[i]['traffic']) + '\n') # Below Theshold: Reduce rules (siblingIP, siblingWild) = IPRules.findSiblingRule(targetList[i]) siblingRule = IPRules.findMatch(targetList, siblingIP, siblingWild) if siblingRule != []: if siblingRule[0]['traffic'] <= (totalTraffic * Globals.MINTGTTHRESH) and \ targetList.index(siblingRule[0]) < i: parentRule = IPRules.getParentRule(siblingRule[0]) Globals.ASSIGNLOG.write('\t' + str(parentRule['ip']) + '/' + str(parentRule['wild']) + ' combined\n') newTargetList.append(parentRule) oldTargetList.append(targetList[i]) oldTargetList.append(siblingRule[0]) else: Globals.ASSIGNLOG.write('\t' + str(targetList[i]['ip']) + " Fine Threshold " + str(targetList[i]['traffic']) + '\n') if targetList[i]['replica'] == -1: newTargetList.append(targetList[i]) oldTargetList.append(targetList[i]) return (oldTargetList, newTargetList)
def handleRules(rulesList): for i, rule in enumerate(rulesList): newRules = rule['NewRules'] oldRules = rule['OldRules'] # 1:1 means simply replacement! Never should be N:N if len(newRules) == len(oldRules): oldRule = oldRules[0] newRule = newRules[0] oldIP = oldRule['ip'] oldWild = oldRule['wild'] oldTraffic = oldRule['traffic'] newIP = newRule['ip'] newWild = newRule['wild'] newReplica = newRule['replica'] Globals.TARGETRULES.remove(oldRule) oldInstalledChildren = IPRules.findChildRules( Globals.INSTALLEDRULES, oldIP, oldWild) for i, oldChildRule in enumerate(oldInstalledChildren): newChildRule = copyRule(oldChildRule) newChildRule['replica'] = newReplica actionReplaceRule([oldChildRule], [newChildRule]) newRule['traffic'] = oldTraffic Globals.TARGETRULES.append(newRule) # 1:2 install more specific targets elif len(newRules) > len(oldRules): oldRule = oldRules[0] newRule1 = newRules[0] newRule2 = newRules[1] oldIP = oldRule['ip'] oldWild = oldRule['wild'] oldReplica = oldRule['replica'] oldTraffic = oldRule['traffic'] newIP1 = newRule1['ip'] newWild1 = newRule1['wild'] newReplica1 = newRule1['replica'] newTraffic1 = long(oldTraffic / 2) newIP2 = newRule2['ip'] newWild2 = newRule2['wild'] newReplica2 = newRule2['replica'] newTraffic2 = long(oldTraffic / 2) Globals.TARGETRULES.remove(oldRule) oldInstalledChildren = IPRules.findChildRules( Globals.INSTALLEDRULES, oldIP, oldWild) for i, rule in enumerate(oldInstalledChildren): newChildren = IPRules.genChildrenRules(rule) if IPRules.findMatch([IPRules.getParentRule(newChildren[0])], newIP1, newWild1) != []: newChildren[0]['replica'] = newReplica1 newChildren[1]['replica'] = newReplica1 else: newChildren[0]['replica'] = newReplica2 newChildren[1]['replica'] = newReplica2 newChildren[0]['traffic'] = long(rule['traffic'] / 2) newChildren[1]['traffic'] = long(rule['traffic'] / 2) rule['replica'] = oldReplica actionReplaceRule([rule], newChildren) Globals.TARGETRULES.append(newRule1) Globals.TARGETRULES.append(newRule2) # 2:1 install more general targets else: newRule = newRules[0] newIP = newRule['ip'] newWild = newRule['wild'] newReplica = newRule['replica'] oldRule1 = oldRules[0] oldRule2 = oldRules[1] oldIP1 = oldRule1['ip'] oldWild1 = oldRule1['wild'] oldTraffic1 = oldRule1['traffic'] oldIP2 = oldRule2['ip'] oldWild2 = oldRule2['wild'] oldTraffic2 = oldRule2['traffic'] Globals.TARGETRULES.remove(oldRule1) Globals.TARGETRULES.remove(oldRule2) oldInstalledChildren1 = IPRules.findChildRules( Globals.INSTALLEDRULES, oldIP1, oldWild1) oldTransitionChildren1 = IPRules.findChildRules( Globals.TRANSITRULES, oldIP1, oldWild1) oldInstalledChildren1.extend(oldTransitionChildren1) oldRule1['replica'] = newReplica actionReplaceRule(oldInstalledChildren1, [oldRule1]) oldInstalledChildren2 = IPRules.findChildRules( Globals.INSTALLEDRULES, oldIP2, oldWild2) oldTransitionChildren2 = IPRules.findChildRules( Globals.TRANSITRULES, oldIP2, oldWild2) oldInstalledChildren2.extend(oldTransitionChildren2) oldRule2['replica'] = newReplica actionReplaceRule(oldInstalledChildren2, [oldRule2]) newRule['traffic'] = long(oldTraffic1 + oldTraffic2) Globals.TARGETRULES.append(newRule)
def handleRules(rulesList): for i, rule in enumerate(rulesList): newRules = rule['NewRules'] oldRules = rule['OldRules'] # 1:1 means simply replacement! Never should be N:N if len(newRules) == len(oldRules): oldRule = oldRules[0] newRule = newRules[0] oldIP = oldRule['ip'] oldWild = oldRule['wild'] oldTraffic = oldRule['traffic'] newIP = newRule['ip'] newWild = newRule['wild'] newReplica = newRule['replica'] Globals.TARGETRULES.remove(oldRule) oldInstalledChildren = IPRules.findChildRules(Globals.INSTALLEDRULES, oldIP, oldWild) for i, oldChildRule in enumerate(oldInstalledChildren): newChildRule = copyRule(oldChildRule) newChildRule['replica'] = newReplica actionReplaceRule([oldChildRule], [newChildRule]) newRule['traffic'] = oldTraffic Globals.TARGETRULES.append(newRule) # 1:2 install more specific targets elif len(newRules) > len(oldRules): oldRule = oldRules[0] newRule1 = newRules[0] newRule2 = newRules[1] oldIP = oldRule['ip'] oldWild = oldRule['wild'] oldReplica = oldRule['replica'] oldTraffic = oldRule['traffic'] newIP1 = newRule1['ip'] newWild1 = newRule1['wild'] newReplica1 = newRule1['replica'] newTraffic1 = long(oldTraffic / 2) newIP2 = newRule2['ip'] newWild2 = newRule2['wild'] newReplica2 = newRule2['replica'] newTraffic2 = long(oldTraffic / 2) Globals.TARGETRULES.remove(oldRule) oldInstalledChildren = IPRules.findChildRules(Globals.INSTALLEDRULES, oldIP, oldWild) for i, rule in enumerate(oldInstalledChildren): newChildren = IPRules.genChildrenRules(rule) if IPRules.findMatch([IPRules.getParentRule(newChildren[0])], newIP1, newWild1) != []: newChildren[0]['replica'] = newReplica1 newChildren[1]['replica'] = newReplica1 else: newChildren[0]['replica'] = newReplica2 newChildren[1]['replica'] = newReplica2 newChildren[0]['traffic'] = long(rule['traffic'] / 2) newChildren[1]['traffic'] = long(rule['traffic'] / 2) rule['replica'] = oldReplica actionReplaceRule([rule], newChildren) Globals.TARGETRULES.append(newRule1) Globals.TARGETRULES.append(newRule2) # 2:1 install more general targets else: newRule = newRules[0] newIP = newRule['ip'] newWild = newRule['wild'] newReplica = newRule['replica'] oldRule1 = oldRules[0] oldRule2 = oldRules[1] oldIP1 = oldRule1['ip'] oldWild1 = oldRule1['wild'] oldTraffic1 = oldRule1['traffic'] oldIP2 = oldRule2['ip'] oldWild2 = oldRule2['wild'] oldTraffic2 = oldRule2['traffic'] Globals.TARGETRULES.remove(oldRule1) Globals.TARGETRULES.remove(oldRule2) oldInstalledChildren1 = IPRules.findChildRules(Globals.INSTALLEDRULES, oldIP1, oldWild1) oldTransitionChildren1 = IPRules.findChildRules(Globals.TRANSITRULES, oldIP1, oldWild1) oldInstalledChildren1.extend(oldTransitionChildren1) oldRule1['replica'] = newReplica actionReplaceRule(oldInstalledChildren1, [oldRule1]) oldInstalledChildren2 = IPRules.findChildRules(Globals.INSTALLEDRULES, oldIP2, oldWild2) oldTransitionChildren2 = IPRules.findChildRules(Globals.TRANSITRULES, oldIP2, oldWild2) oldInstalledChildren2.extend(oldTransitionChildren2) oldRule2['replica'] = newReplica actionReplaceRule(oldInstalledChildren2, [oldRule2]) newRule['traffic'] = long(oldTraffic1 + oldTraffic2) Globals.TARGETRULES.append(newRule)