# Path: pif_ir/bir/objects/bir_struct.py
# class BIRStruct(object):
#     required_attributes = ['fields']
# 
#     def __init__(self, name, struct_attrs):
#         check_attributes(name, struct_attrs, BIRStruct.required_attributes)
#         logging.debug("Adding bir_struct {0}".format(name))
# 
#         self.name = name
#         self.length = 0
#         self.fields = OrderedDict()
#         self.field_offsets = OrderedDict()
# 
#         for tmp_field in struct_attrs['fields']:
#             for name, size in tmp_field.items():
#                 self.fields[name] = size
# 
#             self.field_offsets[name] = self.length
#             self.length += size
# 
#     def __len__(self):
#         return self.length
# 
#     def field_offset(self, fld_name):
#         return self.field_offsets.get(fld_name, 0)
# 
#     def field_size(self, fld_name):
#         return self.fields.get(fld_name, 0)
# 
# Path: pif_ir/bir/objects/metadata_instance.py
# class MetadataInstance(object):
#     required_attributes = ['values', 'visibility']
# 
#     def __init__(self, name, metadata_attrs, bir_structs, buf=None, 
#                  bit_offset=0):
#         check_attributes(name, metadata_attrs, 
#                          MetadataInstance.required_attributes)
# 
#         logging.debug("Adding metadata {0}".format(name))
#         self.name = name
#         self.values = OrderedDict()
#         # FIXME: used for syntactic checking
#         self.visibility = metadata_attrs['visibility']
# 
#         struct_name = metadata_attrs['values']
#         for f_name, f_size in bir_structs[struct_name].fields.items():
#             self.values[f_name] = ValueInstance(f_name, f_size)
# 
#         if buf:
#             self.extract(buf, bit_offset)
# 
#     def __len__(self):
#         return sum([len(fld) for fld in self.values.values()])
# 
#     def __int__(self):
#         value = 0;
#         for fld in self.values.values():
#             value = (value << len(fld)) + int(fld)
#         return value
# 
#     def to_dict(self):
#         return dict([(v.name,int(v)) for v in self.values.values()])
# 
#     def extract(self, buf, bit_offset=0):
#         fld_offset = 0;
#         for fld in self.values.values():
#             fld.extract(buf, bit_offset + fld_offset);
#             fld_offset += len(fld)
# 
#     def serialize(self):
#         byte_list = bytearray(len(self)/8)
#         bit_offset = 0;
#         for fld in self.values.values():
#             fld.update(byte_list, bit_offset)
#             bit_offset += len(fld)
#         return byte_list
# 
#     def get_value(self, value_name):
#         if value_name not in  self.values:
#             return 0
#         return self.values[value_name].value
# 
#     def set_value(self, value_name, value):
#         fld = self.values.get(value_name, None)
#         if fld:
#             fld.value = value
# 
#     def reset_values(self, new_val=0):
#         for fld in self.values.values():
#             fld.value = new_val


import logging
import struct
from pif_ir.bir.objects.bir_struct import BIRStruct
from pif_ir.bir.objects.metadata_instance import MetadataInstance
from test_common import yaml_eth_struct_dict
from test_common import yaml_eth_meta_dict
#!/usr/bin/env python

logging.basicConfig(level=logging.INFO)
logging.info("RUNNING TEST: %s" % __file__)

# packet to extract data from
eth_data = struct.pack("BBBBBB", 0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33)
eth_data += struct.pack("BBBBBB", 0xff, 0xff, 0xff, 0xff, 0xff, 0xff)
eth_data += struct.pack("BB", 0x81, 0x00)
eth_pkt = bytearray(eth_data)

# bir structs for the the metadata fields
bir_structs = {'eth_t':BIRStruct('eth_t', yaml_eth_struct_dict)}


# Path: pif_ir/bir/objects/bir_struct.py
# class BIRStruct(object):
#     required_attributes = ['fields']
# 
#     def __init__(self, name, struct_attrs):
#         check_attributes(name, struct_attrs, BIRStruct.required_attributes)
#         logging.debug("Adding bir_struct {0}".format(name))
# 
#         self.name = name
#         self.length = 0
#         self.fields = OrderedDict()
#         self.field_offsets = OrderedDict()
# 
#         for tmp_field in struct_attrs['fields']:
#             for name, size in tmp_field.items():
#                 self.fields[name] = size
# 
#             self.field_offsets[name] = self.length
#             self.length += size
# 
#     def __len__(self):
#         return self.length
# 
#     def field_offset(self, fld_name):
#         return self.field_offsets.get(fld_name, 0)
# 
#     def field_size(self, fld_name):
#         return self.fields.get(fld_name, 0)
# 
# Path: pif_ir/bir/objects/metadata_instance.py
# class MetadataInstance(object):
#     required_attributes = ['values', 'visibility']
# 
#     def __init__(self, name, metadata_attrs, bir_structs, buf=None, 
#                  bit_offset=0):
#         check_attributes(name, metadata_attrs, 
#                          MetadataInstance.required_attributes)
# 
#         logging.debug("Adding metadata {0}".format(name))
#         self.name = name
#         self.values = OrderedDict()
#         # FIXME: used for syntactic checking
#         self.visibility = metadata_attrs['visibility']
# 
#         struct_name = metadata_attrs['values']
#         for f_name, f_size in bir_structs[struct_name].fields.items():
#             self.values[f_name] = ValueInstance(f_name, f_size)
# 
#         if buf:
#             self.extract(buf, bit_offset)
# 
#     def __len__(self):
#         return sum([len(fld) for fld in self.values.values()])
# 
#     def __int__(self):
#         value = 0;
#         for fld in self.values.values():
#             value = (value << len(fld)) + int(fld)
#         return value
# 
#     def to_dict(self):
#         return dict([(v.name,int(v)) for v in self.values.values()])
# 
#     def extract(self, buf, bit_offset=0):
#         fld_offset = 0;
#         for fld in self.values.values():
#             fld.extract(buf, bit_offset + fld_offset);
#             fld_offset += len(fld)
# 
#     def serialize(self):
#         byte_list = bytearray(len(self)/8)
#         bit_offset = 0;
#         for fld in self.values.values():
#             fld.update(byte_list, bit_offset)
#             bit_offset += len(fld)
#         return byte_list
# 
#     def get_value(self, value_name):
#         if value_name not in  self.values:
#             return 0
#         return self.values[value_name].value
# 
#     def set_value(self, value_name, value):
#         fld = self.values.get(value_name, None)
#         if fld:
#             fld.value = value
# 
#     def reset_values(self, new_val=0):
#         for fld in self.values.values():
#             fld.value = new_val


# Path: pif_ir/bir/tests/test_metadata_instance.py
import logging
import struct
from pif_ir.bir.objects.bir_struct import BIRStruct
from pif_ir.bir.objects.metadata_instance import MetadataInstance
from test_common import yaml_eth_struct_dict
from test_common import yaml_eth_meta_dict

#!/usr/bin/env python

logging.basicConfig(level=logging.INFO)
logging.info("RUNNING TEST: %s" % __file__)

# packet to extract data from
eth_data = struct.pack("BBBBBB", 0xAA, 0xBB, 0xCC, 0x11, 0x22, 0x33)
eth_data += struct.pack("BBBBBB", 0xff, 0xff, 0xff, 0xff, 0xff, 0xff)
eth_data += struct.pack("BB", 0x81, 0x00)
eth_pkt = bytearray(eth_data)

# bir structs for the the metadata fields
bir_structs = {'eth_t':BIRStruct('eth_t', yaml_eth_struct_dict)}


metadata = MetadataInstance('eth', yaml_eth_meta_dict, bir_structs, 