# Path: sts/util/console.py
# class msg():
#   global_io_master = None
# 
#   BEGIN = '\033[1;'
#   END = '\033[1;m'
# 
#   GRAY, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, CRIMSON = map(lambda num: str(num) + "m", range(30, 39))
#   B_BLACK, B_RED, B_GREEN, B_YELLOW, B_BLUE, B_MAGENTA, B_CYAN, B_GRAY, B_CRIMSON =  map(lambda num: str(num) + "m", range(40, 49))
# 
#   @staticmethod
#   def interactive(message):
#     # todo: would be nice to simply give logger a color arg, but that doesn't exist...
#     print msg.BEGIN + msg.WHITE + message + msg.END
# 
#   @staticmethod
#   def event(message):
#     print msg.BEGIN + msg.CYAN + message + msg.END
# 
#   @staticmethod
#   def openflow_event(message):
#     print msg.BEGIN + msg.BLUE + message + msg.END
# 
#   @staticmethod
#   def special_event(message):
#     print msg.BEGIN + msg.MAGENTA + message + msg.END
# 
#   @staticmethod
#   def replay_event_success(message):
#     print msg.BEGIN + msg.CYAN + msg.BEGIN + msg.B_BLUE + message + msg.END
# 
#   @staticmethod
#   def replay_event_timeout(message):
#     print msg.BEGIN + msg.MAGENTA + msg.BEGIN + msg.B_RED + message + msg.END
# 
#   @staticmethod
#   def mcs_event(message):
#     print msg.BEGIN + msg.B_MAGENTA + message + msg.END
# 
#   @staticmethod
#   def raw_input(message):
#     prompt = msg.BEGIN + msg.WHITE + message + msg.END
#     if msg.global_io_master:
#       s = msg.global_io_master.raw_input(prompt)
#     else:
#       s = raw_input(prompt)
# 
#     if s == "debug":
#       import pdb
#       pdb.set_trace()
#     return s
# 
#   @staticmethod
#   def success(message):
#     print msg.BEGIN + msg.B_GREEN + msg.BEGIN + msg.WHITE + message + msg.END
# 
#   @staticmethod
#   def fail(message):
#     print msg.BEGIN + msg.B_RED + msg.BEGIN + msg.WHITE + message + msg.END
# 
#   @staticmethod
#   def set_io_master(io_master):
#     msg.global_io_master = io_master
# 
#   @staticmethod
#   def unset_io_master():
#     msg.global_io_master = None


from pox.openflow.libopenflow_01 import *
from entities import *
from sts.util.console import msg
from collections import defaultdict
    from config_parser.openflow_parser import get_uniq_port_id
    from config_parser.openflow_parser import get_uniq_port_id
  from config_parser.openflow_parser import get_uniq_port_id
import logging
import collections
import time
    import topology_loader.topology_loader as hsa_topo
    import headerspace.applications as hsa
    import headerspace.applications as hsa
    import topology_loader.topology_loader as hsa_topo
    import headerspace.applications as hsa
    import topology_loader.topology_loader as hsa_topo
    import headerspace.applications as hsa
    import headerspace.applications as hsa
    import topology_loader.topology_loader as hsa_topo
    import headerspace.applications as hsa
    import topology_loader.topology_loader as hsa_topo
  '''
  Tracks all invariant violations and decides whether each one is transient or persistent
  '''
  def __init__(self, persistence_threshold=0, buffer_persistent_violations=False):
    '''
    persistence_threshold: number of logical time units a violation must persist
      beyond the initial detection round before we declare that it is a persistent
      violation. If zero, return any violation as a persistent violation.

    buffer_persistent_violations: if false, ViolationTracker returns as soon as a
      persistent violation is detected, even if there other related violations that
      would have become persistent if allowed a few more rounds. Setting this flag
      to true causes ViolationTracker to wait for another persistence_threshold
      before returning persistent violations

    violation2time: key is the violation signature (string), and value is a two-tuple
      (start_time, end_time), where start_time is the logical time at which the violation
      is first observed, and end time that at which the violation is last observed
    '''
    # TODO(cs): persistence_threshold should be specified *per invariant*. For
    # example, there are some invariants such as "controller's should not
    # crash" that should never be violated.
    self.persistence_threshold = persistence_threshold
    self.violation2time = {}
    self.buffer_persistent_violations = buffer_persistent_violations

  def track(self, violations, logical_time):
    # First, untrack violations that expire
    for v in self.violation2time.keys():
      if v not in violations:

# Path: sts/util/console.py
# class msg():
#   global_io_master = None
# 
#   BEGIN = '\033[1;'
#   END = '\033[1;m'
# 
#   GRAY, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, CRIMSON = map(lambda num: str(num) + "m", range(30, 39))
#   B_BLACK, B_RED, B_GREEN, B_YELLOW, B_BLUE, B_MAGENTA, B_CYAN, B_GRAY, B_CRIMSON =  map(lambda num: str(num) + "m", range(40, 49))
# 
#   @staticmethod
#   def interactive(message):
#     # todo: would be nice to simply give logger a color arg, but that doesn't exist...
#     print msg.BEGIN + msg.WHITE + message + msg.END
# 
#   @staticmethod
#   def event(message):
#     print msg.BEGIN + msg.CYAN + message + msg.END
# 
#   @staticmethod
#   def openflow_event(message):
#     print msg.BEGIN + msg.BLUE + message + msg.END
# 
#   @staticmethod
#   def special_event(message):
#     print msg.BEGIN + msg.MAGENTA + message + msg.END
# 
#   @staticmethod
#   def replay_event_success(message):
#     print msg.BEGIN + msg.CYAN + msg.BEGIN + msg.B_BLUE + message + msg.END
# 
#   @staticmethod
#   def replay_event_timeout(message):
#     print msg.BEGIN + msg.MAGENTA + msg.BEGIN + msg.B_RED + message + msg.END
# 
#   @staticmethod
#   def mcs_event(message):
#     print msg.BEGIN + msg.B_MAGENTA + message + msg.END
# 
#   @staticmethod
#   def raw_input(message):
#     prompt = msg.BEGIN + msg.WHITE + message + msg.END
#     if msg.global_io_master:
#       s = msg.global_io_master.raw_input(prompt)
#     else:
#       s = raw_input(prompt)
# 
#     if s == "debug":
#       import pdb
#       pdb.set_trace()
#     return s
# 
#   @staticmethod
#   def success(message):
#     print msg.BEGIN + msg.B_GREEN + msg.BEGIN + msg.WHITE + message + msg.END
# 
#   @staticmethod
#   def fail(message):
#     print msg.BEGIN + msg.B_RED + msg.BEGIN + msg.WHITE + message + msg.END
# 
#   @staticmethod
#   def set_io_master(io_master):
#     msg.global_io_master = io_master
# 
#   @staticmethod
#   def unset_io_master():
#     msg.global_io_master = None


# Path: sts/invariant_checker.py
from pox.openflow.libopenflow_01 import *
from entities import *
from sts.util.console import msg
from collections import defaultdict
    from config_parser.openflow_parser import get_uniq_port_id
    from config_parser.openflow_parser import get_uniq_port_id
  from config_parser.openflow_parser import get_uniq_port_id
import logging
import collections
import time
    import topology_loader.topology_loader as hsa_topo
    import headerspace.applications as hsa
    import headerspace.applications as hsa
    import topology_loader.topology_loader as hsa_topo
    import headerspace.applications as hsa
    import topology_loader.topology_loader as hsa_topo
    import headerspace.applications as hsa
    import headerspace.applications as hsa
    import topology_loader.topology_loader as hsa_topo
    import headerspace.applications as hsa
    import topology_loader.topology_loader as hsa_topo

  '''
  Tracks all invariant violations and decides whether each one is transient or persistent
  '''
  def __init__(self, persistence_threshold=0, buffer_persistent_violations=False):
    '''
    persistence_threshold: number of logical time units a violation must persist
      beyond the initial detection round before we declare that it is a persistent
      violation. If zero, return any violation as a persistent violation.

    buffer_persistent_violations: if false, ViolationTracker returns as soon as a
      persistent violation is detected, even if there other related violations that
      would have become persistent if allowed a few more rounds. Setting this flag
      to true causes ViolationTracker to wait for another persistence_threshold
      before returning persistent violations

    violation2time: key is the violation signature (string), and value is a two-tuple
      (start_time, end_time), where start_time is the logical time at which the violation
      is first observed, and end time that at which the violation is last observed
    '''
    # TODO(cs): persistence_threshold should be specified *per invariant*. For
    # example, there are some invariants such as "controller's should not
    # crash" that should never be violated.
    self.persistence_threshold = persistence_threshold
    self.violation2time = {}
    self.buffer_persistent_violations = buffer_persistent_violations

  def track(self, violations, logical_time):
    # First, untrack violations that expire
    for v in self.violation2time.keys():
      if v not in violations:

        msg.success("Violation %s turns out to be transient!" % v)