def virtual_members(self): virtual_members = [] if self.ir_class.is_subclassof("of_oxm"): value_member = find(lambda m: m.name, self.ir_model_members) if value_member: oxm_entry = model.oxm_map[self.interface.name] virtual_members += [ JavaVirtualMember( self, "matchField", java_type.make_match_field_jtype( value_member.java_type.public_type), "MatchField.%s" % oxm_entry.value), JavaVirtualMember(self, "masked", java_type.boolean, "true" if oxm_entry.masked else "false"), ] else: virtual_members += [ JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype(), "null"), JavaVirtualMember(self, "masked", java_type.boolean, "false"), ] if not find(lambda m: m.name == "version", self.ir_model_members): virtual_members.append( JavaVirtualMember( self, "version", java_type.of_version, "OFVersion.%s" % self.version.constant_version)) return tuple(virtual_members)
def virtual_members(self): virtual_members = [] if self.ir_class.is_subclassof("of_oxm"): value_member = find(lambda m: m.name, self.ir_model_members) if value_member: oxm_entry = model.oxm_map[self.interface.name] virtual_members += [ JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype(value_member.java_type.public_type), "MatchField.%s" % oxm_entry.value), JavaVirtualMember(self, "masked", java_type.boolean, "true" if oxm_entry.masked else "false"), ] else: virtual_members += [ JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype(), "null"), JavaVirtualMember(self, "masked", java_type.boolean, "false"), ] elif self.ir_class.is_subclassof("of_oxs"): value_member = find(lambda m: m.name, self.ir_model_members) if value_member: oxs_entry = model.oxs_map[self.interface.name] virtual_members += [ JavaVirtualMember(self, "statField", java_type.make_stat_field_jtype(value_member.java_type.public_type), "StatField.%s" % oxs_entry.value), JavaVirtualMember(self, "masked", java_type.boolean, "true" if oxs_entry.masked else "false"), ] else: virtual_members += [ JavaVirtualMember(self, "statField", java_type.make_stat_field_jtype(), "null"), JavaVirtualMember(self, "masked", java_type.boolean, "false"), ] if not find(lambda m: m.name == "version", self.ir_model_members): virtual_members.append(JavaVirtualMember(self, "version", java_type.of_version, "OFVersion.%s" % self.version.constant_version)) return tuple(virtual_members)
def virtual_members(self): virtual_members = [] if self.name == "OFOxm": virtual_members += [ JavaVirtualMember(self, "value", java_type.generic_t), JavaVirtualMember(self, "mask", java_type.generic_t), JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype("T")), JavaVirtualMember(self, "masked", java_type.boolean), JavaVirtualMember(self, "canonical", java_type.make_oxm_jtype("T")) ] elif self.ir_class.is_subclassof("of_oxm"): value = find(lambda m: m.name=="value", self.ir_model_members) if value: field_type = java_type.make_match_field_jtype(value.java_type.public_type) else: field_type = java_type.make_match_field_jtype() virtual_members += [ JavaVirtualMember(self, "matchField", field_type), JavaVirtualMember(self, "masked", java_type.boolean), JavaVirtualMember(self, "canonical", java_type.make_oxm_jtype(value.java_type.public_type), custom_template=lambda builder: "OFOxm{}_getCanonical.java".format(".Builder" if builder else "")), ] if not find(lambda x: x.name == "mask", self.ir_model_members): virtual_members.append( JavaVirtualMember(self, "mask", find(lambda x: x.name == "value", self.ir_model_members).java_type)) elif self.name =="OFErrorMsg": virtual_members += [ JavaVirtualMember(self, "data", java_type.error_cause_data) ] if not find(lambda m: m.name == "version", self.ir_model_members): virtual_members.append(JavaVirtualMember(self, "version", java_type.of_version)) return tuple(virtual_members)
def virtual_members(self): virtual_members = [] if self.name == "OFOxm": virtual_members += [ JavaVirtualMember(self, "value", java_type.generic_t), JavaVirtualMember(self, "mask", java_type.generic_t), JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype("T")), JavaVirtualMember(self, "masked", java_type.boolean), ] elif self.parent_interface and self.parent_interface.startswith("OFOxm"): field_type = java_type.make_match_field_jtype(model.oxm_map[self.name].type_name) \ if self.name in model.oxm_map \ else java_type.make_match_field_jtype() virtual_members += [ JavaVirtualMember(self, "matchField", field_type), JavaVirtualMember(self, "masked", java_type.boolean), ] if not find(lambda x: x.name == "mask", self.ir_model_members): virtual_members.append(JavaVirtualMember(self, "mask", find(lambda x: x.name == "value", self.ir_model_members).java_type)) if not find(lambda m: m.name == "version", self.ir_model_members): virtual_members.append(JavaVirtualMember(self, "version", java_type.of_version)) return tuple(virtual_members)
def entry_by_c_name(self, name): res = find(lambda e: e.c_name == name, self.entries) if res: return res else: raise KeyError("Enum %s: no entry with c_name %s" % (self.name, name))
def virtual_members(self): virtual_members = [] if self.name == "OFOxm": virtual_members += [ JavaVirtualMember(self, "value", java_type.generic_t), JavaVirtualMember(self, "mask", java_type.generic_t), JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype("T")), JavaVirtualMember(self, "masked", java_type.boolean), JavaVirtualMember(self, "canonical", java_type.make_oxm_jtype("T")) ] elif self.ir_class.is_subclassof("of_oxm"): value = find(lambda m: m.name == "value", self.ir_model_members) if value: field_type = java_type.make_match_field_jtype( value.java_type.public_type) else: field_type = java_type.make_match_field_jtype() virtual_members += [ JavaVirtualMember(self, "matchField", field_type), JavaVirtualMember(self, "masked", java_type.boolean), JavaVirtualMember( self, "canonical", java_type.make_oxm_jtype(value.java_type.public_type), custom_template=lambda builder: "OFOxm{}_getCanonical.java" .format(".Builder" if builder else "")), ] if not find(lambda x: x.name == "mask", self.ir_model_members): virtual_members.append( JavaVirtualMember( self, "mask", find(lambda x: x.name == "value", self.ir_model_members).java_type)) elif self.name == "OFErrorMsg": virtual_members += [ JavaVirtualMember(self, "data", java_type.error_cause_data) ] if not find(lambda m: m.name == "version", self.ir_model_members): virtual_members.append( JavaVirtualMember(self, "version", java_type.of_version)) return tuple(virtual_members)
def create_ofinput(ast): """ Create an OFInput from an AST @param ast An AST as returned by loxi_front_end.parser.parse @returns An OFInput object """ ctx = FrontendCtx(set()) ofinput = OFInput(wire_versions=set(), classes=[], enums=[]) for decl_ast in ast: if decl_ast[0] == 'struct': # 0: "struct" # 1: name # 2: potentially list of [param_name, param_value] # 3: super_class or None # 4: list of members superclass = decl_ast[3] members = [create_member(m_ast, ctx) for m_ast in decl_ast[4]] discriminators = [ m for m in members if isinstance(m, OFDiscriminatorMember) ] if len(discriminators) > 1: raise InputError("%s: Cannot support more than one discriminator by class - got %s" % (decl_ast[1], repr(discriminators))) ofclass = OFClass(name=decl_ast[1], members=members, superclass=superclass, virtual = len(discriminators) > 0, params = { param: value for param, value in decl_ast[2] }) ofinput.classes.append(ofclass) if decl_ast[0] == 'enum': # 0: "enum" # 1: name # 2: potentially list of [param_name, param_value] # 3: list of [constant_name, constant_value]+ enum = OFEnum(name=decl_ast[1], entries=[OFEnumEntry(name=x[0], value=x[2], params={param:value for param, value in x[1] }) for x in decl_ast[3]], params = { param: value for param, value in decl_ast[2] } ) ofinput.enums.append(enum) elif decl_ast[0] == 'metadata': if decl_ast[1] == 'version': if decl_ast[2] == 'any': ofinput.wire_versions.update(of_g.wire_ver_map.keys()) elif int(decl_ast[2]) in of_g.supported_wire_protos: ofinput.wire_versions.add(int(decl_ast[2])) else: raise InputError("Unrecognized wire protocol version %r" % decl_ast[2]) found_wire_version = True if not ofinput.wire_versions: raise InputError("Missing #version metadata") for used_enum in ctx.used_enums: if not find(lambda e: e.name == used_enum, ofinput.enums): raise Exception("Undeclared enum used in OFInput: {}".format(used_enum)) return ofinput
def entry_by_version_value(self, version, value): res = find( lambda e: e.values[version] == value if version in e.values else False, self.entries) if res: return res else: raise KeyError("Enum %s: no entry with version %s, value %s" % (self.name, version, value))
def lookup_ir_wiretype(oftype, version): """ if of is a reference to an enum in ir, resolve it to the wiretype declared in that enum. Else return oftype """ enums = loxi_globals.ir[version].enums enum = find(lambda e: e.name == oftype, enums) if enum and 'wire_type' in enum.params: return enum.params['wire_type'] else: return oftype
def lookup_ir_wiretype(oftype, version): """ if of is a reference to an enum in ir, resolve it to the wiretype declared in that enum. Else return oftype """ enums = of_g.ir[version].enums enum = find(lambda e: e.name == oftype, enums) if enum and 'wire_type' in enum.params: return enum.params['wire_type'] else: return oftype
def virtual_members(self): virtual_members = [] if self.interface.parent_interface and self.interface.parent_interface.startswith("OFOxm"): if self.interface.name in model.oxm_map: oxm_entry = model.oxm_map[self.interface.name] virtual_members += [ JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype(oxm_entry.type_name), "MatchField.%s" % oxm_entry.value), JavaVirtualMember(self, "masked", java_type.boolean, "true" if oxm_entry.masked else "false"), ] else: virtual_members += [ JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype(), "null"), JavaVirtualMember(self, "masked", java_type.boolean, "false"), ] if not find(lambda m: m.name == "version", self.ir_model_members): virtual_members.append(JavaVirtualMember(self, "version", java_type.of_version, "OFVersion.%s" % self.version.constant_version)) return tuple(virtual_members)
def virtual_members(self): if self.name == "OFOxm": return ( JavaVirtualMember(self, "value", java_type.generic_t), JavaVirtualMember(self, "mask", java_type.generic_t), JavaVirtualMember(self, "matchField", java_type.make_match_field_jtype("T")), JavaVirtualMember(self, "masked", java_type.boolean), ) elif self.parent_interface and self.parent_interface.startswith("OFOxm"): field_type = java_type.make_match_field_jtype(model.oxm_map[self.name].type_name) \ if self.name in model.oxm_map \ else java_type.make_match_field_jtype() return ( JavaVirtualMember(self, "matchField", field_type), JavaVirtualMember(self, "masked", java_type.boolean), ) \ + \ ( ( JavaVirtualMember(self, "mask", find(lambda x: x.name == "value", self.ir_model_members).java_type), ) if not find(lambda x: x.name == "mask", self.ir_model_members) else () ) else: return ()
def discriminator(self): return find(lambda m: isinstance(m, OFDiscriminatorMember), self.ir_class.members)
def lookup_ir_enum(oftype, version): """ if oftype is a reference to an enum in ir, return the value-name mapping; otherwise return None """ enums = loxi_globals.ir[version].enums return find(lambda e: e.name == oftype, enums)
def member_by_name(self, name): return find(lambda m: m.name == name, self.members)
def enum_by_name(self, name): res = find(lambda e: e.name == name, self.enums) if not res: raise KeyError("Could not find enum with name %s" % name) return res
def interface_by_name(self, name): return find(lambda i: erase_type_annotation(i.name) == erase_type_annotation(name), self.interfaces)
def enum_by_name(self, name): return find(lambda enum: enum.name == name, self.enums)
def entry_by_version_value(self, version, value): res = find(lambda e: e.values[version] == value if version in e.values else False, self.entries) if res: return res else: raise KeyError("Enum %s: no entry with version %s, value %s" % (self.name, version, value))
def has_type_members(self): return find(lambda m: isinstance(m, OFTypeMember), self.members) is not None
def discriminator(self): return find(lambda m: type(m) == OFDiscriminatorMember, self.members)
def member_by_name(self, name): return find(lambda m: hasattr(m, "name") and m.name == name, self.members)
def superclass(self): return find(lambda c: c.version == self.version and c.c_name == self.ir_class.superclass, model.all_classes)
def masked_enum_group(self): group = find(lambda g: self.name in g.members, model.masked_enum_groups[self.enum.name]) return group
def class_by_name(self, name): return find(lambda ofclass: ofclass.name == name, self.classes)
def length_member(self): return find(lambda m: type(m) == OFLengthMember, self.members)