inmanta-nfv-test-api 2.8.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. inmanta_nfv_test_api-2.8.0/PKG-INFO +19 -0
  2. inmanta_nfv_test_api-2.8.0/README.md +163 -0
  3. inmanta_nfv_test_api-2.8.0/pyproject.toml +37 -0
  4. inmanta_nfv_test_api-2.8.0/setup.cfg +59 -0
  5. inmanta_nfv_test_api-2.8.0/src/inmanta_nfv_test_api.egg-info/PKG-INFO +19 -0
  6. inmanta_nfv_test_api-2.8.0/src/inmanta_nfv_test_api.egg-info/SOURCES.txt +67 -0
  7. inmanta_nfv_test_api-2.8.0/src/inmanta_nfv_test_api.egg-info/dependency_links.txt +1 -0
  8. inmanta_nfv_test_api-2.8.0/src/inmanta_nfv_test_api.egg-info/entry_points.txt +5 -0
  9. inmanta_nfv_test_api-2.8.0/src/inmanta_nfv_test_api.egg-info/not-zip-safe +1 -0
  10. inmanta_nfv_test_api-2.8.0/src/inmanta_nfv_test_api.egg-info/requires.txt +12 -0
  11. inmanta_nfv_test_api-2.8.0/src/inmanta_nfv_test_api.egg-info/top_level.txt +2 -0
  12. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/__init__.py +15 -0
  13. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/config.py +126 -0
  14. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/exceptions.py +19 -0
  15. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/host.py +64 -0
  16. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/main.py +98 -0
  17. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/py.typed +0 -0
  18. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/__init__.py +152 -0
  19. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/__init__.py +15 -0
  20. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/actions.py +183 -0
  21. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/common.py +30 -0
  22. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/enodeb.py +292 -0
  23. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/gnodeb.py +287 -0
  24. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/instance.py +76 -0
  25. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/interface.py +250 -0
  26. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/namespace.py +140 -0
  27. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/proxy.py +130 -0
  28. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/route.py +314 -0
  29. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/ue_4g.py +283 -0
  30. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/controllers/ue_5g.py +276 -0
  31. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/__init__.py +15 -0
  32. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/base_model.py +68 -0
  33. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/common.py +92 -0
  34. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/enodeb.py +59 -0
  35. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/gnodeb.py +84 -0
  36. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/instance.py +69 -0
  37. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/interface.py +157 -0
  38. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/iperf.py +42 -0
  39. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/namespace.py +46 -0
  40. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/ping.py +47 -0
  41. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/route.py +108 -0
  42. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/ue_4g.py +57 -0
  43. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/data/ue_5g.py +156 -0
  44. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/__init__.py +15 -0
  45. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/actions.py +179 -0
  46. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/base_service.py +72 -0
  47. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/bond_interface.py +68 -0
  48. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/enodeb.py +268 -0
  49. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/gnodeb.py +284 -0
  50. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/instance.py +338 -0
  51. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/interface.py +377 -0
  52. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/namespace.py +155 -0
  53. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/route.py +162 -0
  54. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/ue_4g.py +259 -0
  55. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/ue_5g.py +261 -0
  56. inmanta_nfv_test_api-2.8.0/src/nfv_test_api/v2/services/vlan_interface.py +105 -0
  57. inmanta_nfv_test_api-2.8.0/src/pytest_nfv_test_api/__init__.py +157 -0
  58. inmanta_nfv_test_api-2.8.0/src/pytest_nfv_test_api/py.typed +0 -0
  59. inmanta_nfv_test_api-2.8.0/tests/test_basic.py +85 -0
  60. inmanta_nfv_test_api-2.8.0/tests/test_instance.py +475 -0
  61. inmanta_nfv_test_api-2.8.0/tests/test_interface_model.py +227 -0
  62. inmanta_nfv_test_api-2.8.0/tests/test_interfaces.py +199 -0
  63. inmanta_nfv_test_api-2.8.0/tests/test_iperf.py +61 -0
  64. inmanta_nfv_test_api-2.8.0/tests/test_namespaces.py +51 -0
  65. inmanta_nfv_test_api-2.8.0/tests/test_ping.py +206 -0
  66. inmanta_nfv_test_api-2.8.0/tests/test_routes.py +133 -0
  67. inmanta_nfv_test_api-2.8.0/tests/test_srsran.py +198 -0
  68. inmanta_nfv_test_api-2.8.0/tests/test_ueransim.py +201 -0
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.4
2
+ Name: inmanta-nfv-test-api
3
+ Version: 2.8.0
4
+ Summary: API for testing network functions.
5
+ Author: Inmanta
6
+ Author-email: code@inmanta.com
7
+ License: Apache-2.0
8
+ Requires-Dist: Flask<3
9
+ Requires-Dist: PyYAML
10
+ Requires-Dist: flask-cors
11
+ Requires-Dist: flask-restx
12
+ Requires-Dist: flask-apispec
13
+ Requires-Dist: click
14
+ Requires-Dist: pingparsing
15
+ Requires-Dist: trparse
16
+ Requires-Dist: werkzeug
17
+ Requires-Dist: apispec
18
+ Requires-Dist: requests
19
+ Requires-Dist: pydantic>=2
@@ -0,0 +1,163 @@
1
+ # NFV Test API
2
+
3
+ This package offers a server that creates network namespaces and places interface into these network namespaces. Through the API it is possible to:
4
+ - create single and double tagged network interfaces
5
+ - set interface state and mtu
6
+ - configure ipv4 and ipv6 addresses on (sub)interfaces
7
+ - send a ping from a network namespace
8
+
9
+ This API can be used to automate testing of network service deployments.
10
+
11
+ The process requires iproute2 with full json support (>=4.15) and needs to run as root.
12
+
13
+ For testing purposes the service can also be started with the simulate option. The API will then use the configuration in the config file to create API responses without actually making changes to the system. In simulation mode, only a ping to 1.1.1.1 will return a result.
14
+
15
+ The test api works with python 3.9, python 3.10 is not supported.
16
+
17
+ ## Installation
18
+
19
+ Clone the source:
20
+
21
+ ```
22
+ git clone git@code.inmanta.com:solutions/tools/nfv-test-api.git
23
+ cd nfv-test-api
24
+ ```
25
+
26
+ Create a new virtual env for this service and activate it:
27
+
28
+ ```
29
+ python3 -m venv env
30
+ source ./env/bin/activate
31
+ ```
32
+
33
+ or (if you have virtualenvwrapper)
34
+
35
+ ```
36
+ mkvirtualenv -p 3.9 env
37
+ workon env
38
+ ```
39
+
40
+ Install all dependencies and the project:
41
+
42
+ ```
43
+ make install
44
+ ```
45
+
46
+ ## Start the server
47
+
48
+ ### Either start the server directly on your host
49
+
50
+ ```
51
+ python3 -m nfv_test_api.main --config config.yaml
52
+ ```
53
+
54
+ You can see the swagger interface at this url :
55
+
56
+ ```
57
+ http://0.0.0.0:8080/api/v2/docs
58
+ ```
59
+
60
+ ### Web UI
61
+
62
+ The server can also serve a web console (a single-page app, source in `ui/`). The
63
+ container image builds it and serves it automatically at the root path, on the
64
+ same origin as the API:
65
+
66
+ ```
67
+ http://<host>:8080/
68
+ ```
69
+
70
+ Because it is served on the same origin, the UI talks to `/api/v2` directly — no
71
+ CORS or proxy configuration is needed.
72
+
73
+ To serve the UI when running the server directly on your host, build it once and
74
+ point the server at the build output, either with the `web_ui_path` config option
75
+ or the `NFV_TEST_API_WEB_UI` environment variable:
76
+
77
+ ```
78
+ cd ui && npm ci && npm run build && cd ..
79
+ NFV_TEST_API_WEB_UI=ui/dist python3 -m nfv_test_api.main --config config.yaml
80
+ ```
81
+
82
+ If neither is set (or the directory does not exist), the UI is simply not served
83
+ and the API keeps working as before. For UI development with hot-reload against a
84
+ running instance, see `ui/README.md`.
85
+
86
+ ### Instance identity and discovery
87
+
88
+ In a lab there are typically many nfv-test-api instances. Each instance has a
89
+ name, which defaults to its hostname — in docker/containerlab that is the
90
+ container/node name, so the UI shows which instance you are connected to without
91
+ any configuration. Instances also discover each other automatically: every
92
+ instance broadcasts a small UDP beacon on the management network (the interface
93
+ holding the default route, never on the interfaces under test), and collects the
94
+ beacons of its peers.
95
+
96
+ The identity is exposed on the API:
97
+
98
+ ```
99
+ # get the name of this instance and the peers it knows about
100
+ curl http://<host>:8080/api/v2/instance
101
+
102
+ # rename this instance and/or register peers explicitly (e.g. across routed networks,
103
+ # where broadcast discovery cannot reach); changes are in memory only
104
+ curl -X PATCH http://<host>:8080/api/v2/instance -H "Content-Type: application/json" \
105
+ -d '{"name": "subscriber-1", "peers": [{"name": "subscriber-2", "url": "http://10.0.0.3:8080"}]}'
106
+ ```
107
+
108
+ In the web UI the masthead shows the connected instance and lets you switch to
109
+ any known peer. Switching picks how to reach the peer automatically: the UI
110
+ connects to it directly when it is reachable from the browser, and otherwise
111
+ falls back to proxying the API calls through this instance
112
+ (`GET/POST/... /api/v2/proxy/<name>/...`). So switching keeps working even when
113
+ the browser cannot reach the peers directly — for example when this instance is
114
+ exposed through a reverse proxy such as Cloudflare and the peers only have
115
+ management-network addresses, the proxy reaches them on your behalf. The masthead
116
+ also offers a link to open the UI served by the peer itself (always a direct
117
+ connection, so only useful when the peer is reachable from the browser).
118
+
119
+ Discovery is configured with these config options, each with an environment
120
+ variable fallback (the config file takes precedence):
121
+
122
+ | Option | Env variable | Default | Description |
123
+ | ------------------- | ------------------------------ | ------------ | -------------------------------------------------------- |
124
+ | `instance_name` | `NFV_TEST_API_INSTANCE_NAME` | hostname | The name of this instance |
125
+ | `discovery_enabled` | `NFV_TEST_API_DISCOVERY` | `true` | Set to `false` to disable the UDP beacons entirely |
126
+ | `discovery_group` | `NFV_TEST_API_DISCOVERY_GROUP` | `""` | Instances only see peers in the same group |
127
+ | `discovery_port` | `NFV_TEST_API_DISCOVERY_PORT` | `41963` | UDP port the beacons are sent to |
128
+
129
+ Set `discovery_group` when more than one lab (possibly with identical node names)
130
+ shares the same L2 management network: each lab only sees its own instances.
131
+
132
+ ### Or start the server within container
133
+
134
+ We first need to build the docker image (make sure your current directory is the root of the repo, where the dockerfile is located):
135
+
136
+ ```
137
+ sudo docker build -t nfv-test-api .
138
+ ```
139
+
140
+ To run a container with the image, make sure you use the privileged flag to be able to use all the functionalities of the container:
141
+
142
+ ```
143
+ sudo docker run -d --name client --privileged nfv-test-api
144
+ ```
145
+
146
+ The server will automatically start with the container.
147
+
148
+ To get the ip address of the container :
149
+
150
+ ```
151
+ docker inspect client -f "{{ .NetworkSettings.Networks.bridge.IPAddress }}"
152
+ ```
153
+
154
+ You can also use the image with containerlab as done in the `examples/containerlab` folder.
155
+
156
+ ## Use the testing fixtures somewhere else
157
+
158
+ The package contains on the side of the server source, a few helper fixtures, in the package named `pytest_nfv_test_api`. To install all the dependencies required by this package, install the `nfv-test-api` python package with the `pytest` extra option:
159
+ ```
160
+ pip install inmanta-nfv-test-api[pytest]
161
+ ```
162
+
163
+ The fixture `nfv_test_api_endpoint` can then be used to spin up a container running the nfv-test-api. The fixture returns a string, which will be the base api endpoint, it has the form: `http://<container-ip>:8080/api/v2/`.
@@ -0,0 +1,37 @@
1
+ [build-system]
2
+ requires = ["setuptools", "wheel"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [tool.bumpversion]
6
+ current_version = "2.8.0"
7
+ tag = false
8
+ commit = false
9
+ parse = """
10
+ ^(?P<major>0|[1-9]\\d*)\\.
11
+ (?P<minor>0|[1-9]\\d*)\\.
12
+ (?P<patch>0|[1-9]\\d*)
13
+ (?:
14
+ .(?P<tag>dev0)
15
+ )?$
16
+ """
17
+ serialize = [
18
+ "{major}.{minor}.{patch}.{tag}",
19
+ "{major}.{minor}.{patch}",
20
+ ]
21
+
22
+ [tool.bumpversion.parts.tag]
23
+ values = ["dev0", ""]
24
+ optional_value = ""
25
+
26
+ [[tool.bumpversion.files]]
27
+ filename = "setup.cfg"
28
+ search = "^version = {current_version}$"
29
+ regex = true
30
+ replace = "version = {new_version}"
31
+ serialize = ["{major}.{minor}.{patch}"]
32
+
33
+ [[tool.bumpversion.files]]
34
+ filename = "setup.cfg"
35
+ serialize = ["{tag}"]
36
+ search = "tag_build = {current_version}"
37
+ replace = "tag_build = {new_version}"
@@ -0,0 +1,59 @@
1
+ [metadata]
2
+ name = inmanta-nfv-test-api
3
+ version = 2.8.0
4
+ description = API for testing network functions.
5
+ author = Inmanta
6
+ author_email = code@inmanta.com
7
+ license = Apache-2.0
8
+ copyright = 2023 Inmanta
9
+
10
+ [options]
11
+ zip_safe = False
12
+ include_package_data = True
13
+ include = misc/*
14
+ install_requires = Flask<3
15
+ PyYAML
16
+ flask-cors
17
+ flask-restx
18
+ flask-apispec
19
+ click
20
+ pingparsing
21
+ trparse
22
+ werkzeug
23
+ apispec
24
+ requests
25
+ pydantic>=2
26
+
27
+ [options.packages.find]
28
+ include = src*
29
+
30
+ [options.entry_points]
31
+ console_scripts =
32
+ nfv-test-api = nfv_test_api.main:main
33
+ pytest11 = nfv_test_api = pytest_nfv_test_api
34
+
35
+ [egg_info]
36
+ tag_build =
37
+ tag_date = 0
38
+ tag_svn_revision = 0
39
+
40
+ [flake8]
41
+ ignore = H405,H404,H302,H306,H301,H101,E252,E203,F722,W503
42
+ builtins = string,number,bool
43
+ max-line-length = 128
44
+ exclude = **/.env,venv
45
+ copyright-check = True
46
+ copyright-author = Inmanta
47
+ select = E,F,W,C,BLK,I
48
+
49
+ [isort]
50
+ multi_line_output = 3
51
+ include_trailing_comma = True
52
+ force_grid_wrap = 0
53
+ use_parentheses = True
54
+ line_length = 88
55
+
56
+ [black]
57
+ line-length = 128
58
+ target-version = 'py36', 'py37', 'py38'
59
+
@@ -0,0 +1,19 @@
1
+ Metadata-Version: 2.4
2
+ Name: inmanta-nfv-test-api
3
+ Version: 2.8.0
4
+ Summary: API for testing network functions.
5
+ Author: Inmanta
6
+ Author-email: code@inmanta.com
7
+ License: Apache-2.0
8
+ Requires-Dist: Flask<3
9
+ Requires-Dist: PyYAML
10
+ Requires-Dist: flask-cors
11
+ Requires-Dist: flask-restx
12
+ Requires-Dist: flask-apispec
13
+ Requires-Dist: click
14
+ Requires-Dist: pingparsing
15
+ Requires-Dist: trparse
16
+ Requires-Dist: werkzeug
17
+ Requires-Dist: apispec
18
+ Requires-Dist: requests
19
+ Requires-Dist: pydantic>=2
@@ -0,0 +1,67 @@
1
+ README.md
2
+ pyproject.toml
3
+ setup.cfg
4
+ src/inmanta_nfv_test_api.egg-info/PKG-INFO
5
+ src/inmanta_nfv_test_api.egg-info/SOURCES.txt
6
+ src/inmanta_nfv_test_api.egg-info/dependency_links.txt
7
+ src/inmanta_nfv_test_api.egg-info/entry_points.txt
8
+ src/inmanta_nfv_test_api.egg-info/not-zip-safe
9
+ src/inmanta_nfv_test_api.egg-info/requires.txt
10
+ src/inmanta_nfv_test_api.egg-info/top_level.txt
11
+ src/nfv_test_api/__init__.py
12
+ src/nfv_test_api/config.py
13
+ src/nfv_test_api/exceptions.py
14
+ src/nfv_test_api/host.py
15
+ src/nfv_test_api/main.py
16
+ src/nfv_test_api/py.typed
17
+ src/nfv_test_api/v2/__init__.py
18
+ src/nfv_test_api/v2/controllers/__init__.py
19
+ src/nfv_test_api/v2/controllers/actions.py
20
+ src/nfv_test_api/v2/controllers/common.py
21
+ src/nfv_test_api/v2/controllers/enodeb.py
22
+ src/nfv_test_api/v2/controllers/gnodeb.py
23
+ src/nfv_test_api/v2/controllers/instance.py
24
+ src/nfv_test_api/v2/controllers/interface.py
25
+ src/nfv_test_api/v2/controllers/namespace.py
26
+ src/nfv_test_api/v2/controllers/proxy.py
27
+ src/nfv_test_api/v2/controllers/route.py
28
+ src/nfv_test_api/v2/controllers/ue_4g.py
29
+ src/nfv_test_api/v2/controllers/ue_5g.py
30
+ src/nfv_test_api/v2/data/__init__.py
31
+ src/nfv_test_api/v2/data/base_model.py
32
+ src/nfv_test_api/v2/data/common.py
33
+ src/nfv_test_api/v2/data/enodeb.py
34
+ src/nfv_test_api/v2/data/gnodeb.py
35
+ src/nfv_test_api/v2/data/instance.py
36
+ src/nfv_test_api/v2/data/interface.py
37
+ src/nfv_test_api/v2/data/iperf.py
38
+ src/nfv_test_api/v2/data/namespace.py
39
+ src/nfv_test_api/v2/data/ping.py
40
+ src/nfv_test_api/v2/data/route.py
41
+ src/nfv_test_api/v2/data/ue_4g.py
42
+ src/nfv_test_api/v2/data/ue_5g.py
43
+ src/nfv_test_api/v2/services/__init__.py
44
+ src/nfv_test_api/v2/services/actions.py
45
+ src/nfv_test_api/v2/services/base_service.py
46
+ src/nfv_test_api/v2/services/bond_interface.py
47
+ src/nfv_test_api/v2/services/enodeb.py
48
+ src/nfv_test_api/v2/services/gnodeb.py
49
+ src/nfv_test_api/v2/services/instance.py
50
+ src/nfv_test_api/v2/services/interface.py
51
+ src/nfv_test_api/v2/services/namespace.py
52
+ src/nfv_test_api/v2/services/route.py
53
+ src/nfv_test_api/v2/services/ue_4g.py
54
+ src/nfv_test_api/v2/services/ue_5g.py
55
+ src/nfv_test_api/v2/services/vlan_interface.py
56
+ src/pytest_nfv_test_api/__init__.py
57
+ src/pytest_nfv_test_api/py.typed
58
+ tests/test_basic.py
59
+ tests/test_instance.py
60
+ tests/test_interface_model.py
61
+ tests/test_interfaces.py
62
+ tests/test_iperf.py
63
+ tests/test_namespaces.py
64
+ tests/test_ping.py
65
+ tests/test_routes.py
66
+ tests/test_srsran.py
67
+ tests/test_ueransim.py
@@ -0,0 +1,5 @@
1
+ [console_scripts]
2
+ nfv-test-api = nfv_test_api.main:main
3
+
4
+ [pytest11]
5
+ nfv_test_api = pytest_nfv_test_api
@@ -0,0 +1,12 @@
1
+ Flask<3
2
+ PyYAML
3
+ flask-cors
4
+ flask-restx
5
+ flask-apispec
6
+ click
7
+ pingparsing
8
+ trparse
9
+ werkzeug
10
+ apispec
11
+ requests
12
+ pydantic>=2
@@ -0,0 +1,2 @@
1
+ nfv_test_api
2
+ pytest_nfv_test_api
@@ -0,0 +1,15 @@
1
+ """
2
+ Copyright 2021 Inmanta
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ """
@@ -0,0 +1,126 @@
1
+ """
2
+ Copyright 2021 Inmanta
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ """
16
+
17
+ import os
18
+ import pathlib
19
+ from typing import Any, Dict, Optional
20
+
21
+ import yaml
22
+ from pydantic import BaseModel, Field
23
+
24
+
25
+ def env_flag(name: str, default: bool) -> bool:
26
+ raw = os.environ.get(name)
27
+ if raw is None:
28
+ return default
29
+ return raw.strip().lower() not in ("0", "false", "no", "off")
30
+
31
+
32
+ class Config(BaseModel):
33
+ host: str = "127.0.0.1"
34
+ port: int = 8080
35
+ # Directory with the built web UI (Vite `dist`) to serve. When unset, the
36
+ # NFV_TEST_API_WEB_UI environment variable is used as a fallback; if neither
37
+ # points at an existing directory, the UI is simply not served.
38
+ web_ui_path: Optional[str] = None
39
+ # Name of this instance, shown in the web UI and advertised to other
40
+ # instances via discovery. Defaults to the NFV_TEST_API_INSTANCE_NAME
41
+ # environment variable, or the hostname (= the container/node name in
42
+ # docker and containerlab) when unset.
43
+ instance_name: Optional[str] = Field(
44
+ default_factory=lambda: os.environ.get("NFV_TEST_API_INSTANCE_NAME")
45
+ )
46
+ # Discover other instances by broadcasting UDP beacons on the management
47
+ # network (the interface holding the default route). Set the
48
+ # NFV_TEST_API_DISCOVERY environment variable to "false" to disable.
49
+ discovery_enabled: bool = Field(
50
+ default_factory=lambda: env_flag("NFV_TEST_API_DISCOVERY", True)
51
+ )
52
+ # Instances only see peers in the same discovery group. Set this (or the
53
+ # NFV_TEST_API_DISCOVERY_GROUP environment variable) when multiple labs
54
+ # share the same L2 management network.
55
+ discovery_group: str = Field(
56
+ default_factory=lambda: os.environ.get("NFV_TEST_API_DISCOVERY_GROUP", "")
57
+ )
58
+ discovery_port: int = Field(
59
+ default_factory=lambda: int(
60
+ os.environ.get("NFV_TEST_API_DISCOVERY_PORT", "41963")
61
+ )
62
+ )
63
+ discovery_interval: float = 5.0
64
+ gnb_config_folder: str = "gnb_config/"
65
+ gnb_log_folder: str = "gnb_log/"
66
+ ue_5g_config_folder: str = "ue_5g_config/"
67
+ ue_5g_log_folder: str = "ue_5g_log/"
68
+ enb_config_folder: str = "enb_config/"
69
+ enb_log_folder: str = "enb_log/"
70
+ ue_4g_config_folder: str = "ue_4g_config/"
71
+ ue_4g_log_folder: str = "ue_4g_log/"
72
+ iperf_folder: str = "iperf/"
73
+
74
+
75
+ CONFIG = None
76
+
77
+
78
+ def get_config(
79
+ config_file: Optional[str] = None,
80
+ config_dict: Optional[Dict[str, Any]] = None,
81
+ ) -> Config:
82
+ global CONFIG
83
+ if CONFIG is not None:
84
+ return CONFIG
85
+
86
+ if config_dict is None:
87
+ default_path = "/etc/nfv-test-api.yaml"
88
+ path_config_file = config_file if config_file is not None else default_path
89
+ with open(path_config_file, "r") as stream:
90
+ config_dict = yaml.safe_load(stream)
91
+
92
+ CONFIG = Config(**config_dict)
93
+
94
+ # create gnb folders
95
+ gnb_config_folder = pathlib.Path(CONFIG.gnb_config_folder)
96
+ gnb_config_folder.mkdir(parents=True, exist_ok=True)
97
+
98
+ gnb_log_folder = pathlib.Path(CONFIG.gnb_log_folder)
99
+ gnb_log_folder.mkdir(parents=True, exist_ok=True)
100
+
101
+ # create 5g ue folders
102
+ ue_5g_config_folder = pathlib.Path(CONFIG.ue_5g_config_folder)
103
+ ue_5g_config_folder.mkdir(parents=True, exist_ok=True)
104
+
105
+ ue_5g_log_folder = pathlib.Path(CONFIG.ue_5g_log_folder)
106
+ ue_5g_log_folder.mkdir(parents=True, exist_ok=True)
107
+
108
+ # create enb folders
109
+ enb_config_folder = pathlib.Path(CONFIG.enb_config_folder)
110
+ enb_config_folder.mkdir(parents=True, exist_ok=True)
111
+
112
+ enb_log_folder = pathlib.Path(CONFIG.enb_log_folder)
113
+ enb_log_folder.mkdir(parents=True, exist_ok=True)
114
+
115
+ # create 5g ue folders
116
+ ue_4g_config_folder = pathlib.Path(CONFIG.ue_4g_config_folder)
117
+ ue_4g_config_folder.mkdir(parents=True, exist_ok=True)
118
+
119
+ ue_4g_log_folder = pathlib.Path(CONFIG.ue_4g_log_folder)
120
+ ue_4g_log_folder.mkdir(parents=True, exist_ok=True)
121
+
122
+ # create iperf folder
123
+ iperf_folder = pathlib.Path(CONFIG.iperf_folder)
124
+ iperf_folder.mkdir(parents=True, exist_ok=True)
125
+
126
+ return CONFIG
@@ -0,0 +1,19 @@
1
+ """
2
+ Copyright 2021 Inmanta
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ """
16
+
17
+
18
+ class ServerError(Exception):
19
+ pass
@@ -0,0 +1,64 @@
1
+ """
2
+ Copyright 2021 Inmanta
3
+
4
+ Licensed under the Apache License, Version 2.0 (the "License");
5
+ you may not use this file except in compliance with the License.
6
+ You may obtain a copy of the License at
7
+
8
+ http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ Unless required by applicable law or agreed to in writing, software
11
+ distributed under the License is distributed on an "AS IS" BASIS,
12
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ See the License for the specific language governing permissions and
14
+ limitations under the License.
15
+ """
16
+
17
+ import logging
18
+ import subprocess
19
+ from typing import List, Optional, Tuple
20
+
21
+ LOGGER = logging.getLogger(__name__)
22
+
23
+
24
+ class Host:
25
+ def __init__(self, shell_entry_point: List[str] = []) -> None:
26
+ self._shell_entry_point = shell_entry_point
27
+
28
+ def exec(self, command: List[str], timeout: Optional[int] = 10) -> Tuple[str, str]:
29
+ cmd = self._shell_entry_point + command
30
+ LOGGER.debug("Running command %s", cmd)
31
+ process = subprocess.Popen(
32
+ cmd,
33
+ shell=False,
34
+ universal_newlines=True,
35
+ stdout=subprocess.PIPE,
36
+ stderr=subprocess.PIPE,
37
+ )
38
+ try:
39
+ # Make sure our command terminates.
40
+ # We set a hard timeout of 10 seconds, it should work for all
41
+ # the commands we run. If it doesn't, we can create a more
42
+ # elaborate mechanism.
43
+ return process.communicate(timeout=timeout)
44
+ except subprocess.TimeoutExpired:
45
+ # Kill the process and return the output we had so far
46
+ process.terminate()
47
+ return process.communicate(timeout=1)
48
+
49
+ def hostname(self) -> str:
50
+ stdout, stderr = self.exec(["hostname"])
51
+ if stderr:
52
+ raise RuntimeError(f"Failed to run hostname command on host: {stderr}")
53
+
54
+ return stdout.strip()
55
+
56
+
57
+ class NamespaceHost(Host):
58
+ def __init__(self, namespace: str) -> None:
59
+ super().__init__(shell_entry_point=["ip", "netns", "exec", namespace])
60
+
61
+ def get_raw_namespaces(self) -> List[object]:
62
+ raise NotImplementedError(
63
+ "You shouldn't try to interact with namespaces from inside another one"
64
+ )