def lookup_typedef(self, stmt, typename): if self.typedefs.has_key(typename): return self.typedefs[typename] typedef = statements.search_typedef(stmt, typename) if not typedef: typedef = self.lookup_global_type(typename, stmt) if not typedef: raise Exception("Undefined type here: '%s' (%s)" % (typename, stmt.path())) typevalidator = extract_type(typedef, self) type = TypeDef(typename, typevalidator) self.typedefs[typename] = type return type
def handle_type(self): typename = self.get_type() assert typename is not None if typename == "leafref" \ or self.get_tailf(('tailf-common', 'non-strict-leafref')): # Will be handled later self.is_leafref = True value = ["<leafref>"] elif self.schema.lookup_map("type_map", typename): value = self.schema.lookup_map("type_map", typename) elif typename == "string": value = self.get_string() elif re.match("u?int(\\d+)", typename): value = self.get_integer() elif typename == "decimal64": value = self.get_decimal() elif typename == "enumeration": value = self.get_enum() elif typename == "boolean": value = ["false", "true"] elif typename == "empty": value = ["<empty-true>"] if self.is_mandatory() \ else ["<empty-false>", "<empty-true>"] elif typename == "union": nodes = self.get_union() for node in nodes: for y in node.handle_type(): yield y return elif typename.find(":") >= 0: # External types must be mapped in the yang_type_map pytest.fail("Missing type map entry at " + self.path + ": " + typename) else: # Oops, missing in pyang? if not hasattr(self.stmt, "i_orig_module"): self.stmt.i_orig_module = self.stmt.parent.i_orig_module if not hasattr(self.stmt, "i_typedefs"): self.stmt.i_typedefs = self.stmt.parent.i_typedefs # Typedef? typedef = statements.search_typedef(self.stmt, typename) if typedef is None: statements.print_tree(self.stmt) raise Exception("Unexpected type: " + typename) t = drned_node(self.schema, typedef) for y in t.handle_type(): yield y return yield value
def typestring(node): def get_nontypedefstring(node): s = "" found = False t = node.search_one('type') if t is not None: s = t.arg + '\n' if t.arg == 'enumeration': found = True s = s + ' : {' for enums in t.substmts: s = s + enums.arg + ',' s = s + '}' elif t.arg == 'leafref': found = True s = s + ' : ' p = t.search_one('path') if p is not None: s = s + p.arg elif t.arg == 'identityref': found = True b = t.search_one('base') if b is not None: s = s + ' {' + b.arg + '}' elif t.arg == 'union': found = True uniontypes = t.search('type') s = s + '{' + uniontypes[0].arg for uniontype in uniontypes[1:]: s = s + ', ' + uniontype.arg s = s + '}' typerange = t.search_one('range') if typerange is not None: found = True s = s + ' [' + typerange.arg + ']' length = t.search_one('length') if length is not None: found = True s = s + ' {length = ' + length.arg + '}' pattern = t.search_one('pattern') if pattern is not None: # truncate long patterns found = True s = s + ' {pattern = ' + pattern.arg + '}' return s s = get_nontypedefstring(node) if s != "": t = node.search_one('type') # chase typedef type_namespace = None i_type_name = None name = t.arg if name.find(":") == -1: prefix = None else: [prefix, name] = name.split(':', 1) if prefix is None or t.i_module.i_prefix == prefix: # check local typedefs pmodule = node.i_module typedef = statements.search_typedef(t, name) else: # this is a prefixed name, check the imported modules err = [] pmodule = util.prefix_to_module(t.i_module, prefix, t.pos, err) if pmodule is None: return typedef = statements.search_typedef(pmodule, name) if typedef != None: s = s + get_nontypedefstring(typedef) return s
def getType(node): global codegenTypesToYangTypesMap xpath = statements.mk_path_str(node, True) def resolveType(stmt, nodeType): if nodeType == "string" \ or nodeType == "instance-identifier" \ or nodeType == "identityref": return codegenTypesToYangTypesMap["string"] elif nodeType == "enumeration": enums = [] for enum in stmt.substmts: if enum.keyword == "enum": enums.append(enum.arg) return codegenTypesToYangTypesMap["string"], enums elif nodeType == "empty" or nodeType == "boolean": return {"type": "boolean", "format": "boolean"} elif nodeType == "leafref": return handle_leafref(node, xpath) elif nodeType == "union": return codegenTypesToYangTypesMap["string"] elif nodeType == "decimal64": return codegenTypesToYangTypesMap[nodeType] elif nodeType in [ 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'binary', 'bits' ]: return codegenTypesToYangTypesMap[nodeType] else: print("no base type found") sys.exit(2) base_types = [ 'int8', 'int16', 'int32', 'int64', 'uint8', 'uint16', 'uint32', 'uint64', 'decimal64', 'string', 'boolean', 'enumeration', 'bits', 'binary', 'leafref', 'identityref', 'empty', 'union', 'instance-identifier' ] # Get Type of a node t = node.search_one('type') while t.arg not in base_types: # chase typedef name = t.arg if name.find(":") == -1: prefix = None else: [prefix, name] = name.split(':', 1) if prefix is None or t.i_module.i_prefix == prefix: # check local typedefs pmodule = node.i_module typedef = statements.search_typedef(t, name) else: # this is a prefixed name, check the imported modules err = [] pmodule = util.prefix_to_module(t.i_module, prefix, t.pos, err) if pmodule is None: return typedef = statements.search_typedef(pmodule, name) if typedef is None: print( "Typedef ", name, " is not found, make sure all dependent modules are present") sys.exit(2) t = typedef.search_one('type') return resolveType(t, t.arg)
def typestring(node): def get_nontypedefstring(node): s = {} found = False t = node.search_one('type') if t is not None: s['type'] = t.arg if t.arg == 'enumeration': found = True s['enumeration'] = [] for enums in t.substmts: s['enumeration'].append(enums.arg) elif t.arg == 'leafref': found = True p = t.search_one('path') if p is not None: s['path'] = p.arg elif t.arg == 'identityref': found = True b = t.search_one('base') if b is not None: s['base'] = b.arg elif t.arg == 'union': found = True uniontypes = t.search('type') s['union'] = [uniontypes[0].arg] for uniontype in uniontypes[1:]: s['union'].append(uniontype.arg) typerange = t.search_one('range') if typerange is not None: found = True s['type_range'] = typerange.arg length = t.search_one('length') if length is not None: found = True s['length'] = length.arg pattern = t.search_one('pattern') if pattern is not None: found = True s['pattern'] = json_escape(pattern.arg) return s s = get_nontypedefstring(node) if len(s) != 0: t = node.search_one('type') # chase typedef type_namespace = None i_type_name = None name = t.arg if name.find(":") == -1: prefix = None else: [prefix, name] = name.split(':', 1) if prefix is None or t.i_module.i_prefix == prefix: # check local typedefs pmodule = node.i_module typedef = statements.search_typedef(t, name) else: # this is a prefixed name, check the imported modules err = [] pmodule = util.prefix_to_module( t.i_module, prefix, t.pos, err) if pmodule is None: return typedef = statements.search_typedef(pmodule, name) if typedef != None: s['typedef'] = get_nontypedefstring(typedef) return s
def typestring(node): def get_nontypedefstring(node): s = "" found = False t = node.search_one('type') if t is not None: s = t.arg + '\n' if t.arg == 'enumeration': found = True s = s + ' : {' for enums in t.substmts: s = s + enums.arg + ',' s = s + '}' elif t.arg == 'leafref': found = True s = s + ' : ' p = t.search_one('path') if p is not None: s = s + p.arg elif t.arg == 'identityref': found = True b = t.search_one('base') if b is not None: s = s + ' {' + b.arg + '}' elif t.arg == 'union': found = True uniontypes = t.search('type') s = s + '{' + uniontypes[0].arg for uniontype in uniontypes[1:]: s = s + ', ' + uniontype.arg s = s + '}' typerange = t.search_one('range') if typerange is not None: found = True s = s + ' [' + typerange.arg + ']' length = t.search_one('length') if length is not None: found = True s = s + ' {length = ' + length.arg + '}' pattern = t.search_one('pattern') if pattern is not None: # truncate long patterns found = True s = s + ' {pattern = ' + pattern.arg + '}' return s s = get_nontypedefstring(node) if s != "": t = node.search_one('type') # chase typedef type_namespace = None i_type_name = None name = t.arg if name.find(":") == -1: prefix = None else: [prefix, name] = name.split(':', 1) if prefix is None or t.i_module.i_prefix == prefix: # check local typedefs pmodule = node.i_module typedef = statements.search_typedef(t, name) else: # this is a prefixed name, check the imported modules err = [] pmodule = statements.prefix_to_module(t.i_module,prefix,t.pos,err) if pmodule is None: return typedef = statements.search_typedef(pmodule, name) if typedef != None: s = s + get_nontypedefstring(typedef) return s