Skip to content

Commit

Permalink
Generate OpenAPI Python modules (closes
Browse files Browse the repository at this point in the history
#18).

Rather than including a set of pre-generated API modules, include a
build time script to automatically download and generate the modules.
  • Loading branch information
davidjwbbc authored and rjb1000 committed Sep 27, 2022
1 parent 43778b0 commit 6944bea
Show file tree
Hide file tree
Showing 29 changed files with 136 additions and 8,642 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
*.pyc
*.pyo
*.egg-info
*.dist-info
__pycache__
dist
build
src/rt_5gms_as/openapi_5g
32 changes: 32 additions & 0 deletions build_scripts/backend.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os.path
import logging
from setuptools import build_meta as _orig
import subprocess

replace = ['build_sdist']
log = logging.getLogger(__name__)

# Copy all exported variables from the original
for name in _orig.__all__:
if name not in replace:
v = getattr(_orig, name)
globals()[name] = v
__all__ = _orig.__all__

def _check_openapi():
custom_build_dir = os.path.dirname(__file__)
topdir = os.path.join(custom_build_dir,'..')
srcdir = os.path.join(topdir,'src')
rt_5gms_as_dir = os.path.join(srcdir, 'rt_5gms_as')
openapi_dir = os.path.join(rt_5gms_as_dir, 'openapi_5g')
if not os.path.isdir(openapi_dir):
log.info('Generating OpenAPI Python classes...')
result = subprocess.run([os.path.join(custom_build_dir,'generate_openapi')], stdin=None, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
log.error("Failed: %s", result.stderr.decode('utf-8'))
else:
log.info('Done generating OpenAPI Python classes')

def build_sdist(sdist_directory, config_settings=None):
_check_openapi()
return _orig.build_sdist(sdist_directory, config_settings)
49 changes: 49 additions & 0 deletions build_scripts/generate_openapi
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/sh
#
# 5G-MAG Reference Tools: 5GMS Application Server
# ===============================================
#
# File: generate_openapi
# License: 5G-MAG Public License (v1.0)
# Author: David Waring
# Copyright: (C) 2022 British Broadcasting Corporation
#
# For full license terms please see the LICENSE file distributed with this
# program. If this file is missing then the license can be retrieved from
# https://drive.google.com/file/d/1cinCiA778IErENZ3JN52VFW-1ffHpx7Z/view
#
# This will download the openapi-generator JAR, fetch the latest release 17
# OpenAPI YAML files for 5G and use them to generate python modules for parsing
# the 5GMS ContentHostingConfiguration JSON from TS 26.512 (M1 interface).
#

scriptdir=`dirname "$0"`
scriptdir=`cd "$scriptdir"; pwd`

rt_5gms_as_dir=`cd "${scriptdir}/../src/rt_5gms_as"; pwd`

find_java() {
# Use the JAVA environment variable if set else look for "java" command
if [ -n "$JAVA" ]; then
echo $JAVA
else
which java
fi
}

if [ ! -d "${rt_5gms_as_dir}/openapi_5g" ]; then
mkdir -p "${rt_5gms_as_dir}/openapi_5g"
java=`find_java`
tmpdir=`mktemp -d --tmpdir openapi-generator.XXXXXXXX`
trap "rm -rf '$tmpdir'" 0 1 2 3 4 5 6 7 8 10 11 12 13 14
(
cd "$tmpdir"
wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/6.0.1/openapi-generator-cli-6.0.1.jar -O openapi-generator-cli.jar
git clone -b REL-17 https://forge.3gpp.org/rep/all/5G_APIs.git
mkdir 5g-api-python
"$java" -jar openapi-generator-cli.jar generate -i 5G_APIs/TS26512_M1_ContentHostingProvisioning.yaml -g python --additional-properties packageName=rt_5gms_as.openapi_5g,projectName=openapi-5g -o 5g-api-python
cp -rv 5g-api-python/rt_5gms_as/openapi_5g "$rt_5gms_as_dir/"
)
fi

exit 0
45 changes: 31 additions & 14 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ Files in this repository:
- README.md - Project README file.
- LICENSE - The software license for this project.
- pyproject.toml - The Python project description for building and installing the application.
- build_scripts/ - Scripts used when building the python project.
- backend.py - build backend wrapper to trigger extra build actions.
- generate_openapi - Will generate the OpenAPI python modules if not already present.
- docs/ - Development documentation and examples.
- README.md - This document.
- chc.json - An example ContentHostingConfiguration JSON file which provisions a pull-ingest AS for "Big Buck Bunny".
Expand All @@ -22,10 +25,17 @@ Files in this repository:

Running the example without building
------------------------------------
Make sure that nginx is installed on the local system and is found on the
current command path (`$PATH`).
Make sure that git, java, wget and nginx are installed on the local system and
can be found on the current command path (`$PATH`).

Then:
Generate the OpenAPI python modules (these are not part of the source
distribution):
```
cd rt-5gms-application-server
build_scripts/generate_openapi
```

Run the example directly:
```
cd rt-5gms-application-server/src
python3 -m rt_5gms_as.app ../docs/rt-common-shared/5gms/examples/ContentHostingConfiguration_Big-Buck-Bunny_pull-ingest.json
Expand All @@ -35,21 +45,28 @@ This will start nginx with a configuration which will provide a reverse proxy to

Regenerating the 5G API bindings
--------------------------------
Download the openapi-generator, e.g.
```
wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/6.0.1/openapi-generator-cli-6.0.1.jar -O openapi-generator-cli.jar
```
The `build_scripts/generate_openapi` script will use wget, git and java to
download the openapi-generator tool, the 5G OpenAPI YAML and generate the `rt_5gms_as.openapi_5g` Python module package. The script will only do this if the
`src/rt_5gms_as/openapi_5g` directory does not already exist.

Get a copy of the 5G APIs:
Therefore to regenerate the API bindings you first need to remove the old bindings:
```
git clone -b REL-17 https://forge.3gpp.org/rep/all/5G_APIs.git
cd rt-5gms-application-server
rm -rf src/rt_5gms_as/openapi_5g
```

Generate the bindings:
Then run the generator script:
```
mkdir 5g-api-python
java -jar openapi-generator-cli.jar generate -i 5G_APIs/TS26512_M1_ContentHostingProvisioning.yaml -g python --additional-properties packageName=rt_5gms_as.openapi_5g,projectName=openapi-5g -o 5g-api-python
rm -rf rt-5gms-application-server/src/rt_5gms_as/openapi_5g
mv 5g-api-python/rt_5gms_as/openapi_5g rt-5gms-application-server/src/rt_5gms_as/
rt-5gms-application-server/build_scripts/generate_openapi
```

For reference (or if it is desirable to recreate the steps manually) the `generate_openapi` script performs the following actions:
- Uses `wget` to fetch version 6.0.1 of the [openapi-generator-cli](https://github.com/OpenAPITools/openapi-generator-cli).
- e.g. `wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/6.0.1/openapi-generator-cli-6.0.1.jar -O openapi-generator-cli.jar`
- Uses `git` to clone the [5G OpenAPI repository](https://forge.3gpp.org/rep/all/5G_APIs.git).
- e.g. `git clone -b REL-17 https://forge.3gpp.org/rep/all/5G_APIs.git`
- Uses the openapi-generator-cli, downloaded in the first step, to generate the API bindings.
- e.g. `mkdir 5g-api-python; java -jar openapi-generator-cli.jar generate -i 5G_APIs/TS26512_M1_ContentHostingProvisioning.yaml -g python --additional-properties packageName=rt_5gms_as.openapi_5g,projectName=openapi-5g -o 5g-api-python`
- Copies the API Python package to the `src/rt_5gms_as/openapi_5g` directory.
- e.g. `cp -r 5g-api-python/rt_5gms_as/openapi_5g rt-5gms-application-server/src/rt_5gms_as/`

23 changes: 19 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
[build-system]
requires = ["setuptools"]
build-backend = "setuptools.build_meta"
build-backend = "backend"
backend-path = ["build_scripts"]

[project]
name = "rt-5gms-application-server"
version = "0.0.1"
dependencies = [
'urllib3 >= 1.25.3',
'python-dateutil',
'importlib-metadata; python_version >= 3.7',
'python-dateutil'
]
scripts = { 5gms-application-server = "rt_5gms_as.app" }
requires-python = ">=3.7"
scripts = { 5gms-application-server = "rt_5gms_as.app:main" }
authors = [
{ name = 'David Waring', email = '[email protected]' }
]
license = { file = "LICENSE" }
readme = "README.md"

[project.urls]
"Homepage" = "https://5g-mag.com/"
"Source" = "https://github.com/5g-mag/rt-5gms-application-server"

[tool.setuptools.packages.find]
where = ["src"]

[tool.setuptools.package-data]
"rt_5gms_as.proxies" = ["*.tmpl"]

[tool.setuptools.data-files]
'docs' = ['docs/*']
27 changes: 0 additions & 27 deletions src/rt_5gms_as/openapi_5g/__init__.py

This file was deleted.

3 changes: 0 additions & 3 deletions src/rt_5gms_as/openapi_5g/api/__init__.py

This file was deleted.

Loading

0 comments on commit 6944bea

Please sign in to comment.