def handle_enumeration(tag, target, name, parent): """ Handle a 'type enumeration' tag tag: pyang object for type enumeration target: xml tag to place resulting node parent: parent YangCSDLConversionObj for document access """ prop_node = SubElement(target, 'EnumType', {'Name': name + 'Enumeration'}) parent.my_doc.types[name + 'Enumeration'] = prop_node my_content = collectChildren(tag) for tag in my_content: if tag.keyword == 'enum': member_node = SubElement(prop_node, 'Member') member_node.set('Name', tag.arg) xml_convenience.add_annotation( member_node, { 'Term': redfishtypes.get_descriptive_properties_mapping( tag.keyword), 'String': tag.arg }) for tag in collectChildren(tag): consume_tag_basic(tag, member_node, parent) else: consume_tag_basic(tag, prop_node, parent)
def handle_description(yang_obj, target): yang_obj.arg = yang_obj.arg + '.' if yang_obj.arg[ -1] != '.' else yang_obj.arg yang_obj.arg = yang_obj.arg.replace('\n', ' ') annotation = xml_convenience.add_annotation( target, { 'Term': 'OData.Description', 'String': yang_obj.arg.split('. ')[0].strip('.') + '.' }) annotation_b = xml_convenience.add_annotation(target, { 'Term': 'OData.LongDescription', 'String': yang_obj.arg }) return annotation_b
def handle_generic_node(keyword, arg, target): """ Handler for any Enum annotations defined in YangExtensions Usually of keywords with limited values, such as Deviate and Config """ term, enummember = redfishtypes.get_annotation_enum(keyword, arg) annotation = xml_convenience.add_annotation(target, { 'Term': term, 'EnumMember': enummember }) return annotation
def create_leaf(yang_obj, target, parent): csdlname = get_valid_csdl_identifier(yang_obj.arg) my_content = collectChildren(yang_obj) if yang_obj.keyword == 'notification': prop_node = SubElement(parent.my_doc.Schema, "EntityType", { 'Name': csdlname, 'BaseType': 'Resource.v1_0_0.Resource' }) else: prop_node = SubElement(target, "Property", { 'Name': csdlname, 'Type': 'Edm.Primitive' }) if yang_obj.keyword == 'leaf': if len([x for x in my_content if x.keyword == 'type']) == 0: print('leaf must include type') if yang_obj.keyword == 'leaf-list': prop_node.set( 'Type', 'Collection({}.{})'.format('prefix' + "v1_0_0", csdlname)) xml_convenience.add_annotation( prop_node, { "Term": "OData.LongDescription", "String": "List of the type {}.".format(csdlname) }) if yang_obj.keyword == 'anyxml': prop_node.set('Type', redfishtypes.types_mapping[yang_obj.keyword]) for tag in my_content: consume_tag_basic(tag, prop_node, parent) handle_generic_node('leaf', yang_obj.keyword, prop_node) xml_convenience.add_annotation(prop_node, { 'Term': 'OData.Permissions', 'EnumMember': 'OData.Permission/Read' })
def create_container(yang_obj, target, parent): mobj = YangCSDLConversionObj(yang_obj, parent) csdlname = mobj.csdlname if yang_obj.keyword in ['grouping']: return navigation_property = SubElement(target, 'NavigationProperty') # Handle Container Grammar if yang_obj.keyword in ['container']: tname = csdlname.split('.')[-1] parent.my_doc.imports[csdlname] = mobj navigation_property.set('Name', tname + 'Container') navigation_property.set('Type', mobj.get_full_csdlname() + '.' + tname) # Handle List Grammar if yang_obj.keyword in ['list']: csdlname = csdlname + 'Collection' tname = csdlname.split('.')[-1] parent.my_doc.imports[csdlname] = csdlname navigation_property.set('Name', tname) navigation_property.set('Type', mobj.get_full_csdlname() + '.' + tname) navigation_property.set('ContainsTarget', 'true') # At the end of the grammar, modify outside nav property xml_convenience.add_annotation(navigation_property, { 'Term': 'OData.Permissions', 'EnumMember': 'OData.Permissions/Read' }) xml_convenience.add_annotation( navigation_property, { 'Term': 'OData.Description', 'String': 'Navigation property that points to a resource of {}.'.format( str(csdlname)) }) xml_convenience.add_annotation(navigation_property, { 'Term': 'OData.LongDescription', 'String': 'Automatically generated.' }) xml_convenience.add_annotation(navigation_property, {'Term': 'OData.AutoExpandReferences'})
def handle_typedef(tag, target, parent): new_node = SubElement(parent.my_doc.Schema, 'TypeDefinition', {'Name': get_valid_csdl_identifier(tag.arg)}) yang_type = 'empty' for inner_tag in collectChildren(tag): if inner_tag.keyword == 'type': yang_type = inner_tag.arg yang_children_inner = collectChildren(inner_tag) if inner_tag.arg == 'union': union_annotation = xml_convenience.add_annotation( new_node, {'Term': 'RedfishYang.union'}) col = SubElement(union_annotation, 'Collection') for child in yang_children_inner: if child.keyword != 'type': continue else: inn = SubElement(col, 'String') inn.text = '"{}"'.format( redfishtypes.get_valid_csdl_identifier(child.arg)) elif inner_tag.arg == 'enumeration': parent.my_doc.Schema.remove(new_node) handle_enumeration(inner_tag, parent.my_doc.Schema, tag.arg, parent) yang_type = 'enumeration' else: yang_type_location, my_type, yang_type = dereference_type( inner_tag, target, parent) if yang_type is None: yang_type_location, yang_type = 'RedfishYang', 'string' new_node.set('UnderlyingType', 'Edm.String') else: new_node.set('UnderlyingType', my_type) annotation = SubElement( new_node, 'Annotation', { 'Term': yang_type_location + '.YangType', 'EnumMember': yang_type_location + '.YangTypes/' + yang_type }) else: consume_tag_basic(inner_tag, new_node, parent) if yang_type != 'enumeration': parent.my_doc.types[tag.arg] = new_node
def handle_generic_statement(yang_obj, target): """ Create a statement, just the most generic interpretation of a keyword (For unrecognized keywords or raw conversion) """ if type(yang_obj.keyword) is tuple: yang_obj.keyword = ':'.join(yang_obj.keyword) string = yang_obj.keyword string += ' ' + yang_obj.arg if yang_obj.arg not in ['', ' ', None] else '' annotation = xml_convenience.add_annotation(target, { 'Term': 'RedfishYang.statement', 'String': yang_obj.keyword }) my_children = collectChildren(yang_obj) if len(my_children) > 0: print('Skipped children {} in tag {} {}'.format( len(my_children), yang_obj.keyword, yang_obj.arg)) return annotation
def handle_generic(yang_obj, target=None): """ Attempts to handle generically tags that require no special treatment Creates standard RedfishYang annotation, unless otherwise is "unknown" or "Description" """ if yang_obj.arg: yang_obj.arg.replace('\n', '') else: yang_obj.arg = '' annotation = xml_convenience.add_annotation( target, { 'Term': redfishtypes.get_descriptive_properties_mapping(yang_obj.keyword), 'String': yang_obj.arg.replace('\n', '') }) my_children = collectChildren(yang_obj) if len(my_children) > 0: print('Skipped children {} in tag {} {}'.format( len(my_children), yang_obj.keyword, yang_obj.arg)) return annotation