Background
Protocols in an SDN network may be divided into management plane protocols and control plane protocols by function. The SDN controller is used as a boundary, and the interface can be divided into a south interface and a north interface according to the hierarchy of the programmable interfaces. The OpenFlow protocol is strictly a control-layer southbound interface protocol, and the OVSDB management protocol is a management-layer southbound interface protocol.
The OVSDB Management Protocol (Open vSwitch Database Management Protocol) was originally proposed by VMware corporation, and an Open Virtual Switch Database (OVSDB) responsible for managing an Open-source software switch (OpenvSwitch, OVS) is an SDN Management Protocol for implementing programmable access and configuration Management of a virtual switch. The OVSDB management protocol defines a set of RPC interfaces, and a user can manage the OVSDB in a remote calling mode, and mainly comprises a communication protocol (JSON-RPC) method and supported OVSDB operation. The OVS is a main application of the OVSDB, the data mode of the OVSDB is defined by OVSDB Schema (DB-SCHEMA), the switch supports data interaction with the controller through the OVSDB, the data are stored in the OVSDB database in the form of tables, the tables are OVSDB Schema tables, and when the switch and the controller are both OVSDB clients (communicating with an ovsddb database server through an ovsddb management protocol), rows can be added in the tables, and addition, deletion and modification of the rows can be subscribed.
With the development of SDN and virtualization technologies, virtual switches have gradually replaced some traditional hardware switches by virtue of flexible, elastic, and economical characteristics, and occupy a place in a data center network. The OVSDB also optimizes and strengthens and supplements the function short boards continuously, such as safety, compatibility to a protocol stack, disaster tolerance, escape and the like. The OVSDB is used as a database to define a database table structure and the table item relationship of each member based on the Schema of the JSON format, and the characteristics of supporting RPC interface access and the like can also be widely applied as a general database.
The OVSDB management protocol in the prior art has the following problems:
1. the operation of a large amount of hard codes and high repeatability of the database exists, and the operation is complicated and easy to make mistakes;
2. in the service development stage, the table entry fields of the database are newly and frequently modified, codes needing to be modified can not be found in time, and BUGs are left;
3. the ovsdb-client operation command line needs to construct a database operation by itself, and is troublesome to use and cannot realize the rapid generation of test data.
Currently, OVSDB is integrated within OVS projects using c language to implement its server code and client code.
Disclosure of Invention
The purpose of the invention is as follows: aiming at the problems in the prior art, the invention discloses a method, a system, equipment and a medium for automatically generating OVSDB client codes, which automatically generate the Go language codes of a defined related database by using a database Schema file, wherein the Go language codes comprise client connection, each table item operation API, a table item data structure and related member range definition, thereby reducing the complexity of using OVSDB by Go language users and improving the readability and maintainability of the codes.
The technical scheme is as follows: the invention adopts the following technical scheme: an OVSDB client code automatic generation method is characterized by comprising the following steps:
s1, obtaining a Schema file to be analyzed, and checking the validity of the Schema file, wherein the Schema file defines attributes including the attributes of the database and the attributes of each item in the database;
s2, defining the analyzed data format, and analyzing the content in the legal Schema file into the data format to obtain the analyzed data;
s3, generating a database Go language code based on the analyzed data and a template code file, wherein the template code file comprises a pre-generated code template, and the method comprises the following steps:
s31, traversing the analyzed data, and generating a public code by combining the template code file according to the database attribute;
s32, traversing the analyzed data, and generating an API code of each table entry by combining the template code file according to the attributes of each table entry;
and S33, formatting the public codes and the API codes of each table entry to obtain the final database Go language codes.
Preferably, in step S1, the checking the validity of the Schema file includes the operations of: checking whether the Schema file exists and whether the Schema file is opened and read normally; checking whether the content of the Schema file conforms to Json format grammar; checking whether relevant keywords in the contents of the Schema file conform to RFC7047 specifications; it is checked whether the Schema file can be created as a temporary database file using the ovsdb-tool.
Preferably, if the validity check of the Schema file is passed, deleting the temporary database file; and if the legality check of the Schema file fails, returning specific error information and guiding a developer to position and modify the Schema file.
Preferably, in step S2, the parsed data format is defined according to the RFC7047 specification.
Preferably, in step S2, the Go standard library json deserialization function is called to parse the content in the Schema file into the defined data format.
Preferably, before executing step S31, a storage path directory of the generated code is created, and if the directory already exists, the old file in the directory is cleared.
Preferably, step S33 includes the operations of: formatting the code by using a gofmt tool, wherein line feed, blank space and bracket matching are included; and formatting the code by using a goimport tool, and importing the dependency package.
An OVSDB client code automatic generation system, comprising:
a validity checking module: the method comprises the steps that the method is used for obtaining a Schema file to be analyzed and carrying out validity check on the Schema file, wherein the Schema file defines attributes comprising attributes of a database and attributes of each item in the database;
an analysis module: defining an analyzed data format, and analyzing the content in a legal Schema file into the data format to obtain analyzed data;
a code generation module: generating a database Go language code based on the analyzed data and a template code file, wherein the template code file comprises a pre-generated code template, and the method comprises the following steps:
a common code generation module: traversing the analyzed data, and generating a public code by combining a template code file according to the database attribute;
the table entry code generation module: traversing the analyzed data, and generating an API code of each table entry by combining the template code file according to the attribute of each table entry;
a formatting module: and formatting the public codes and the API codes of each table entry to obtain the final Go language code of the database.
Preferably, the legality checking module for legality checking the Schema file includes: whether the Schema file is normally opened and read, whether the Schema file content conforms to Json format syntax, whether related keywords in the Schema file content conform to RFC7047 specifications and whether the Schema file can be created into a temporary database file by using an ovsdb-tool.
Preferably, if the Schema file passes the validity check, the validity check module deletes the temporary database file; and if the Schema file fails to pass the validity check, the validity check module returns specific error information and guides a developer to position and modify the Schema file.
Preferably, the parsing module defines the parsed data format according to RFC7047 specifications.
Preferably, the parsing module calls a Go standard library json deserialization function to parse the content in the Schema file into the defined data format.
Preferably, the code generation module further comprises a directory creation module for creating a storage path directory of the generated code and clearing an old file under the directory if the directory already exists.
Preferably, the formatting module performs operations comprising: formatting the code by using a gofmt tool, wherein line feed, blank space and bracket matching are included; and formatting the code by using a goimport tool, and importing the dependency package.
An OVSDB client code automatic generation device comprises a processor, a memory and a computer program which is stored on the memory and can run on the processor, and is characterized in that the processor realizes any one of the OVSDB client code automatic generation methods when executing the program.
A computer-readable storage medium having stored thereon a computer program, wherein the program, when executed by a processor, implements any of the above OVSDB client code automatic generation methods.
Has the advantages that: the invention has the following beneficial effects:
according to the invention, the Go Json analysis packet deserialization OVSDB Schema file is used for extracting detailed information of a key database, database table entries, table entry fields and the like, classifying the OVSDB database, generating all codes by using a pre-written template code file, increasing and deleting the search API by generating each table entry of the OVSDB to avoid a user from directly constructing database operation codes, and using the structured table entry data to avoid unstructured character string mapping, so that the difficulty of integrating the OVSDB with the Go language item is reduced, and the code readability and maintainability are increased.
Detailed Description
The present invention will be further described with reference to the accompanying drawings.
The invention discloses an OVSDB client code automatic generation method, which uses a Go Json analysis packet deserialization OVSDB Schema file to extract detailed information of a key database, database table items, table item fields and the like, classifies the OVSDB database, and generates all codes by using a pre-written template code file, wherein the code generation process is shown as figure 1 and comprises the following steps:
step S1, checking the validity of Schema file
The method comprises the steps of firstly obtaining an OVSDB Schema file to be analyzed, wherein the Schema file is a set of database objects, the table item structure, the attribute and the relation among all table items in a database are defined according to service requirements, the content comprises a database name, a database version number, a table item name and information of all table items, and the information of all table items comprises the maximum line number, the data type, the persistence attribute, the maximum and minimum value, the length range, the value range and the like of fields in the table items.
Then, as shown in fig. 2, the method performs validity check on the Schema file, and includes the following steps:
step S11, checking whether the Schema file exists and whether the Schema file is opened and read normally;
step S12, after the check of step S11 is passed, checking whether the content of the Schema file conforms to Json format grammar;
after the check of the step S13 and the step S12 are passed, whether the related keywords in the content of the Schema file conform to the RFC7047 specification is checked;
step S14, after the check of step S13 is passed, checking whether the Schema file can be created as a temporary database file by using an ovsdb-tool;
and step S15 and step S14, the validity check of the Schema file is finished, the temporary database file is deleted, and the subsequent flow is entered.
In steps S11 to S14, if any step fails, i.e., the check fails, then specific error information is returned and the developer is guided to locate and modify the Schema file.
After the Schema file is modified, the code is regenerated, and the modification of the related code can be checked by a compiler or even an editor.
Step S2, Json analysis
And step S21, defining the data format after Json analysis according to the RFC7047 specification.
In an embodiment of the present invention, as shown in fig. 3, the parsed data is stored in the structure database schema for further processing.
And step S22, calling a Go standard library json deserialization method Unmarshal to analyze the Schema file content into a database Schema structure body to obtain analyzed data.
The structure database schema includes: database name, database version number and each table entry substructure TableSchema; each table entry substructure, TableSchema, includes: the index of the specific table entry, the maximum line number, whether the attribute of the root table entry is present or not and the substructures of the fields are ColumnSchema; each field substructure, ColumnSchema, includes: the data type of the specific field and the field persistence attribute, and the data type of the specific field may be interger, string, uuid, and the like.
Extracting a key value pair from the Schema file, wherein the key is the name of a field type in the Schema file, the value is the value of a specific field type extracted from the Schema file, processing the conversion from an unstructured key value pair (mapping) to a structure (struct) by using a reflection mechanism provided by a go language, and the specific method is that a structure generated by definition is used for obtaining the type of a structure member of the structure through reflection, and traversing the structure member to obtain a corresponding value from the unstructured key value pair according to the type of the structure member and writing the corresponding value into a structure variable.
The table entry according to the RFC specification sometimes cannot completely satisfy the service requirement, so the attribute of the table entry needs to be expanded. For example: adding default value attributes to some fields in the table entry, obtaining the default values in the analysis process and generating corresponding constants, and directly writing the constants into the table entry in the subsequent codes; the expanding method is that a json map label is added in the specific field attribute of the entry in the Schema file to describe the default value attribute, for example, 1000 is added in the max _ number field attribute, and the default value of the maximum value in the entry is 1000.
For each table entry in the database, distinguishing the table entry into three types, namely Global table entry, Root entry and non-Root table entry, according to the attribute of the table entry, wherein:
global table entry: the maximum line number is 1, one table line is in the whole state, and an index is not required to be defined and is used for storing system or service global information;
root table entry: the root attribute is true, an index needs to be defined, other table entries can be referred, and most APIs are supported;
non-Root table entry: must be referenced by at least one other entry and does not support a separate create-delete operation.
The APIs that are respectively and specifically supportable by Global, Root and non-Root entries are shown in fig. 3.
Step S3, code generation
Generating a database Go language code based on the analyzed data and a template code file, wherein the template code file comprises a pre-generated code template, and the method comprises the following steps:
step S31 is to create a storage path directory for the generated code, and if the directory already exists, the old file is emptied.
Step S32, generating related public codes by combining the template code file according to the database attributes and storing the related public codes into a single go file, wherein the steps comprise: defining database names and table entry names; the database client is connected with an initialization function; common constants and functions; initializing a database default value map; OVSDB Transact operates common functions, etc.
The public code comprises a public function and a non-public function, wherein the name of the public function is unchanged, and the public function can be directly generated by copying a template code file; the name of the non-public function changes according to the content of the Schema file, and the non-public function can be generated by modifying part of the content in the template code file, for example, modifying part of character strings and numerical values.
Step S33, traversing all the entries, generating an API code for each entry by combining the template code file according to the maximum number of lines and whether the entry attribute is root, where the API code provides a general function set, and developing the corresponding entry by calling the API code, including: a table entry data structure; defining table entry fields; table entry index data structures (there may be multiple sets); the definition of the maximum and minimum value range of the int type field; enumerating type acquirable value definitions; adding, deleting, modifying, checking, traversing and clearing an API function; appropriate annotations are generated for the derivable variables or functions.
And storing the API codes of the table entries into the source files of the respective table entries, namely table _ table.
For different types of entries, different API code templates exist in the template code file, and the API codes of corresponding entries can be generated by replacing corresponding character strings in the template codes according to attributes such as the names of the parsed entries, the types of the fields and the like.
Step S34, formatting the code, including the operations of:
formatting all generated codes by using a gofmt tool to enable the codes to have a uniform format (including line feed, blank space, bracket matching and the like);
and (5) importing the dependency package by using a goimport tool, and formatting the codes again to obtain the final Go language codes of the database.
In the codes generated by the method, the OVSDB allows the table entry to define a plurality of combined indexes, and does not limit the modification of the index field, and the value of the index field is specified when the table entry is added in actual use and is not modified subsequently; all non-global entries specify at least one index.
For fields of an array type, members within the table entry structure are optimized to be of a base type when no maximum minimum value is specified or a maximum value of 1 is specified, while preserving their update operations.
With respect to floating point numbers, OVSDB json definable primitive data types include "real", all data types are converted to float64 type at the time of json resolution, which needs to be converted to int type.
The data select operations conditions support further operators "<", "<" = "," =, "> =", and ">", and the state also supports operations such as "+ =", "- =", "/=", or "% =" for numbers, which do not have a currently common API, and can be implemented using a packaged muterows function.
The following is illustrated by means of specific examples:
1. firstly, a database Schema file is required to be stored as a file name of ovsdb.
{
"name": "OVS_DB",
"tables": {
"Table_A": {
"columns": {
"name": {"type": "string"},
"num_id_1": {"type": "integer"},
"enable": {"type": "boolean"},
"reftable": {
"type": {"key": {"type": "uuid", "refTable": "Table_B"},
"min": 0, "max": "unlimited"}},
"set": {
"type": {"key": {"type": "string", "enum": ["set",
["physical_port", "lag", "vlan", "bridge_domain", "sub_port"]],
"default": "physical_port"}, "min": 1, "max": 128}},
"number": {
"type": {"key": {"type": "integer", "minInteger": 0, "maxInteger": 4294967295, "default": 1000}, "min": 0, "max": 1}},
"map": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}},
"indexes": [["name"], ["name", "num_id_1"]],
"isRoot": true},
"Table_B": {
"columns": {
"name": {"type": "string"},
"enable": {
"type": {"key": {"type": "boolean", "default": true}, "min": 0, "max": 1}}},
"indexes": [["name"]],
"isRoot": false},
"Table_Global": {
"columns": {
"name": {"type": "string"},
"set": {
"type": {"key": {"type": "string"}, "min": 1, "max": 128}},
"map": {
"type": {"key": "string", "value": "string",
"min": 0, "max": "unlimited"}}},
"maxRows": 1,
"isRoot": true}
},
"version": "1.0.0"
}
2. The code automatic generation method is executed: checking the legality of the Schema file, performing Json analysis on the content of the Schema file, generating a Go language code of a database by combining a template code file, and formatting the Go language code:
go run odbgen.go -f ovsdb.ovsschema
3. the generated Go code is checked under the project lib/odbapi/ovsdb catalog.
An example of service development using the generated Go code in the project is as follows:
initializing database client connection:
ovsdb.InitOvsdb(addrStr)
creating a table item:
tableA := ovsdb.TableTableA{
Name: “A1”,
NumID1: “8”,
Enable: true,
Number:11111,
}
tableAUUID, err := ovsdb.TableAAdd(tableA)
setting a table entry field:
ovsdb.TableASetField(indexA, ovsdb.TableAFieldSet, “A100”)
ovsdb.TableASetField(indexA,ovsdb.TableAFieldNumber, 33333)
acquiring a table entry:
indexA := ovsdb.TableAIndex{
Name: “A1”,
}
tableA, err := ovsdb.TableAGetByIndex(indexA)
traversing the database:
func printA(tableA TableA){
fmt.printf(“A: %v\n”, tableA)
}
ovsdb.TableAIterator(printA)
deleting the emptying data:
ovsdb.TableADelByIndex(indexA)
ovsdb.TableAClear()
the invention discloses an OVSDB client code automatic generation system, which comprises:
a validity checking module: the method comprises the steps that the method is used for obtaining a Schema file to be analyzed and carrying out validity check on the Schema file, wherein the Schema file defines attributes comprising attributes of a database and attributes of each item in the database;
an analysis module: defining an analyzed data format according to RFC7047 specifications, and analyzing the contents in a legal Schema file into the data format to obtain analyzed data;
a code generation module: generating a database Go language code based on the analyzed data and a template code file, wherein the template code file comprises a pre-generated code template, and the method comprises the following steps:
the directory creating module is used for creating a storage path directory for generating codes, and clearing the old files in the directory if the directory exists;
a common code generation module: traversing the analyzed data, and generating a public code by combining a template code file according to the database attribute;
the table entry code generation module: traversing the analyzed data, generating an API code of each table entry by combining the template code file according to the attribute of each table entry, and storing the API code of each table entry as a go source file independent of the table entry;
a formatting module: and formatting the public codes and the API codes of each table entry to obtain the final Go language code of the database.
The legality checking module is used for carrying out legality checking on the Schema file and comprises the following steps: whether the Schema file is normally opened and read, whether the Schema file content conforms to Json format syntax, whether related keywords in the Schema file content conform to RFC7047 specifications and whether the Schema file can be created into a temporary database file by using an ovsdb-tool. If the Schema file passes the validity check, the validity check module deletes the temporary database file; and if the Schema file fails to pass the validity check, the validity check module returns specific error information and guides a developer to position and modify the Schema file.
The analysis module defines the analyzed data format according to the RFC7047 specification, and calls a Go standard library json deserialization function to analyze the content in the Schema file to the data format.
The formatting module performs operations comprising: the gofmt tool formats the codes, including line feed, space and bracket matching; and formatting the code by using a goimport tool, and importing the dependency package.
The invention discloses an OVSDB client code automatic generation device, which comprises a processor, a memory and a computer program which is stored on the memory and can run on the processor, wherein the processor realizes the OVSDB client code automatic generation method when executing the program.
The invention discloses a computer readable storage medium, which stores a computer program, and the program realizes the OVSDB client code automatic generation method when being executed by a processor.
The above description is only of the preferred embodiments of the present invention, and it should be noted that: it will be apparent to those skilled in the art that various modifications and adaptations can be made without departing from the principles of the invention and these are intended to be within the scope of the invention.