def test_from_json(self):
   # Arrange
   sw1 = mock.Mock()
   sw1.dpid = 1
   sw1.to_json.return_value = 1
   sw2 = mock.Mock()
   sw2.dpid = 2
   sw2.to_json.return_value = 2
   # It's really hard to mock this, because of using assert_type
   p1 = ofp_phy_port(port_no=1)
   p2 = ofp_phy_port(port_no=2)
   sw1.ports = [p1]
   sw2.ports = [p2]
   json_dict = {'start_port': {'hw_addr': '00:00:00:00:00:00',
                               'curr': 0, 'name': '',
                               'supported': 0,
                               '__type__': 'pox.openflow.libopenflow_01.ofp_phy_port',
                               'state': 0, 'advertised': 0, 'peer': 0,
                               'config': 0, 'port_no': 1},
                'start_node': 1,
                'end_port': {'hw_addr': '00:00:00:00:00:00', 'curr': 0,
                             'name': '', 'supported': 0,
                             '__type__': 'pox.openflow.libopenflow_01.ofp_phy_port',
                             'state': 0, 'advertised': 0, 'peer': 0,
                             'config': 0, 'port_no': 2},
                '__type__': 'sts.entities.sts_entities.Link',
                'end_node': 2}
   # Act
   link = Link.from_json(json_dict)
   # Assert
   self.assertEquals(link.start_node, json_dict['start_node'])
   self.assertEquals(link.start_port.port_no, json_dict['start_port']['port_no'])
   self.assertEquals(link.end_node, json_dict['end_node'])
   self.assertEquals(link.end_port.port_no, json_dict['end_port']['port_no'])
 def test_from_json(self):
     # Arrange
     sw1 = mock.Mock()
     sw1.dpid = 1
     sw1.to_json.return_value = 1
     sw2 = mock.Mock()
     sw2.dpid = 2
     sw2.to_json.return_value = 2
     # It's really hard to mock this, because of using assert_type
     p1 = ofp_phy_port(port_no=1)
     p2 = ofp_phy_port(port_no=2)
     sw1.ports = [p1]
     sw2.ports = [p2]
     json_dict = {
         'start_port': {
             'hw_addr': '00:00:00:00:00:00',
             'curr': 0,
             'name': '',
             'supported': 0,
             '__type__': 'pox.openflow.libopenflow_01.ofp_phy_port',
             'state': 0,
             'advertised': 0,
             'peer': 0,
             'config': 0,
             'port_no': 1
         },
         'start_node': 1,
         'end_port': {
             'hw_addr': '00:00:00:00:00:00',
             'curr': 0,
             'name': '',
             'supported': 0,
             '__type__': 'pox.openflow.libopenflow_01.ofp_phy_port',
             'state': 0,
             'advertised': 0,
             'peer': 0,
             'config': 0,
             'port_no': 2
         },
         '__type__': 'sts.entities.sts_entities.Link',
         'end_node': 2
     }
     # Act
     link = Link.from_json(json_dict)
     # Assert
     self.assertEquals(link.start_node, json_dict['start_node'])
     self.assertEquals(link.start_port.port_no,
                       json_dict['start_port']['port_no'])
     self.assertEquals(link.end_node, json_dict['end_node'])
     self.assertEquals(link.end_port.port_no,
                       json_dict['end_port']['port_no'])
    def test_eq(self, SwitchCls):
        sw1 = mock.MagicMock()
        sw1.dpid = 1
        sw2 = SwitchCls()
        sw2.dpid = 2
        # It's really hard to mock this, because of using assert_type
        p1 = ofp_phy_port(port_no=1)
        p2 = ofp_phy_port(port_no=2)
        sw1.ports = [p1]
        sw2.ports = [p2]
        link1 = Link(sw1, p1, sw2, p2)
        link2 = Link(sw2, p2, sw1, p1)

        self.assertEquals(link1, link1)
        self.assertNotEquals(link1, link2)
  def test_eq(self, SwitchCls):
    sw1 = mock.MagicMock()
    sw1.dpid = 1
    sw2 = SwitchCls()
    sw2.dpid = 2
    # It's really hard to mock this, because of using assert_type
    p1 = ofp_phy_port(port_no=1)
    p2 = ofp_phy_port(port_no=2)
    sw1.ports = [p1]
    sw2.ports = [p2]
    link1 = Link(sw1, p1, sw2, p2)
    link2 = Link(sw2, p2, sw1, p1)

    self.assertEquals(link1, link1)
    self.assertNotEquals(link1, link2)
  def test_init(self, SwitchCls):
    sw1 = mock.MagicMock()
    sw1.dpid = 1
    sw2 = SwitchCls()
    sw2.dpid = 2
    # It's really hard to mock this, because of using assert_type
    p1 = ofp_phy_port(port_no=1)
    p2 = ofp_phy_port(port_no=2)
    sw1.ports = [p1]
    sw2.ports = [p2]
    link = Link(sw1, p1, sw2, p2)

    self.assertEquals(sw1.dpid, link.start_software_switch.dpid)
    self.assertEquals(sw2.dpid, link.end_software_switch.dpid)
    self.assertEquals(p1, link.start_port)
    self.assertEquals(p2, link.end_port)
    def test_to_jsont(self, SwitchCls):
        # Arrange
        json_dict = {
            "node1": {
                "hid":
                1,
                "interfaces": [{
                    "hw_addr": "11:22:33:44:55:66",
                    "ips": ["127.0.0.1"],
                    "__type__": "sts.entities.hosts.HostInterface",
                    "name": "eth0"
                }],
                "__type__":
                "sts.entities.hosts.Host",
                "name":
                "h1"
            },
            "port2": {
                "hw_addr": "00:00:00:00:00:00",
                "curr": 0,
                "name": "",
                "supported": 0,
                "__type__": "pox.openflow.libopenflow_01.ofp_phy_port",
                "state": 0,
                "advertised": 0,
                "peer": 0,
                "config": 0,
                "port_no": 1
            },
            "node2": 1,
            "__type__": "sts.entities.sts_entities.AccessLink",
            "port1": {
                "hw_addr": "11:22:33:44:55:66",
                "ips": ["127.0.0.1"],
                "__type__": "sts.entities.hosts.HostInterface",
                "name": "eth0"
            }
        }

        sw1 = SwitchCls()
        sw1.dpid = 1
        sw1.to_json.return_value = 1
        # It's really hard to mock this, because of using assert_type
        p1 = ofp_phy_port(port_no=1)
        hw_addr_str = "11:22:33:44:55:66"
        hw_addr = EthAddr(hw_addr_str)
        ip_str = "127.0.0.1"
        ip = IPAddr(ip_str)
        ifname = "eth0"
        interface = HostInterface(hw_addr, ip, name=ifname)
        hname = "h1"
        hid = 1
        host = Host(interface, name=hname, hid=hid)
        # Act
        link = AccessLink.from_json(json_dict)
        # Assert
        self.assertEquals(link.host.to_json(), host.to_json())
        self.assertEquals(link.interface.to_json(), interface.to_json())
        self.assertEquals(link.switch, 1)
        self.assertEquals(link.switch_port.to_json(), p1.to_json())
Beispiel #7
0
  def up (event):
    devs = pxpcap.PCap.get_devices()
    ports = _ports.split(",")
    phys = []
    portnum = 1
    if len(ports) == 1 and ports[0] == '': ports = []
    for p in list(ports):
      if p not in devs:
        log.error("Device %s not available -- ignoring", p)
        continue
      dev = devs[p]
      if dev.get('addrs',{}).get('ethernet',{}).get('addr') is None:
        log.error("Device %s has no ethernet address -- ignoring", p)
        continue
      if dev.get('addrs',{}).get('AF_INET') != None:
        log.error("Device %s has an IP address -- ignoring", p)
        continue
      phy = of.ofp_phy_port()
      phy.port_no = portnum
      portnum += 1
      phy.hw_addr = dev['addrs']['ethernet']['addr']
      phy.name = p
      # Fill in features sort of arbitrarily
      phy.curr = of.OFPPF_10MB_HD
      phy.advertised = of.OFPPF_10MB_HD
      phy.supported = of.OFPPF_10MB_HD
      phy.peer = of.OFPPF_10MB_HD
      phys.append(phy)

    do_launch(PCapSwitch, addr, port, max_retry_delay, dpid, ports=phys)
    def test_init(self, SwitchCls):
        sw1 = mock.MagicMock()
        sw1.dpid = 1
        sw2 = SwitchCls()
        sw2.dpid = 2
        # It's really hard to mock this, because of using assert_type
        p1 = ofp_phy_port(port_no=1)
        p2 = ofp_phy_port(port_no=2)
        sw1.ports = [p1]
        sw2.ports = [p2]
        link = Link(sw1, p1, sw2, p2)

        self.assertEquals(sw1.dpid, link.start_software_switch.dpid)
        self.assertEquals(sw2.dpid, link.end_software_switch.dpid)
        self.assertEquals(p1, link.start_port)
        self.assertEquals(p2, link.end_port)
Beispiel #9
0
  def up (event):
    devs = pxpcap.PCap.get_devices()
    ports = _ports.split(",")
    phys = []
    portnum = 1
    if len(ports) == 1 and ports[0] == '': ports = []
    for p in list(ports):
      if p not in devs:
        log.error("Device %s not available -- ignoring", p)
        continue
      dev = devs[p]
      if dev.get('addrs',{}).get('ethernet',{}).get('addr') is None:
        log.error("Device %s has no ethernet address -- ignoring", p)
        continue
      if dev.get('addrs',{}).get('AF_INET') != None:
        log.error("Device %s has an IP address -- ignoring", p)
        continue
      phy = of.ofp_phy_port()
      phy.port_no = portnum
      portnum += 1
      phy.hw_addr = dev['addrs']['ethernet']['addr']
      phy.name = p
      # Fill in features sort of arbitrarily
      phy.curr = of.OFPPF_10MB_HD
      phy.advertised = of.OFPPF_10MB_HD
      phy.supported = of.OFPPF_10MB_HD
      phy.peer = of.OFPPF_10MB_HD
      phys.append(phy)

    do_launch(PCapSwitch, address, port, max_retry_delay, dpid, ports=phys,
              extra_args=extra)
Beispiel #10
0
 def test_to_json(self):
     # Arrange
     sw1 = mock.Mock()
     sw1.dpid = 1
     sw1.to_json.return_value = 1
     sw2 = mock.Mock()
     sw2.dpid = 2
     sw2.to_json.return_value = 2
     # It's really hard to mock this, because of using assert_type
     p1 = ofp_phy_port(port_no=1)
     p2 = ofp_phy_port(port_no=2)
     sw1.ports = [p1]
     sw2.ports = [p2]
     link = Link(sw1, p1, sw2, p2)
     # Act
     json_dict = link.to_json()
     # Assert
     self.assertEquals(json_dict['start_node'], 1)
     self.assertEquals(json_dict['start_port']['port_no'], 1)
     self.assertEquals(json_dict['end_node'], 2)
     self.assertEquals(json_dict['end_port']['port_no'], 2)
 def test_to_json(self):
   # Arrange
   sw1 = mock.Mock()
   sw1.dpid = 1
   sw1.to_json.return_value = 1
   sw2 = mock.Mock()
   sw2.dpid = 2
   sw2.to_json.return_value = 2
   # It's really hard to mock this, because of using assert_type
   p1 = ofp_phy_port(port_no=1)
   p2 = ofp_phy_port(port_no=2)
   sw1.ports = [p1]
   sw2.ports = [p2]
   link = Link(sw1, p1, sw2, p2)
   # Act
   json_dict = link.to_json()
   # Assert
   self.assertEquals(json_dict['start_node'], 1)
   self.assertEquals(json_dict['start_port']['port_no'], 1)
   self.assertEquals(json_dict['end_node'], 2)
   self.assertEquals(json_dict['end_port']['port_no'], 2)
Beispiel #12
0
    def add_interface(self, name, port_no=-1, on_error=None, start=False):
        if on_error is None:
            on_error = log.error

        devs = pxpcap.PCap.get_devices()
        if name not in devs:
            on_error("Device %s not available -- ignoring", name)
            return
        dev = devs[name]
        if dev.get('addrs', {}).get('ethernet', {}).get('addr') is None:
            on_error("Device %s has no ethernet address -- ignoring", name)
            return
        if dev.get('addrs', {}).get('AF_INET') != None:
            on_error("Device %s has an IP address -- ignoring", name)
            return
        for no, p in self.px.iteritems():
            if p.device == name:
                on_error("Device %s already added", name)

        if port_no == -1:
            while True:
                port_no = self._next_port
                self._next_port += 1
                if port_no not in self.ports: break

        if port_no in self.ports:
            on_error("Port %s already exists -- ignoring", port_no)
            return

        phy = of.ofp_phy_port()
        phy.port_no = port_no
        phy.hw_addr = dev['addrs']['ethernet']['addr']
        phy.name = name
        # Fill in features sort of arbitrarily
        phy.curr = of.OFPPF_10MB_HD
        phy.advertised = of.OFPPF_10MB_HD
        phy.supported = of.OFPPF_10MB_HD
        phy.peer = of.OFPPF_10MB_HD

        self.add_port(phy)

        px = pxpcap.PCap(name, callback=self._pcap_rx, start=False)
        px.port_no = phy.port_no
        self.px[phy.port_no] = px

        if start:
            px.start()

        return px
Beispiel #13
0
    def add_interface(self, name, port_no=-1, on_error=None, start=False):
        if on_error is None:
            on_error = log.error

        devs = pxpcap.PCap.get_devices()
        if name not in devs:
            on_error("Device %s not available -- ignoring", name)
            return
        dev = devs[name]
        if dev.get('addrs', {}).get('ethernet', {}).get('addr') is None:
            on_error("Device %s has no ethernet address -- ignoring", name)
            return
        if dev.get('addrs', {}).get('AF_INET') != None:
            on_error("Device %s has an IP address -- ignoring", name)
            return
        for no, p in self.px.iteritems():
            if p.device == name:
                on_error("Device %s already added", name)

        if port_no == -1:
            while True:
                port_no = self._next_port
                self._next_port += 1
                if port_no not in self.ports: break

        if port_no in self.ports:
            on_error("Port %s already exists -- ignoring", port_no)
            return

        phy = of.ofp_phy_port()
        phy.port_no = port_no
        phy.hw_addr = dev['addrs']['ethernet']['addr']
        phy.name = name
        # Fill in features sort of arbitrarily
        phy.curr = of.OFPPF_10MB_HD
        phy.advertised = of.OFPPF_10MB_HD
        phy.supported = of.OFPPF_10MB_HD
        phy.peer = of.OFPPF_10MB_HD

        self.add_port(phy)

        px = pxpcap.PCap(name, callback=self._pcap_rx, start=False)
        px.port_no = phy.port_no
        self.px[phy.port_no] = px

        if start:
            px.start()

        return px
Beispiel #14
0
def gini_get_device_ports():
    port_list = []
    port_num = _GINIC.getPortNumber()
    print("[gini_get_device_ports]This device has %d ports", port_num)
    port_tuple_list = _GINIC.getPortTuple()
    print("[gini_get_device_ports]Done!!")
    for tp in port_tuple_list:
        print("tuple: ", tp)
        port = of.ofp_phy_port()
        port.port_no = tp[0]
        port.hw_addr = of.EthAddr(tp[1])
        port.name = tp[2]
        port_list.append(copy.deepcopy(port)) # wrong way??
    print("port list is: ", port_list)
    return port_list
 def test_init(self, SwitchCls):
   # Arrange
   sw1 = SwitchCls()
   sw1.dpid = 1
   # It's really hard to mock this, because of using assert_type
   p1 = ofp_phy_port(port_no=1)
   hw_addr_str = "11:22:33:44:55:66"
   hw_addr = EthAddr(hw_addr_str)
   ip_str = "127.0.0.1"
   ip = IPAddr(ip_str)
   ifname = "eth0"
   interface = HostInterface(hw_addr, ip, name=ifname)
   hname = "h1"
   hid = 1
   host = Host(interface, name=hname, hid=hid)
   # Act
   link = AccessLink(host, interface, sw1, p1)
   # Assert
   self.assertEquals(link.host, host)
   self.assertEquals(link.interface, interface)
Beispiel #16
0
 def test_init(self, SwitchCls):
     # Arrange
     sw1 = SwitchCls()
     sw1.dpid = 1
     # It's really hard to mock this, because of using assert_type
     p1 = ofp_phy_port(port_no=1)
     hw_addr_str = "11:22:33:44:55:66"
     hw_addr = EthAddr(hw_addr_str)
     ip_str = "127.0.0.1"
     ip = IPAddr(ip_str)
     ifname = "eth0"
     interface = HostInterface(hw_addr, ip, name=ifname)
     hname = "h1"
     hid = 1
     host = Host(interface, name=hname, hid=hid)
     # Act
     link = AccessLink(host, interface, sw1, p1)
     # Assert
     self.assertEquals(link.host, host)
     self.assertEquals(link.interface, interface)
 def test_to_jsont(self, SwitchCls):
   # Arrange
   sw1 = SwitchCls()
   sw1.dpid = 1
   sw1.to_json.return_value = 1
   # It's really hard to mock this, because of using assert_type
   p1 = ofp_phy_port(port_no=1)
   hw_addr_str = "11:22:33:44:55:66"
   hw_addr = EthAddr(hw_addr_str)
   ip_str = "127.0.0.1"
   ip = IPAddr(ip_str)
   ifname = "eth0"
   interface = HostInterface(hw_addr, ip, name=ifname)
   hname = "h1"
   hid = 1
   host = Host(interface, name=hname, hid=hid)
   link = AccessLink(host, interface, sw1, p1)
   # Act
   json_dict = link.to_json()
   # Assert
   self.assertEquals(json_dict['node1'], host.to_json())
   self.assertEquals(json_dict['port1'], interface.to_json())
   self.assertEquals(json_dict['node2'], 1)
   self.assertEquals(json_dict['port2'], p1.to_json())
Beispiel #18
0
 def test_to_jsont(self, SwitchCls):
     # Arrange
     sw1 = SwitchCls()
     sw1.dpid = 1
     sw1.to_json.return_value = 1
     # It's really hard to mock this, because of using assert_type
     p1 = ofp_phy_port(port_no=1)
     hw_addr_str = "11:22:33:44:55:66"
     hw_addr = EthAddr(hw_addr_str)
     ip_str = "127.0.0.1"
     ip = IPAddr(ip_str)
     ifname = "eth0"
     interface = HostInterface(hw_addr, ip, name=ifname)
     hname = "h1"
     hid = 1
     host = Host(interface, name=hname, hid=hid)
     link = AccessLink(host, interface, sw1, p1)
     # Act
     json_dict = link.to_json()
     # Assert
     self.assertEquals(json_dict['node1'], host.to_json())
     self.assertEquals(json_dict['port1'], interface.to_json())
     self.assertEquals(json_dict['node2'], 1)
     self.assertEquals(json_dict['port2'], p1.to_json())
Beispiel #19
0
    def add_interface(self,
                      name,
                      port_no=-1,
                      on_error=None,
                      start=False,
                      virtual=False):
        """
    Add an interface

    This is usually a PCap interface, unless virtual is set.  If virtual
    is True, this creates a virtual port which isn't connected to any
    channel.  If it's a string, it's the channel name.
    """
        if self.magic_virtual_port_names:
            if name.startswith("@"):
                name = name[1:]
                virtual = True
                if "@" in name:
                    name, virtual = name.split("@", 1)

        if on_error is None:
            on_error = log.error

        phy = of.ofp_phy_port()
        phy.name = name
        # Fill in features sort of arbitrarily
        phy.curr = of.OFPPF_10MB_HD
        phy.advertised = of.OFPPF_10MB_HD
        phy.supported = of.OFPPF_10MB_HD
        phy.peer = of.OFPPF_10MB_HD

        if virtual:
            px = VirtualPort(self, phy)
            if isinstance(virtual, str):
                px.channel = virtual
        else:
            devs = pxpcap.PCap.get_devices()
            if name not in devs:
                on_error("Device %s not available -- ignoring", name)
                return
            dev = devs[name]
            if dev.get('addrs', {}).get('ethernet', {}).get('addr') is None:
                on_error("Device %s has no ethernet address -- ignoring", name)
                return
            if dev.get('addrs', {}).get('AF_INET') != None:
                on_error("Device %s has an IP address -- ignoring", name)
                return
            for no, p in self.px.iteritems():
                if p.device == name:
                    on_error("Device %s already added", name)

            phy.hw_addr = dev['addrs']['ethernet']['addr']

            px = pxpcap.PCap(name, callback=self._pcap_rx, start=False)

        if port_no == -1:
            while True:
                port_no = self._next_port
                self._next_port += 1
                if port_no not in self.ports: break

        if port_no in self.ports:
            on_error("Port %s already exists -- ignoring", port_no)
            return

        phy.port_no = port_no
        self.px[phy.port_no] = px

        if virtual:
            # We create the MAC based on the port_no, so we have to do it here
            # and not earlier.
            phy.hw_addr = self._gen_ethaddr(phy.port_no)

        self.add_port(phy)

        if start:
            px.start()

        return px
  def test_to_jsont(self, SwitchCls):
    # Arrange
    json_dict = {
      "node1": {
        "hid": 1,
        "interfaces": [
          {
            "hw_addr": "11:22:33:44:55:66",
            "ips": [
              "127.0.0.1"
            ],
            "__type__": "sts.entities.hosts.HostInterface",
            "name": "eth0"
          }
        ],
        "__type__": "sts.entities.hosts.Host",
        "name": "h1"
      },
      "port2": {
        "hw_addr": "00:00:00:00:00:00",
        "curr": 0,
        "name": "",
        "supported": 0,
        "__type__": "pox.openflow.libopenflow_01.ofp_phy_port",
        "state": 0,
        "advertised": 0,
        "peer": 0,
        "config": 0,
        "port_no": 1
      },
      "node2": 1,
      "__type__": "sts.entities.sts_entities.AccessLink",
      "port1": {
        "hw_addr": "11:22:33:44:55:66",
        "ips": [
          "127.0.0.1"
        ],
        "__type__": "sts.entities.hosts.HostInterface",
        "name": "eth0"
      }
    }

    sw1 = SwitchCls()
    sw1.dpid = 1
    sw1.to_json.return_value = 1
    # It's really hard to mock this, because of using assert_type
    p1 = ofp_phy_port(port_no=1)
    hw_addr_str = "11:22:33:44:55:66"
    hw_addr = EthAddr(hw_addr_str)
    ip_str = "127.0.0.1"
    ip = IPAddr(ip_str)
    ifname = "eth0"
    interface = HostInterface(hw_addr, ip, name=ifname)
    hname = "h1"
    hid = 1
    host = Host(interface, name=hname, hid=hid)
    # Act
    link = AccessLink.from_json(json_dict)
    # Assert
    self.assertEquals(link.host.to_json(), host.to_json())
    self.assertEquals(link.interface.to_json(), interface.to_json())
    self.assertEquals(link.switch, 1)
    self.assertEquals(link.switch_port.to_json(), p1.to_json())