Skip to content

Commit

Permalink
Adding support for output contrail JSON Schema.
Browse files Browse the repository at this point in the history
Contrail JSON Schema has JSON Schema + Data Model
information + IFMap information.
This commit also add utility to convert the Contrial JSON
schema to
XMLSchema using Jinja2.

Closes-Bug: 1729708
Change-Id: Ic9ff18741c807a91a016b864f0afa1b49f48788f
  • Loading branch information
nati committed Nov 15, 2017
1 parent 94b3381 commit 00d410b
Show file tree
Hide file tree
Showing 6 changed files with 729 additions and 109 deletions.
39 changes: 39 additions & 0 deletions README_jsonschema.md
@@ -0,0 +1,39 @@
In this document, we describe how to convert JSON Schema to XML and vise versa.

# How to use utility

## Convert XMLSchema to JSON Schema

```shell
python generateDS.py -f -o $DESTINATION_DIR -g contrail-json-schema $PATH_TO_XMLSCHEMA
```

We will have types.json and $RESOURCE_NAME_schema.json In $DESTINATION_DIR.
types.json contains sub types.

## Convert JSON Schema to XMLSchema.

```shell
python json_schema_convert.py $PATH_TO_JSONSCHEMA_DIR \
json_schema_template/xmlschema.tmpl > $PATH_TO_XMLSCHEMA
```

json_schema_convert.py accepts jinja2 template, so you can also generate codes using
jinja2 template.

# Contrail JSON schema

In order to generate code and database model, we extend JSON Schema using
following additional properties.

- id resource ID
- singular singular form of the resource
- plural plural form of the resource
- api_type generated API style
- extends schema inheritance
- references references for another resource
- parents parents resources
- schema JSON Schema

# Limitation
Comments, Annotaion informations and description for enum element in the original schemas lost in JSONSchema output.
27 changes: 19 additions & 8 deletions ccmap.py
Expand Up @@ -11,6 +11,7 @@
from device_api import DeviceApiGenerator
from golang_api import GoLangApiGenerator
from json_schemagen import JsonSchemaGenerator
from contrail_json_schemagen import ContrailJsonSchemaGenerator
from copy import deepcopy


Expand All @@ -21,6 +22,7 @@ class IFMapGenerator(object):
Step 3. Generate C++ decoder
Step 4. Generate xsd corresponding to data structures.
"""

def __init__(self, parser, genCategory):
self._Parser = parser
self._idl_parser = None
Expand Down Expand Up @@ -51,8 +53,10 @@ def _BuildDataModel(self, children):
to_ident = self._IdentifierLocate(to_name)
for from_ident in self._Identifiers.values():
ann_copy = deepcopy(annotation)
ann_copy[0].name = '%s-%s' % (from_ident.getName(), to_ident.getName())
meta = self.MetadataLocate(ann_copy[0].name, None, ann_copy)
ann_copy[0].name = '%s-%s' % (from_ident.getName(),
to_ident.getName())
meta = self.MetadataLocate(
ann_copy[0].name, None, ann_copy)
meta.SetSchemaElement(element)
from_ident.addLinkInfo(meta, to_ident, attrs)
to_ident.addBackLinkInfo(meta, from_ident, attrs)
Expand Down Expand Up @@ -98,7 +102,7 @@ def _ProcessMetadata(self, element, annotation):
identifier.SetProperty(meta)
else:
(from_name, to_name, attrs) = \
self._idl_parser.GetLinkInfo(element.getName())
self._idl_parser.GetLinkInfo(element.getName())
from_ident = self._IdentifierLocate(from_name)
to_ident = self._IdentifierLocate(to_name)
from_ident.addLinkInfo(meta, to_ident, attrs)
Expand Down Expand Up @@ -128,8 +132,8 @@ def MetadataLocate(self, name, typename, annotation):
typename = None

meta = IFMapMetadata.Create(name,
self._idl_parser.IsProperty(annotation),
annotation, typename)
self._idl_parser.IsProperty(annotation),
annotation, typename)
self._Metadata[name] = meta
return meta

Expand All @@ -156,7 +160,7 @@ def _GenerateBackendClassImpl(self):
filename = self._Parser.outFilename + '_agent.cc'
clntfile = self._Parser.makeFile(filename)
classgen.GenerateAgent(clntfile, hfilename,
self._Identifiers, self._Metadata)
self._Identifiers, self._Metadata)

def _GenerateBackendParsers(self):
hfilename = self._Parser.outFilename + '_types.h'
Expand All @@ -183,7 +187,7 @@ def _GenerateJavaApi(self, xsd_root):

def _GenerateDeviceApi(self, xsd_root):
apigen = DeviceApiGenerator(self._Parser, xsd_root,
self._Identifiers, self._Metadata)
self._Identifiers, self._Metadata)
apigen.Generate(self._Parser.outFilename)

def _GenerateGoLangApi(self, xsd_root):
Expand All @@ -193,7 +197,12 @@ def _GenerateGoLangApi(self, xsd_root):

def _GenerateJsonSchema(self, xsd_root):
apigen = JsonSchemaGenerator(self._Parser, self._cTypesDict,
self._Identifiers, self._Metadata)
self._Identifiers, self._Metadata)
apigen.Generate(self._Parser.outFilename)

def _GenerateContrailJsonSchema(self, xsd_root):
apigen = ContrailJsonSchemaGenerator(self._Parser, self._cTypesDict,
self._Identifiers, self._Metadata)
apigen.Generate(self._Parser.outFilename)

def setLanguage(self, lang):
Expand All @@ -216,5 +225,7 @@ def generate(self, root, infile, outFilename):
self._GenerateDeviceApi(root)
elif self._genCategory == 'golang-api':
self._GenerateGoLangApi(root)
elif self._genCategory == 'contrail-json-schema':
self._GenerateContrailJsonSchema(root)
elif self._genCategory == 'json-schema':
self._GenerateJsonSchema(root)

0 comments on commit 00d410b

Please sign in to comment.