CN112235311B - OVSDB client code automatic generation method, system, device and medium - Google Patents
OVSDB client code automatic generation method, system, device and medium Download PDFInfo
- Publication number
- CN112235311B CN112235311B CN202011127502.2A CN202011127502A CN112235311B CN 112235311 B CN112235311 B CN 112235311B CN 202011127502 A CN202011127502 A CN 202011127502A CN 112235311 B CN112235311 B CN 112235311B
- Authority
- CN
- China
- Prior art keywords
- code
- file
- schema file
- database
- ovsdb
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Active
Links
- 238000000034 method Methods 0.000 title claims abstract description 36
- 230000006870 function Effects 0.000 claims description 18
- 238000004590 computer program Methods 0.000 claims description 6
- 238000013507 mapping Methods 0.000 abstract description 3
- 239000008186 active pharmaceutical agent Substances 0.000 description 20
- 238000012986 modification Methods 0.000 description 6
- 230000004048 modification Effects 0.000 description 6
- 230000008569 process Effects 0.000 description 5
- 230000008676 import Effects 0.000 description 4
- 238000011161 development Methods 0.000 description 3
- 239000000284 extract Substances 0.000 description 3
- 230000009286 beneficial effect Effects 0.000 description 2
- RWSOTUBLDIXVET-UHFFFAOYSA-N Dihydrogen sulfide Chemical compound S RWSOTUBLDIXVET-UHFFFAOYSA-N 0.000 description 1
- 238000007792 addition Methods 0.000 description 1
- 230000008859 change Effects 0.000 description 1
- 238000004891 communication Methods 0.000 description 1
- 238000012217 deletion Methods 0.000 description 1
- 230000037430 deletion Effects 0.000 description 1
- 230000001419 dependent effect Effects 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 230000008571 general function Effects 0.000 description 1
- 230000007246 mechanism Effects 0.000 description 1
- 230000002688 persistence Effects 0.000 description 1
- 230000002085 persistent effect Effects 0.000 description 1
- 238000012545 processing Methods 0.000 description 1
- 238000011084 recovery Methods 0.000 description 1
- 230000003252 repetitive effect Effects 0.000 description 1
- 230000000717 retained effect Effects 0.000 description 1
- 238000005728 strengthening Methods 0.000 description 1
- 238000012360 testing method Methods 0.000 description 1
Images
Classifications
-
- H—ELECTRICITY
- H04—ELECTRIC COMMUNICATION TECHNIQUE
- H04L—TRANSMISSION OF DIGITAL INFORMATION, e.g. TELEGRAPHIC COMMUNICATION
- H04L69/00—Network arrangements, protocols or services independent of the application payload and not provided for in the other groups of this subclass
- H04L69/22—Parsing or analysis of headers
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/30—Creation or generation of source code
- G06F8/34—Graphical or visual programming
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F8/00—Arrangements for software engineering
- G06F8/40—Transformation of program code
- G06F8/41—Compilation
- G06F8/44—Encoding
Landscapes
- Engineering & Computer Science (AREA)
- General Engineering & Computer Science (AREA)
- Software Systems (AREA)
- Theoretical Computer Science (AREA)
- Physics & Mathematics (AREA)
- General Physics & Mathematics (AREA)
- Computer Security & Cryptography (AREA)
- Computer Networks & Wireless Communication (AREA)
- Signal Processing (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
Abstract
Description
技术领域technical field
本发明属于SDN(软件定义网络)以及数据库领域,特别涉及一种OVSDB客户端代码自动生成方法、系统、设备及介质。The invention belongs to the field of SDN (Software Defined Network) and databases, and in particular relates to an OVSDB client code automatic generation method, system, device and medium.
背景技术Background technique
SDN网络中的协议按照功能可以分为管理层面协议与控制层面协议。以SDN控制器为界限,按照可编程接口的层级可以分为南向接口与北向接口。OpenFlow协议严格的来说,是一种控制层面的南向接口协议,而OVSDB管理协议,是管理层面的南向接口协议。The protocols in the SDN network can be divided into management plane protocols and control plane protocols according to their functions. Taking the SDN controller as the boundary, according to the level of the programmable interface, it can be divided into southbound interface and northbound interface. Strictly speaking, the OpenFlow protocol is a southbound interface protocol at the control level, while the OVSDB management protocol is a southbound interface protocol at the management level.
OVSDB管理协议(Open vSwitch Database Management Protocol,开放虚拟交换机数据库管理协议)起初由VMware公司提出,负责管理开源的软件交换机(OpenvSwitch,OVS)的开放虚拟交换机数据库(OpenvSwitchDatabase,OVSDB),是一个用于实现对虚拟交换机的可编程访问和配置管理的SDN管理协议。OVSDB管理协议定义了一套RPC接口,用户可通过远程调用的方式管理OVSDB,主要包括通信协议(JSON-RPC)方法和所支持的OVSDB操作。OVS是OVSDB的主要应用,其数据模式由OVSDB Schema(DB-SCHEMA)定义,交换机支持通过OVSDB与控制器交互数据,这些数据以表的形式存储在OVSDB数据库中,这些表即为OVSDBSchema表,当交换机和控制器都为OVSDB Client(通过ovsdb管理协议,来与ovsdb数据库服务器进行通信)时,可以在表中增加行,还可以订阅行的添加、删除和修改。OVSDB Management Protocol (Open vSwitch Database Management Protocol, Open Virtual Switch Database Management Protocol) was originally proposed by VMware, which is responsible for managing the Open vSwitch Database (OVSDB) of open source software switches (OpenvSwitch, OVS). SDN management protocol for programmable access and configuration management of virtual switches. The OVSDB management protocol defines a set of RPC interfaces. Users can manage OVSDB through remote calls, mainly including communication protocol (JSON-RPC) methods and supported OVSDB operations. OVS is the main application of OVSDB. Its data schema is defined by OVSDB Schema (DB-SCHEMA). The switch supports data exchange with the controller through OVSDB. These data are stored in the OVSDB database in the form of tables. These tables are OVSDBSchema tables. When both the switch and the controller are OVSDB Client (to communicate with the ovsdb database server through the ovsdb management protocol), you can add rows to the table, and you can subscribe to the addition, deletion and modification of rows.
随着SDN和虚拟化技术的发展,虚拟交换机依靠灵活、弹性、经济的特性已经逐渐取代部分传统硬件交换机,在数据中心网络中占据一席之地。OVSDB也在不停优化强化补齐功能短板,例如安全性、对协议栈的兼容、容灾与逃生等。OVSDB作为数据库基于JSON格式的Schema定义数据库表结构及各成员表项关系,支持RPC接口访问等特性也可以作为通用数据库得到更广泛的应用。With the development of SDN and virtualization technologies, virtual switches have gradually replaced some traditional hardware switches by virtue of their flexible, elastic, and economical features, occupying a place in data center networks. OVSDB is also constantly optimizing and strengthening functional shortcomings, such as security, compatibility with protocol stacks, disaster recovery and escape. As a database, OVSDB defines the database table structure and member table item relationships based on the JSON format Schema, and supports RPC interface access and other features, and can also be used as a general-purpose database for wider applications.
现有技术中OVSDB管理协议存在如下一些问题:The OVSDB management protocol in the prior art has the following problems:
1、存在数据库大量硬编码且重复性高的操作,繁琐且容易出错;1. There are a large number of hard-coded and highly repetitive operations in the database, which are tedious and error-prone;
2、业务开发阶段,数据库表项字段新增修改频繁,需要修改的代码可能无法及时发现,遗留BUG;2. In the business development stage, database table entries and fields are frequently added and modified, and the code that needs to be modified may not be found in time, leaving bugs;
3、ovsdb-client操作命令行要自行构造数据库操作,使用起来比较麻烦,也无法实现测试数据的快速生成。3. The ovsdb-client operation command line needs to construct the database operation by itself, which is troublesome to use and cannot realize the rapid generation of test data.
目前OVSDB集成在OVS项目内使用c语言实现其服务端代码及客户端代码。At present, OVSDB is integrated in the OVS project using c language to implement its server code and client code.
发明内容SUMMARY OF THE INVENTION
发明目的:针对现有技术中存在的问题,本发明公开了一种OVSDB客户端代码自动生成方法、系统、设备及介质,利用数据库Schema文件自动生成其定义的相关数据库Go语言代码,包含客户端连接、各表项操作API、表项数据结构、相关成员范围定义,降低了Go语言用户使用OVSDB的复杂度,提高代码可读性及可维护性。Purpose of the invention: Aiming at the problems existing in the prior art, the present invention discloses an OVSDB client code automatic generation method, system, device and medium, which uses the database Schema file to automatically generate the relevant database Go language code defined by it, including the client Connection, operation API of each table item, table item data structure, and definition of related member range reduce the complexity of using OVSDB for Go language users, and improve code readability and maintainability.
技术方案:本发明采用如下技术方案:一种OVSDB客户端代码自动生成方法,其特征在于,包括如下步骤:Technical scheme: the present invention adopts the following technical scheme: a kind of OVSDB client code automatic generation method, is characterized in that, comprises the following steps:
S1、获取待解析的Schema文件,并对Schema文件进行合法性检查,Schema文件中定义了包括数据库的属性和数据库中各表项的属性;S1. Obtain the schema file to be parsed, and check the validity of the schema file. The schema file defines attributes including database attributes and attributes of each table item in the database;
S2、定义解析后的数据格式,将合法的Schema文件中的内容解析到该数据格式中,得到解析后的数据;S2. Define the parsed data format, parse the content in the legal Schema file into the data format, and obtain the parsed data;
S3、基于解析后的数据和模板代码文件生成数据库Go语言代码,其中模板代码文件中包括预先生成的代码模板,包括步骤:S3. Generate database Go language code based on the parsed data and template code file, wherein the template code file includes a pre-generated code template, including steps:
S31、遍历解析后的数据,根据数据库属性,结合模板代码文件生成公共代码;S31, traverse the parsed data, and generate public code according to the database attribute and in combination with the template code file;
S32、遍历解析后的数据,根据各表项属性,结合模板代码文件生成每个表项的API代码;S32, traverse the parsed data, and generate the API code of each table item according to the attributes of each table item and combined with the template code file;
S33、对公共代码和每个表项的API代码进行格式化,得到最终的数据库Go语言代码。S33. Format the common code and the API code of each table item to obtain the final database Go language code.
优选地,步骤S1中,对Schema文件进行合法性检查包括操作:检查Schema文件是否存在以及打开和读取是否正常;检查Schema文件内容是否符合Json格式语法;检查Schema文件内容中的相关关键字是否符合RFC7047规范;检查是否能够使用ovsdb-tool工具将Schema文件创建为临时数据库文件。Preferably, in step S1, checking the validity of the Schema file includes operations: checking whether the Schema file exists and whether the opening and reading are normal; checking whether the content of the Schema file conforms to the Json format grammar; checking whether the relevant keywords in the content of the Schema file are not Compliant with the RFC7047 specification; check if the ovsdb-tool tool can be used to create a Schema file as a temporary database file.
优选地,若Schema文件进行合法性检查通过,删除临时数据库文件;若Schema文件进行合法性检查未通过,返回具体错误信息并指导开发人员定位修改Schema文件。Preferably, if the Schema file passes the validity check, the temporary database file is deleted; if the Schema file fails the validity check, specific error information is returned and the developer is instructed to locate and modify the Schema file.
优选地,步骤S2中,根据RFC7047规范定义解析后的数据格式。Preferably, in step S2, the parsed data format is defined according to the RFC7047 specification.
优选地,步骤S2中,调用Go标准库json反序列化函数将Schema文件中的内容解析到定义的数据格式中。Preferably, in step S2, the json deserialization function of the Go standard library is called to parse the content in the Schema file into a defined data format.
优选地,执行步骤S31前,创建生成代码的存放路径目录,若该目录已存在则清空该目录下的旧文件。Preferably, before step S31 is performed, a storage path directory of the generated code is created, and if the directory already exists, the old files in the directory are cleared.
优选地,步骤S33包括操作:使用gofmt工具对代码进行格式化,包括换行、空格、括号匹配;使用goimport工具对代码进行格式化,导入依赖包。Preferably, step S33 includes operations: using the gofmt tool to format the code, including line feed, space, and bracket matching; using the goimport tool to format the code and import the dependency package.
一种OVSDB客户端代码自动生成系统,其特征在于,包括:An OVSDB client code automatic generation system is characterized in that, comprising:
合法性检查模块:用于获取待解析的Schema文件,并对Schema文件进行合法性检查,Schema文件中定义了包括数据库的属性和数据库中各表项的属性;Validity check module: used to obtain the schema file to be parsed, and check the validity of the schema file. The schema file defines attributes including the database attributes and the attributes of each table item in the database;
解析模块:定义解析后的数据格式,将合法的Schema文件中的内容解析到该数据格式中,得到解析后的数据;Parsing module: Define the parsed data format, parse the content of the legal Schema file into the data format, and obtain the parsed data;
代码生成模块:基于解析后的数据和模板代码文件生成数据库Go语言代码,其中模板代码文件中包括预先生成的代码模板,包括:Code generation module: Generate database Go language code based on the parsed data and template code files, where the template code files include pre-generated code templates, including:
公共代码生成模块:遍历解析后的数据,根据数据库属性,结合模板代码文件生成公共代码;Public code generation module: traverse the parsed data, generate public code according to the database attributes, combined with the template code file;
表项代码生成模块:遍历解析后的数据,根据各表项属性,结合模板代码文件生成每个表项的API代码;Table item code generation module: traverse the parsed data, generate the API code of each table item according to the properties of each table item, combined with the template code file;
格式化模块:对公共代码和每个表项的API代码进行格式化,得到最终的数据库Go语言代码。Formatting module: Format the common code and the API code of each table item to get the final database Go language code.
优选地,合法性检查模块对Schema文件进行合法性检查包括:Schema文件的打开和读取是否正常、Schema文件内容是否符合Json格式语法、Schema文件内容中的相关关键字是否符合RFC7047规范、是否能够使用ovsdb-tool工具将Schema文件创建为临时数据库文件。Preferably, the validity check of the schema file by the validity check module includes: whether the schema file is opened and read normally, whether the content of the schema file conforms to the Json format syntax, whether the relevant keywords in the content of the schema file conform to the RFC7047 specification, and whether it can Use the ovsdb-tool tool to create the Schema file as a temporary database file.
优选地,若Schema文件进行合法性检查通过,合法性检查模块删除临时数据库文件;若Schema文件进行合法性检查未通过,合法性检查模块返回具体错误信息并指导开发人员定位修改Schema文件。Preferably, if the Schema file passes the validity check, the validity check module deletes the temporary database file; if the Schema file fails the validity check, the validity check module returns specific error information and guides developers to locate and modify the Schema file.
优选地,解析模块根据RFC7047规范定义解析后的数据格式。Preferably, the parsing module defines the parsed data format according to the RFC7047 specification.
优选地,解析模块调用Go标准库json反序列化函数将Schema文件中的内容解析到定义的数据格式中。Preferably, the parsing module calls the json deserialization function of the Go standard library to parse the content in the schema file into a defined data format.
优选地,代码生成模块还包括目录创建模块,创建生成代码的存放路径目录,若该目录已存在则清空该目录下的旧文件。Preferably, the code generation module further includes a directory creation module, which creates a storage path directory for the generated code, and clears old files in the directory if the directory already exists.
优选地,格式化模块执行包括以下操作:使用gofmt工具对代码进行格式化,包括换行、空格、括号匹配;使用goimport工具对代码进行格式化,导入依赖包。Preferably, the execution of the formatting module includes the following operations: using the gofmt tool to format the code, including matching of newlines, spaces, and brackets; using the goimport tool to format the code and import the dependency package.
一种OVSDB客户端代码自动生成设备,包括处理器、存储器及存储在存储器上并可在处理器上运行的计算机程序,其特征在于,所述处理器执行所述程序时实现上述任意一项所述OVSDB客户端代码自动生成方法。A device for automatically generating OVSDB client code, comprising a processor, a memory, and a computer program stored in the memory and running on the processor, wherein the processor implements any of the above when executing the program. Describe the automatic generation method of OVSDB client code.
一种计算机可读存储介质,其上存储有计算机程序,其特征在于,该程序被处理器执行时实现上述任意一项所述OVSDB客户端代码自动生成方法。A computer-readable storage medium on which a computer program is stored, characterized in that, when the program is executed by a processor, the method for automatically generating an OVSDB client code described in any one of the above is implemented.
有益效果:本发明具有如下有益效果:Beneficial effects: the present invention has the following beneficial effects:
本发明使用Go Json解析包反序列化OVSDB Schema文件,提取关键数据库、数据库表项、表项字段等详细信息,将OVSDB数据库进行分类,使用预先写好的模板代码文件生成所有代码,通过生成OVSDB各表项增删改查API避免用户直接构造数据库操作代码,使用结构化的表项数据避免非结构化的字符串映射,降低了Go语言项目集成OVSDB的难度,增加了代码可读性和可维护性。The present invention uses the Go Json parsing package to deserialize the OVSDB Schema file, extracts detailed information such as key databases, database table items, table item fields, etc., classifies the OVSDB database, uses pre-written template code files to generate all codes, and generates OVSDB by generating OVSDB. The API for adding, deleting, modifying and checking each table entry prevents users from directly constructing database operation codes, and uses structured table item data to avoid unstructured string mapping, which reduces the difficulty of integrating OVSDB in Go language projects, and increases code readability and maintainability. sex.
附图说明Description of drawings
图1为本发明的代码自动生成流程图;Fig. 1 is the code automatic generation flow chart of the present invention;
图2为本发明中对Schema文件进行合法性检查的流程;Fig. 2 is the process flow that the Schema file is checked for legitimacy in the present invention;
图3为本发明中Json解析后的数据格式;Fig. 3 is the data format after Json analysis in the present invention;
图4为本发明中各类表项可支持的API列表。FIG. 4 is a list of APIs supported by various entries in the present invention.
具体实施方式Detailed ways
下面结合附图对本发明作更进一步的说明。The present invention will be further described below in conjunction with the accompanying drawings.
本发明公开了一种OVSDB客户端代码自动生成方法,使用Go Json解析包反序列化OVSDB Schema文件,提取关键数据库、数据库表项、表项字段等详细信息,将OVSDB数据库分类,使用预先写好的模板代码文件生成所有代码,代码生成流程如图1所示,包括如下步骤:The invention discloses an OVSDB client code automatic generation method, which uses the Go Json parsing package to deserialize the OVSDB Schema file, extracts detailed information such as key databases, database table items, table item fields, etc., classifies the OVSDB database, and uses the pre-written OVSDB database. The template code file for generating all codes, the code generation process is shown in Figure 1, including the following steps:
步骤S1、Schema文件合法性检查Step S1, Schema file validity check
首先获取待解析的OVSDB Schema文件,该Schema文件为数据库对象的集合,其中根据业务需求定义了数据库中的表项结构、属性及各表项之间的关系,内容包括数据库名、数据库版本号、表项名以及各个表项的信息,各个表项的信息包括最大行数以及表项中字段的数据类型、持久化属性、最大最小值、长度范围、取值范围等。First, obtain the OVSDB Schema file to be parsed. The Schema file is a collection of database objects, in which the table entry structure, attributes and relationships between table entries in the database are defined according to business requirements, including database name, database version number, The table item name and information about each table item, including the maximum number of rows and the data type, persistent attribute, maximum and minimum value, length range, value range, etc. of the fields in the table item.
之后对Schema文件做合法性检查,如图2所示,包括如下步骤:After that, check the validity of the Schema file, as shown in Figure 2, including the following steps:
步骤S11、检查Schema文件是否存在,以及Schema文件的打开和读取是否正常;Step S11, check whether the Schema file exists, and whether the opening and reading of the Schema file are normal;
步骤S12、步骤S11的检查通过后,检查Schema文件内容是否符合Json格式语法;Step S12, after the check of step S11 is passed, check whether the content of the Schema file conforms to the Json format syntax;
步骤S13、步骤S12的检查通过后,检查Schema文件内容中的相关关键字是否符合RFC7047规范;Step S13, after the check of step S12 is passed, check whether the relevant keywords in the content of the Schema file conform to the RFC7047 specification;
步骤S14、步骤S13的检查通过后,检查是否能够使用ovsdb-tool工具将Schema文件创建为临时数据库文件;After the checks of step S14 and step S13 are passed, check whether the Schema file can be created as a temporary database file using the ovsdb-tool tool;
步骤S15、步骤S14的检查通过后,Schema文件合法性检查结束,删除临时数据库文件,进入后续流程。After the checks in step S15 and step S14 are passed, the validity check of the schema file ends, the temporary database file is deleted, and the subsequent process is entered.
在步骤S11至步骤S14中,任何一步失败即检查未通过,则返回具体错误信息并指导开发人员定位修改Schema文件。In step S11 to step S14, if any step fails, that is, the check fails, a specific error message is returned and the developer is instructed to locate and modify the Schema file.
修改Schema文件后重新生成代码,相关代码的修改可以用编译器甚至编辑器检查出来。After modifying the Schema file, the code is regenerated, and the modification of the relevant code can be checked with the compiler or even the editor.
步骤S2、Json解析Step S2, Json parsing
步骤S21、根据RFC7047规范定义Json解析后的数据格式。Step S21, define the data format after Json parsing according to the RFC7047 specification.
在本发明的一种实施例中,如图3所示,解析后的数据将保存在结构体DatabaseSchema中做进一步处理。In an embodiment of the present invention, as shown in FIG. 3 , the parsed data will be stored in the structure DatabaseSchema for further processing.
步骤S22、调用Go标准库json反序列化方法Unmarshal将Schema文件内容解析到DatabaseSchema结构体中,得到解析后的数据。Step S22, calling the Go standard library json deserialization method Unmarshal to parse the content of the Schema file into the DatabaseSchema structure to obtain the parsed data.
结构体DatabaseSchema中包括:数据库名、数据库版本号和各表项子结构TableSchema;各表项子结构TableSchema中包括:具体表项的索引、最大行数、是否根表项属性以及各字段子结构ColumnSchema;各字段子结构ColumnSchema中包括:具体字段的数据类型以及字段持久化属性,具体字段的数据类型可以是interger,string,uuid等。The structure DatabaseSchema includes: database name, database version number, and the substructure TableSchema of each table item; the substructure TableSchema of each table item includes: the index of the specific table item, the maximum number of rows, whether the attribute of the root table item or not, and the substructure ColumnSchema of each field ; Each field substructure ColumnSchema includes: the data type of the specific field and the field persistence attribute, the data type of the specific field can be interger, string, uuid, etc.
从Schema文件中提取键值对,键是Schema文件中字段类型的名称,值是从Schema文件中提取到的具体字段类型的值,使用go语言提供的反射机制来处理非结构化键值对(mapping)向结构体(struct)转换,具体方法是使用定义生成的结构体通过反射获取其结构体成员的类型,然后遍历结构体成员根据其类型从非结构化键值对中获取对应的值写入结构体变量中。Extract key-value pairs from the Schema file. The key is the name of the field type in the Schema file, and the value is the value of the specific field type extracted from the Schema file. The reflection mechanism provided by the go language is used to process unstructured key-value pairs ( mapping) to a structure (struct), the specific method is to use the structure generated by the definition to obtain the type of its structure members through reflection, and then traverse the structure members to obtain the corresponding value from the unstructured key-value pair according to its type and write into a structure variable.
根据RFC规范的表项有时无法完全满足业务需求,因此需要对表项的属性进行拓展。例如:对表项中某些字段增加默认值属性,在解析过程中获得其默认值并生成对应的常量,在后续代码中可以直接用此常量写入表项中;拓展的方法是在Schema文件中表项的具体字段属性中添加一个json map标签去描述该默认值属性,如max_number字段属性内添加“default” : 1000,表项内最大值的默认值为1000。The table items according to the RFC specification sometimes cannot fully meet the business requirements, so the attributes of the table items need to be expanded. For example: add a default value attribute to some fields in the table item, obtain its default value during the parsing process and generate the corresponding constant, which can be directly written into the table item in the subsequent code; the extension method is to add the default value in the Schema file Add a json map tag to the specific field attribute of the table entry to describe the default value attribute. For example, add "default": 1000 to the max_number field attribute, and the default value of the maximum value in the table item is 1000.
对于数据库中每一个表项,根据其属性将其区分为Global、Root和非Root表项三种类型,其中:For each table entry in the database, it is divided into three types: Global, Root and non-Root table entries according to its attributes, among which:
Global表项:最大行数为1,全局一个表行,不需要定义索引,用于存储系统或业务全局信息;Global table item: the maximum number of rows is 1, one table row globally, no need to define an index, it is used to store system or business global information;
Root表项:root属性为true,需要定义索引,可引用其他表项,支持大多数API;Root entry: The root attribute is true, an index needs to be defined, other entries can be referenced, and most APIs are supported;
非Root表项:必须由至少一个其他表项引用,不支持独立的创建删除操作。Non-Root entry: must be referenced by at least one other entry, independent create and delete operations are not supported.
Global、Root和非Root表项分别具体可支持的API如图4所示。The specific APIs supported by Global, Root, and non-Root entries are shown in Figure 4.
步骤S3、代码生成Step S3, code generation
基于解析后的数据和模板代码文件生成数据库Go语言代码,其中模板代码文件中包括预先生成的代码模板,包括步骤:Generate database Go language code based on the parsed data and template code file, wherein the template code file includes pre-generated code templates, including steps:
步骤S31、创建生成代码的存放路径目录,若该目录已存在则清空旧文件。Step S31 , creating a storage path directory of the generated code, and clearing the old files if the directory already exists.
步骤S32、根据数据库属性,结合模板代码文件生成相关公共代码并保存到单独的go文件中,包括:数据库名、表项名定义;数据库客户端连接初始化函数;通用公共常量及函数;数据库默认值map初始化;OVSDB Transact操作公共函数等。Step S32, according to the database attributes, combined with the template code file to generate relevant public code and save it in a separate go file, including: database name, table item name definition; database client connection initialization function; general public constants and functions; database default value map initialization; OVSDB Transact operates public functions, etc.
公共代码包括公共函数和非公共函数,其中,公共函数的名称不变,公共函数可以直接通过复制模板代码文件生成;非公共函数的名称根据Schema 文件内容变化,非公共函数可以通过修改模板代码文件中的部分内容生成,比如说修改部分字符串、数值。Public codes include public functions and non-public functions. The names of public functions remain unchanged, and public functions can be directly generated by copying the template code file; the names of non-public functions change according to the content of the Schema file, and non-public functions can be modified by modifying the template code file. Part of the content in the generation, such as modifying part of the string, value.
步骤S33、遍历所有表项,根据最大行数以及是否根表项属性,结合模板代码文件生成每个表项的API代码,API代码提供通用功能集,通过调用API代码对对应的表项进行开发,包括:表项数据结构;表项字段定义;表项索引数据结构(可以有多组);int类型字段的最大最小值范围定义;枚举类型可取值定义;表项增删改查、遍历、清空操作API函数;对于可导出的变量或函数生成适当的注释。Step S33, traverse all the table items, generate the API code of each table item according to the maximum number of rows and the attribute of the root table item, combined with the template code file, the API code provides a general function set, and the corresponding table item is developed by calling the API code , including: table entry data structure; table entry field definition; table entry index data structure (there can be multiple groups); definition of the maximum and minimum range of int type fields; , Clear operation API functions; generate appropriate comments for exportable variables or functions.
将各表项的API代码保存到各自表项单独的go源文件中,命名为table_tablename.go。Save the API code of each table item to a separate go source file for each table item, named table_tablename.go.
对于不同类型的表项,模板代码文件中有不同的API代码模板,根据解析出的表项名称、字段类型等属性,替换模板代码中的相应字符串可生成对应表项的API代码。For different types of table items, there are different API code templates in the template code file. According to the parsed table item name, field type and other attributes, the corresponding string in the template code can be replaced to generate the API code of the corresponding table item.
步骤S34、格式化代码,包括操作:Step S34, formatting the code, including operations:
对所有的生成代码使用gofmt工具做格式化,使代码具有统一的格式(包括换行、空格、括号匹配等);Use the gofmt tool to format all the generated code, so that the code has a uniform format (including line breaks, spaces, bracket matching, etc.);
使用goimport工具导入依赖包,对代码再次格式化,得到最终的数据库Go语言代码。Use the goimport tool to import the dependency package, format the code again, and get the final database Go language code.
利用本发明所述方法生成的代码中,关于表项的索引,OVSDB允许表项定义多个组合索引,且没有限制索引字段的修改,实际使用中在添加表项的时候应该指定索引字段的值,并且后续不做修改;所有非global表项至少指定一个索引。In the code generated by the method of the present invention, regarding the index of the table item, OVSDB allows the table item to define multiple combined indexes, and does not limit the modification of the index field. In actual use, the value of the index field should be specified when adding the table item. , and no subsequent modifications are made; all non-global entries specify at least one index.
对于数组类型的字段,当未指定最大最小值或者指定最大值为1时,表项结构内成员优化成基本类型,同时保留其更新操作。For fields of array type, when the maximum and minimum values are not specified or the specified maximum value is 1, the members in the table entry structure are optimized to the basic type, while the update operation is retained.
关于浮点数,OVSDB json可定义的基本数据类型包括“real”,json解析的时候所有数据类型都转成了float64类型,需要将其转换成int类型。Regarding floating-point numbers, the basic data types that can be defined by OVSDB json include "real". When json is parsed, all data types are converted to float64 type, which needs to be converted to int type.
数据选择操作conditions支持更多的操作符“<”、“<=”、“==”、“!=”、“>=”以及“>”,Mutate针对数字也支持“+=”、“-=”、“*=”、“/=”或“%=”等操作,这些没有目前通用的API,可使用封装好的MutateRows函数去实现。Data selection operation conditions support more operators "<", "<=", "==", "!=", ">=" and ">", Mutate also supports "+=", "-" for numbers =", "*=", "/=" or "%=", etc., these operations do not have a current general API, and can be implemented using the encapsulated MutateRows function.
下面利用具体实施例进行说明:The following uses specific embodiments to illustrate:
1、首先需要有一个数据库Schema文件,保存为”ovsdb.ovsschema”文件名,准备的Schema文件如下:1. First, you need to have a database schema file, save it as "ovsdb.ovsschema" file name, the prepared schema file is as follows:
{{
"name": "OVS_DB", "name": "OVS_DB",
"tables": { "tables": {
"Table_A": { "Table_A": {
"columns": { "columns": {
"name": {"type": "string"}, "name": {"type": "string"},
"num_id_1": {"type": "integer"}, "num_id_1": {"type": "integer"},
"enable": {"type": "boolean"}, "enable": {"type": "boolean"},
"reftable": { "reftable": {
"type": {"key": {"type": "uuid", "refTable": "Table_B"}, "type": {"key": {"type": "uuid", "refTable": "Table_B"},
"min": 0, "max": "unlimited"}}, "min": 0, "max": "unlimited"}},
"set": { "set": {
"type": {"key": {"type": "string", "enum": ["set", "type": {"key": {"type": "string", "enum": ["set",
["physical_port", "lag", "vlan", "bridge_domain", "sub_port"]], ["physical_port", "lag", "vlan", "bridge_domain", "sub_port"]],
"default": "physical_port"}, "min": 1, "max": 128}}, "default": "physical_port"}, "min": 1, "max": 128}},
"number": { "number": {
"type": {"key": {"type": "integer", "minInteger": 0, "maxInteger": 4294967295, "default": 1000}, "min": 0, "max": 1}}, "type": {"key": {"type": "integer", "minInteger": 0, "maxInteger": 4294967295, "default": 1000}, "min": 0, "max": 1}} ,
"map": { "map": {
"type": {"key": "string", "value": "string", "type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}, "min": 0, "max": "unlimited"}}},
"indexes": [["name"], ["name", "num_id_1"]], "indexes": [["name"], ["name", "num_id_1"]],
"isRoot": true}, "isRoot": true},
"Table_B": { "Table_B": {
"columns": { "columns": {
"name": {"type": "string"}, "name": {"type": "string"},
"enable": { "enable": {
"type": {"key": {"type": "boolean", "default": true}, "min": 0, "max": 1}}}, "type": {"key": {"type": "boolean", "default": true}, "min": 0, "max": 1}}},
"indexes": [["name"]], "indexes": [["name"]],
"isRoot": false}, "isRoot": false},
"Table_Global": { "Table_Global": {
"columns": { "columns": {
"name": {"type": "string"}, "name": {"type": "string"},
"set": { "set": {
"type": {"key": {"type": "string"}, "min": 1, "max": 128}}, "type": {"key": {"type": "string"}, "min": 1, "max": 128}},
"map": { "map": {
"type": {"key": "string", "value": "string", "type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}}, "min": 0, "max": "unlimited"}}},
"maxRows": 1, "maxRows": 1,
"isRoot": true} "isRoot": true}
}, },
"version": "1.0.0" "version": "1.0.0"
}}
2、执行本代码自动生成方法:检查Schema文件合法性、对Schema文件内容进行Json解析、结合模板代码文件生成数据库Go语言代码并对其进行格式化:2. Execute this automatic code generation method: check the legality of the Schema file, perform Json parsing on the content of the Schema file, generate the database Go language code combined with the template code file and format it:
go run odbgen.go -f ovsdb.ovsschemago run odbgen.go -f ovsdb.ovsschema
3、在项目lib/odbapi/ovsdb目录下检查生成的Go代码。3. Check the generated Go code in the project lib/odbapi/ovsdb directory.
项目中使用生成的Go代码进行业务开发示例如下,包括:Examples of business development using the generated Go code in the project are as follows, including:
数据库客户端连接初始化:Database client connection initialization:
ovsdb.InitOvsdb(addrStr)ovsdb.InitOvsdb(addrStr)
创建表项:Create table entry:
tableA := ovsdb.TableTableA{tableA := ovsdb.TableTableA{
Name: “A1”, Name: "A1",
NumID1: “8”, NumID1: "8",
Enable: true, Enable: true,
Number:11111, Number: 11111,
}}
tableAUUID, err := ovsdb.TableAAdd(tableA)tableAUUID, err := ovsdb.TableAAdd(tableA)
设置表项字段:Set the table entry fields:
ovsdb.TableASetField(indexA, ovsdb.TableAFieldSet, “A100”)ovsdb.TableASetField(indexA, ovsdb.TableAFieldSet, "A100")
ovsdb.TableASetField(indexA,ovsdb.TableAFieldNumber, 33333)ovsdb.TableASetField(indexA,ovsdb.TableAFieldNumber, 33333)
获取表项:Get table entry:
indexA := ovsdb.TableAIndex{indexA := ovsdb.TableAIndex{
Name: “A1”, Name: "A1",
}}
tableA, err := ovsdb.TableAGetByIndex(indexA)tableA, err := ovsdb.TableAGetByIndex(indexA)
遍历数据库:Traverse the database:
funcprintA(tableATableA){funcprintA(tableATableA){
fmt.printf(“A: %v\n”, tableA) fmt.printf("A: %v\n", tableA)
}}
ovsdb.TableAIterator(printA)ovsdb.TableAIterator(printA)
删除清空数据:Delete clear data:
ovsdb.TableADelByIndex(indexA)ovsdb.TableADelByIndex(indexA)
ovsdb.TableAClear()ovsdb.TableAClear()
本发明公开了一种OVSDB客户端代码自动生成系统,包括:The invention discloses an OVSDB client code automatic generation system, comprising:
合法性检查模块:用于获取待解析的Schema文件,并对Schema文件进行合法性检查,Schema文件中定义了包括数据库的属性和数据库中各表项的属性;Validity check module: used to obtain the schema file to be parsed, and check the validity of the schema file. The schema file defines attributes including the database attributes and the attributes of each table item in the database;
解析模块:根据RFC7047规范定义解析后的数据格式,将合法的Schema文件中的内容解析到该数据格式中,得到解析后的数据;Parsing module: Define the parsed data format according to the RFC7047 specification, parse the content of the legal Schema file into the data format, and obtain the parsed data;
代码生成模块:基于解析后的数据和模板代码文件生成数据库Go语言代码,其中模板代码文件中包括预先生成的代码模板,包括:Code generation module: Generate database Go language code based on the parsed data and template code files, where the template code files include pre-generated code templates, including:
目录创建模块,创建生成代码的存放路径目录,若该目录已存在则清空该目录下的旧文件;The directory creation module creates the storage path directory of the generated code, and if the directory already exists, clears the old files in the directory;
公共代码生成模块:遍历解析后的数据,根据数据库属性,结合模板代码文件生成公共代码;Public code generation module: traverse the parsed data, generate public code according to the database attributes, combined with the template code file;
表项代码生成模块:遍历解析后的数据,根据各表项属性,结合模板代码文件生成每个表项的API代码,并将每个表项的API代码保存为该表项单独的go源文件;Table entry code generation module: traverse the parsed data, generate the API code of each table entry according to the attributes of each table item, combined with the template code file, and save the API code of each table item as a separate go source file for the table item ;
格式化模块:对公共代码和每个表项的API代码进行格式化,得到最终的数据库Go语言代码。Formatting module: Format the common code and the API code of each table item to get the final database Go language code.
其中,合法性检查模块对Schema文件进行合法性检查包括:Schema文件的打开和读取是否正常、Schema文件内容是否符合Json格式语法、Schema文件内容中的相关关键字是否符合RFC7047规范、是否能够使用ovsdb-tool工具将Schema文件创建为临时数据库文件。若Schema文件进行合法性检查通过,合法性检查模块删除临时数据库文件;若Schema文件进行合法性检查未通过,合法性检查模块返回具体错误信息并指导开发人员定位修改Schema文件。Among them, the legitimacy check module checks the legitimacy of the Schema file, including: whether the opening and reading of the Schema file are normal, whether the content of the Schema file conforms to the Json format syntax, whether the relevant keywords in the content of the Schema file conform to the RFC7047 specification, and whether it can be used. The ovsdb-tool tool creates schema files as temporary database files. If the schema file passes the validity check, the validity check module deletes the temporary database file; if the schema file fails the validity check, the validity check module returns a specific error message and guides developers to locate and modify the schema file.
解析模块根据RFC7047规范定义解析后的数据格式,解析模块调用Go标准库json反序列化函数将Schema文件中的内容解析到该数据格式中。The parsing module defines the parsed data format according to the RFC7047 specification, and the parsing module calls the Go standard library json deserialization function to parse the content in the Schema file into the data format.
格式化模块执行包括以下操作:gofmt工具对代码进行格式化,包括换行、空格、括号匹配;使用goimport工具对代码进行格式化,导入依赖包。The execution of the formatting module includes the following operations: the gofmt tool formats the code, including line breaks, spaces, and bracket matching; uses the goimport tool to format the code and import dependent packages.
本发明公开了一种OVSDB客户端代码自动生成设备,包括处理器、存储器及存储在存储器上并可在处理器上运行的计算机程序,所述处理器执行所述程序时实现上述OVSDB客户端代码自动生成方法。The invention discloses an OVSDB client code automatic generating device, comprising a processor, a memory and a computer program stored in the memory and running on the processor, the processor implements the above OVSDB client code when the processor executes the program Automatically generate methods.
本发明公开了一种计算机可读存储介质,其上存储有计算机程序,该程序被处理器执行时实现上述OVSDB客户端代码自动生成方法。The present invention discloses a computer-readable storage medium on which a computer program is stored. When the program is executed by a processor, the above-mentioned OVSDB client code automatic generation method is realized.
以上所述仅是本发明的优选实施方式,应当指出:对于本技术领域的普通技术人员来说,在不脱离本发明原理的前提下,还可以做出若干改进和润饰,这些改进和润饰也应视为本发明的保护范围。The above is only the preferred embodiment of the present invention, it should be pointed out that: for those skilled in the art, without departing from the principle of the present invention, several improvements and modifications can also be made, and these improvements and modifications are also It should be regarded as the protection scope of the present invention.
Claims (16)
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011127502.2A CN112235311B (en) | 2020-10-20 | 2020-10-20 | OVSDB client code automatic generation method, system, device and medium |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202011127502.2A CN112235311B (en) | 2020-10-20 | 2020-10-20 | OVSDB client code automatic generation method, system, device and medium |
Publications (2)
Publication Number | Publication Date |
---|---|
CN112235311A CN112235311A (en) | 2021-01-15 |
CN112235311B true CN112235311B (en) | 2022-08-05 |
Family
ID=74117533
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202011127502.2A Active CN112235311B (en) | 2020-10-20 | 2020-10-20 | OVSDB client code automatic generation method, system, device and medium |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN112235311B (en) |
Families Citing this family (7)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN113791769A (en) * | 2021-09-23 | 2021-12-14 | 上海淇玥信息技术有限公司 | Data value taking method and device for complex structure and electronic equipment |
CN114201229B (en) * | 2021-11-04 | 2024-04-05 | 智新控制系统有限公司 | LIN configuration code automatic generation method, system and computer readable storage medium |
CN114356341B (en) * | 2022-01-04 | 2024-09-13 | 腾讯科技(成都)有限公司 | A data processing method, device, equipment, storage medium and product |
CN114995796B (en) * | 2022-05-30 | 2024-07-19 | 宁波三星医疗电气股份有限公司 | A-XDR (advanced digital Signal processor) coding message processing method, storage medium and electronic equipment |
CN115129714B (en) * | 2022-06-24 | 2025-06-06 | 上交所技术有限责任公司 | A data table generation method based on intrusive container and code generation technology |
CN115495442B (en) * | 2022-11-16 | 2023-08-04 | 深圳市明源云采购科技有限公司 | Database operation file generation method, device, equipment and storage medium |
CN116719866B (en) * | 2023-05-09 | 2024-02-13 | 上海银满仓数字科技有限公司 | Multi-format data self-adaptive distribution method and system |
Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109039701A (en) * | 2018-06-25 | 2018-12-18 | 烽火通信科技股份有限公司 | The method and system of a variety of way to manages of the network equipment based on MIB database |
EP3675423A1 (en) * | 2018-12-26 | 2020-07-01 | Juniper Networks, Inc. | Cloud network having multiple protocols using virtualization overlays across physical and virtualized workloads |
Family Cites Families (1)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US11153157B2 (en) * | 2018-03-12 | 2021-10-19 | Stellar Cyber, Inc. | Configuration management using OVSDB protocol |
-
2020
- 2020-10-20 CN CN202011127502.2A patent/CN112235311B/en active Active
Patent Citations (2)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN109039701A (en) * | 2018-06-25 | 2018-12-18 | 烽火通信科技股份有限公司 | The method and system of a variety of way to manages of the network equipment based on MIB database |
EP3675423A1 (en) * | 2018-12-26 | 2020-07-01 | Juniper Networks, Inc. | Cloud network having multiple protocols using virtualization overlays across physical and virtualized workloads |
Also Published As
Publication number | Publication date |
---|---|
CN112235311A (en) | 2021-01-15 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
CN112235311B (en) | OVSDB client code automatic generation method, system, device and medium | |
WO2020233367A1 (en) | Blockchain data storage and query method, apparatus and device, and storage medium | |
US7822710B1 (en) | System and method for data collection | |
US9009195B2 (en) | Software framework that facilitates design and implementation of database applications | |
US7512614B2 (en) | System and method for data ETL in a data warehouse environment | |
CN100535894C (en) | Database object script generation method and system | |
US7933913B2 (en) | Secondary index and indexed view maintenance for updates to complex types | |
US6611844B1 (en) | Method and system for java program storing database object entries in an intermediate form between textual form and an object-oriented form | |
US9424003B1 (en) | Schema-less system output object parser and code generator | |
US8230384B1 (en) | Techniques for generating and processing a schema instance | |
US7120869B2 (en) | Enhanced mechanism for automatically generating a transformation document | |
US8635594B1 (en) | Script language for storage management operations | |
US7143108B1 (en) | Apparatus and method for deletion of objects from an object-relational system in a customizable and database independent manner | |
US20090254881A1 (en) | Code generation techniques for administrative tasks | |
US8141041B2 (en) | Automated configuration updater and method for code builder | |
WO2008099393A2 (en) | Service provisioning system | |
US20060122973A1 (en) | Mechanism for defining queries in terms of data objects | |
US9026908B2 (en) | Systems and methods for providing simultaneous access to documents | |
CN114356964A (en) | Data lineage construction method, device, storage medium and electronic device | |
EP0839358B1 (en) | Emulator for an sql relational-database | |
WO2023151239A1 (en) | Micro-service creation method and related device | |
US7490098B2 (en) | Apparatus, system, and method for processing hierarchical data in disparate data repositories | |
US7958154B2 (en) | Apparatus, system, and method for command manager support for pluggable data formats | |
US8356085B2 (en) | Automated transformation of specifications for devices into executable modules | |
CN112988278B (en) | Meta file modification method and device of resource file, electronic equipment and storage medium |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
GR01 | Patent grant | ||
GR01 | Patent grant | ||
CP03 | Change of name, title or address | ||
CP03 | Change of name, title or address |
Address after: No. 9 Mozhou East Road, Nanjing City, Jiangsu Province, 211111 Patentee after: Zijinshan Laboratory Country or region after: China Address before: No. 9 Mozhou East Road, Jiangning Economic Development Zone, Jiangning District, Nanjing City, Jiangsu Province Patentee before: Purple Mountain Laboratories Country or region before: China |