def validate_ips_and_networks_for_device(self):
        """
        for every device, get the ips and networks
        Device -> Ip Addresses
        Device -> Network
        """
        layer3_catalog = dmd.ZenLinkManager.layer3_catalog
        model_catalog = IModelCatalogTool(dmd)
        failed_devices = []
        for device in get_all_devices(dmd):
            l3_query = Eq('deviceId', device.id)
            layer3_brains = layer3_catalog.evalAdvancedQuery(l3_query)
            layer3_ips = set([ brain.ipAddressId for brain in layer3_brains if brain.ipAddressId ])
            layer3_nets = set([ brain.networkId for brain in layer3_brains if brain.networkId ])

            model_catalog_query = And(Eq('deviceId', "*{0}".format(device.id)), Eq('meta_type', "IpAddress"))
            search_results = model_catalog.search(query=model_catalog_query)
            model_catalog_brains = [ brain for brain in search_results.results ]
            model_catalog_ips = set([ brain.ipAddressId for brain in model_catalog_brains if brain.ipAddressId ])
            model_catalog_nets = set([ brain.networkId for brain in model_catalog_brains if brain.networkId ])

            if not (len(layer3_ips - model_catalog_ips) == len(model_catalog_ips- layer3_ips) == 0 and \
               len(layer3_nets - model_catalog_nets) == len(model_catalog_nets- layer3_nets) == 0) :
                failed_devices.append(device)

        if failed_devices:
            print "TEST FAILED: Catalogs return different ip addresses for the following devices:"
            for dev in failed_devices:
                print "\t\t{0}".format(dev.getPrimaryId())
        else:
            print "TEST PASSED: Both catalogs returned the same ips for every device."
        return len(failed_devices) == 0
def start_ui_client():
    # Obviously not tested at all.
    while True:
        input('Press Enter to start sending a new message.\n')
        available_devices = utils.get_all_devices(TOPOLOGY, DISPLAY_NAME)
        if len(available_devices) == 0:
            print('Sorry, there are no devices in the network at this time.')
            continue

        questions = [
            inquirer.List(
                'available_devices',
                message="Choose a user to send to",
                choices=available_devices,
            )
        ]
        user_destination = inquirer.prompt(questions)['available_devices']
        message_body = input('Enter message: ')
        message = {
            'source': DISPLAY_NAME,
            'type': 'data',
            'destination': user_destination,
            'data': message_body
        }

        if not add_to_the_queue(message):
            print('Failed to deliver your message to {}.'.format(
                user_destination))
    def validate_devices_interfaces_and_mac_addresses(self):
        """
        get macs of avery device in the system with both catalogs
        """
        layer2_catalog = dmd.ZenLinkManager.layer2_catalog
        model_catalog = IModelCatalogTool(dmd)
        failed_devices = []
        for device in get_all_devices(dmd):
            device_uid = "/".join(device.getPrimaryPath())
            layer2_query = Eq('deviceId', device_uid)
            layer2_brains = layer2_catalog.evalAdvancedQuery(layer2_query)
            layer2_macs = set([ brain.macaddress for brain in layer2_brains if brain.macaddress ])
            layer2_ifaces = set([ brain.interfaceId for brain in layer2_brains if brain.interfaceId ])

            model_catalog_query = And(Eq('deviceId', "{0}".format(device_uid)), Eq('meta_type', "IpInterface"))
            search_results = model_catalog.search(query=model_catalog_query)
            model_catalog_brains = [ brain for brain in search_results.results ]
            model_catalog_macs = set([ brain.macaddress for brain in model_catalog_brains if brain.macaddress ])
            model_catalog_ifaces = set([ brain.interfaceId for brain in model_catalog_brains if brain.interfaceId ])
            if not ( len(layer2_macs - model_catalog_macs) == len(model_catalog_macs- layer2_macs) == 0 and \
                len(layer2_ifaces - model_catalog_ifaces) == len(model_catalog_ifaces- layer2_ifaces) == 0 ):
                failed_devices.append(device)

        if failed_devices:
            print "TEST FAILED: Catalogs return different mac addresses or interfaces for the following devices:"
            for dev in failed_devices:
                print "\t\t{0}".format(dev.getPrimaryId())
        else:
            print "TEST PASSED: Both catalogs returned the same mac addresses and interfaces for every device."
        return len(failed_devices) == 0
def flood_control_message(event, target_user):
    for destination in utils.get_all_devices(TOPOLOGY, DISPLAY_NAME):
        new_msg = utils.control_message(event, target_user, DISPLAY_NAME)
        new_msg['destination'] = destination
        if not add_to_the_queue(new_msg):
            details = 'Event: "{}", destination: "{}"'.format(
                event, destination)
            LOGGER.write('Failed to deliver flood message:\n' + details)