<commit_before>
# Nom nom nom nom

'''
If the user does not specify a topology to test on, use by default a full mesh
of switches, with one host connected to each switch. For example, with N = 3:

              controller
     host1                         host2
       |                            |
     switch1-(1)------------(3)--switch2
        \                       /
        (2)                   (4)
          \                   /
           \                 /
            \               /
             (6)-switch3-(5)
                    |
                  host3
'''

from debugger_entities import FuzzSwitchImpl, Link, Host, HostInterface, AccessLink
from pox.openflow.switch_impl import ofp_phy_port, DpPacketOut, SwitchImpl
from pox.lib.addresses import EthAddr, IPAddr
from pox.openflow.libopenflow_01 import *
from pox.lib.util import connect_socket_with_backoff
from pox.lib.revent import EventMixin
import socket
import time
import errno
import sys
import itertools
import logging

logger = logging.getLogger("sts.topology_generator")

def create_switch(switch_id, num_ports):
  ports = []
  for port_no in range(1, num_ports+1):
    port = ofp_phy_port( port_no=port_no,
                          hw_addr=EthAddr("00:00:00:00:%02x:%02x" % (switch_id, port_no)) )
    # monkey patch an IP address onto the port for anteater purposes
    port.ip_addr = "1.1.%d.%d" % (switch_id, port_no)
    ports.append(port)

  def unitialized_io_worker(switch):
    raise SystemError("Not initialialized")

  return FuzzSwitchImpl(create_io_worker=unitialized_io_worker, dpid=switch_id, name="SoftSwitch(%d)" % switch_id, ports=ports)

def get_switchs_host_port(switch):
  ''' return the switch's ofp_phy_port connected to the host '''
  # We wire up the last port of the switch to the host
  return switch.ports[sorted(switch.ports.keys())[-1]]

def create_host(ingress_switch_or_switches, mac_or_macs=None, ip_or_ips=None, get_switch_port=get_switchs_host_port):
  ''' Create a Host, wired up to the given ingress switches '''
  switches = ingress_switch_or_switches
  if type(switches) != list:
    switches = [ingress_switch_or_switches]

  macs = mac_or_macs
  if mac_or_macs and type(mac_or_macs) != list:
    macs = [mac_or_macs]

  ips = ip_or_ips
  if ip_or_ips and type(ip_or_ips) != list:
    ips = [ip_or_ips]

  interfaces = []
  interface_switch_pairs = []
  for switch in switches:
    port = get_switch_port(switch)
    if macs:
      mac = macs.pop(0)
    else:
      mac = EthAddr("12:34:56:78:%02x:%02x" % (switch.dpid, port.port_no))
    if ips:
      ip_addr = ips.pop(0)
    else:
      ip_addr = IPAddr("172.16.58.3") #hard coded for measurement (irrelevant)

    name = "eth%d" % switch.dpid
    interface = HostInterface(mac, ip_addr, name)
    interface_switch_pairs.append((interface, switch))
    interfaces.append(interface)

  name = "host:" + ",".join(map(lambda interface: "%s" % str(interface.ips), interfaces)) 
  host = Host(interfaces, name)
  access_links = [ AccessLink(host, interface, switch, get_switch_port(switch)) for interface, switch in interface_switch_pairs ]
  return (host, access_links)

 
class TopologyGenerator(object):
  def __init__(self):
    self.connections_per_switch = 1
    self.fat_tree = False

  def create_mesh(self, num_switches):
    ''' Returns (patch_panel, switches, network_links, hosts, access_links) '''

    # Every switch has a link to every other switch + 1 host, for N*(N-1)+N = N^2 total ports
    ports_per_switch = (num_switches - 1) + 1

    # Initialize switches
    switches = [ create_switch(switch_id, ports_per_switch) for switch_id in range(1, num_switches+1) ]
    host_access_link_pairs = [ create_host(switch) for switch in switches ]
    hosts = map(lambda pair: pair[0], host_access_link_pairs)
    access_link_list_list = map(lambda pair: pair[1], host_access_link_pairs)
    # this is python's .flatten:
    access_links = list(itertools.chain.from_iterable(access_link_list_list))

    # grab a fully meshed patch panel to wire up these guys
    link_topology = FullyMeshedLinks(switches, access_links)
    patch_panel = BufferedPatchPanel(switches, hosts, link_topology.get_connected_port)
    network_links = link_topology.get_network_links()
<commit_msg>adding a topology object for getting rid of the state tuple<commit_after>
# Nom nom nom nom

'''
If the user does not specify a topology to test on, use by default a full mesh
of switches, with one host connected to each switch. For example, with N = 3:

              controller
     host1                         host2
       |                            |
     switch1-(1)------------(3)--switch2
        \                       /
        (2)                   (4)
          \                   /
           \                 /
            \               /
             (6)-switch3-(5)
                    |
                  host3
'''

from debugger_entities import FuzzSwitchImpl, Link, Host, HostInterface, AccessLink
from pox.openflow.switch_impl import ofp_phy_port, DpPacketOut, SwitchImpl
from pox.lib.addresses import EthAddr, IPAddr
from pox.openflow.libopenflow_01 import *
from pox.lib.util import connect_socket_with_backoff
from pox.lib.revent import EventMixin
import socket
import time
import errno
import sys
import itertools
import logging

logger = logging.getLogger("sts.topology_generator")

def create_switch(switch_id, num_ports):
  ports = []
  for port_no in range(1, num_ports+1):
    port = ofp_phy_port( port_no=port_no,
                          hw_addr=EthAddr("00:00:00:00:%02x:%02x" % (switch_id, port_no)) )
    # monkey patch an IP address onto the port for anteater purposes
    port.ip_addr = "1.1.%d.%d" % (switch_id, port_no)
    ports.append(port)

  def unitialized_io_worker(switch):
    raise SystemError("Not initialialized")

  return FuzzSwitchImpl(create_io_worker=unitialized_io_worker, dpid=switch_id, name="SoftSwitch(%d)" % switch_id, ports=ports)

def get_switchs_host_port(switch):
  ''' return the switch's ofp_phy_port connected to the host '''
  # We wire up the last port of the switch to the host
  return switch.ports[sorted(switch.ports.keys())[-1]]

def create_host(ingress_switch_or_switches, mac_or_macs=None, ip_or_ips=None, get_switch_port=get_switchs_host_port):
  ''' Create a Host, wired up to the given ingress switches '''
  switches = ingress_switch_or_switches
  if type(switches) != list:
    switches = [ingress_switch_or_switches]

  macs = mac_or_macs
  if mac_or_macs and type(mac_or_macs) != list:
    macs = [mac_or_macs]

  ips = ip_or_ips
  if ip_or_ips and type(ip_or_ips) != list:
    ips = [ip_or_ips]

  interfaces = []
  interface_switch_pairs = []
  for switch in switches:
    port = get_switch_port(switch)
    if macs:
      mac = macs.pop(0)
    else:
      mac = EthAddr("12:34:56:78:%02x:%02x" % (switch.dpid, port.port_no))
    if ips:
      ip_addr = ips.pop(0)
    else:
      ip_addr = IPAddr("172.16.58.3") #hard coded for measurement (irrelevant)

    name = "eth%d" % switch.dpid
    interface = HostInterface(mac, ip_addr, name)
    interface_switch_pairs.append((interface, switch))
    interfaces.append(interface)

  name = "host:" + ",".join(map(lambda interface: "%s" % str(interface.ips), interfaces)) 
  host = Host(interfaces, name)
  access_links = [ AccessLink(host, interface, switch, get_switch_port(switch)) for interface, switch in interface_switch_pairs ]
  return (host, access_links)

class Topology(object):
  def __init__(self, patch_panel, switches, network_links, hosts, access_links)
    self.patch_panel = patch_panel
    self.switches = switches
    self.network_links = network_links
    self.hosts = hosts
    self.access_links = access_links

  # TODO add a bunch of other methods to kill switches and stuff like that
 
class TopologyGenerator(object):
  def __init__(self):
    self.connections_per_switch = 1

  def create_mesh(self, num_switches):
    ''' Returns (patch_panel, switches, network_links, hosts, access_links) '''

    # Every switch has a link to every other switch + 1 host, for N*(N-1)+N = N^2 total ports
    ports_per_switch = (num_switches - 1) + 1

    # Initialize switches
    switches = [ create_switch(switch_id, ports_per_switch) for switch_id in range(1, num_switches+1) ]
    host_access_link_pairs = [ create_host(switch) for switch in switches ]
    hosts = map(lambda pair: pair[0], host_access_link_pairs)
    access_link_list_list = map(lambda pair: pair[1], host_access_link_pairs)
    # this is python's .flatten:
    access_links = list(itertools.chain.from_iterable(access_link_list_list))

    # grab a fully meshed patch panel to wire up these guys
    link_topology = FullyMeshedLinks(switches, access_links)
    patch_panel = BufferedPatchPanel(switches, hosts, link_topology.get_connected_port)
    network_links = link_topology.get_network_links()

{'fraction_non_alphanumeric': 0.09825548677546427, 'fraction_numerical': 0.014406302757456387, 'mean_word_length': 3.215370018975332, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '88566', 'n_tokens_mistral': 2973, 'n_tokens_neox': 2848, 'n_words': 731}