def interpret_digest(self, digest_data):
        logger.debug("Digest data from switch [%s] - [%s]", self.name,
                     digest_data)

        if not digest_data or len(digest_data) == 0:
            logger.warning('No digest data to process')
            return
        for members in digest_data:
            logger.debug("Digest members: %s", members)
            if members.WhichOneof('data') == 'struct':
                source_mac = decode_mac(members.struct.members[0].bitstring)
                if source_mac:
                    logger.debug('Digest MAC Address is: %s', source_mac)
                    ingress_port = int(
                        codecs.encode(members.struct.members[1].bitstring,
                                      'hex'), 16)
                    logger.debug('Digest Ingress Port is %s', ingress_port)
                    self.add_data_forward(source_mac, ingress_port)
                else:
                    logger.warning('Could not retrieve source_mac from digest')
            else:
                logger.warning('Digest could not be processed - [%s]',
                               digest_data)

        logger.info('Completed digest processing')
    def test_convert(self):
        """
        Tests convert.py
        """
        logging.basicConfig(level=logging.DEBUG)
        mac = "aa:bb:cc:dd:ee:ff"
        enc_mac = convert.encode_mac(mac)
        self.assertEquals('\xaa\xbb\xcc\xdd\xee\xff', enc_mac)
        dec_mac = convert.decode_mac(enc_mac)
        self.assertEquals(mac, dec_mac)

        ip = "10.0.0.1"
        enc_ip = convert.encode_ipv4(ip)
        self.assertEquals(enc_ip, '\x0a\x00\x00\x01')
        dec_ip = convert.decode_ipv4(enc_ip)
        self.assertEquals(ip, dec_ip)

        num = 1337
        byte_len = 5
        enc_num = convert.encode_num(num, byte_len * 8)
        self.assertEquals(enc_num, '\x00\x00\x00\x05\x39')
        dec_num = convert.decode_num(enc_num)
        self.assertEquals(num, dec_num)

        self.assertTrue(convert.matches_ipv4('10.0.0.1'))
        self.assertFalse(convert.matches_ipv4('10.0.0.1.5'))
        self.assertFalse(convert.matches_ipv4('1000.0.0.1'))
        self.assertFalse(convert.matches_ipv4('10001'))

        ipv6 = '0000:0000:0000:0000:0000:0001:0001:0002'
        enc_ipv6 = convert.encode_ipv6(ipv6)
        self.assertTrue(convert.matches_ipv6(ipv6))
        self.assertTrue(convert.encode(ipv6, 128), enc_ipv6)
        dec_ipv6 = convert.decode_ipv6(enc_ipv6)
        ipv6_addr = ipaddress.ip_address(unicode(ipv6))
        self.assertEquals(str(ipv6_addr), dec_ipv6)

        ipv6b = '0:0:0:0:0:1:1:2'
        enc_ipv6b = convert.encode_ipv6(ipv6b)
        self.assertTrue(convert.matches_ipv6(ipv6b))
        self.assertTrue(convert.encode(ipv6b, 128), enc_ipv6b)
        dec_ipv6b = convert.decode_ipv6(enc_ipv6b)
        ipv6b_addr = ipaddress.ip_address(unicode(ipv6b))
        self.assertEquals(str(ipv6b_addr), dec_ipv6b)

        self.assertEquals(convert.encode(mac, 6 * 8), enc_mac)
        self.assertEquals(convert.encode(ip, 4 * 8), enc_ip)
        self.assertEquals(convert.encode(num, 5 * 8), enc_num)
        self.assertEquals(convert.encode((num, ), 5 * 8), enc_num)
        self.assertEquals(convert.encode([num], 5 * 8), enc_num)

        num = 256
        try:
            convert.encode_num(num, 8)
            self.fail('Exception expected here')
        except SyntaxError:
            pass
Esempio n. 3
0
    def get_data_inspection_src_mac_keys(self):
        """
        Returns a set of macs that is a key to the data_inspection_t table
        :return: set src_mac values
        """
        table_name = '{}.data_inspection_t'.format(self.p4_ingress)
        match_vals = self.get_match_values(table_name)

        out = set()
        for match_val in match_vals:
            for key, value in match_val.items():
                if key == 'hdr.ethernet.src_mac':
                    out.add(decode_mac(value))
        logger.debug('Data Inspection macs from device [%s] - [%s]',
                     self.device_id, out)
        return out
    def get_data_forward_macs(self):
        """
        Returns the key MAC values from the data_forward_t table
        :return: set dst_mac values
        """
        table_name = '{}.data_forward_t'.format(self.p4_ingress)
        match_vals = self.get_match_values(table_name)

        out = set()
        for match_val in match_vals:
            for key, value in match_val.items():
                if key == 'hdr.ethernet.dst_mac':
                    out.add(decode_mac(value))

        logger.debug('Data forward macs from device [%s] - [%s]',
                     self.grpc_addr, out)
        return out