-
Notifications
You must be signed in to change notification settings - Fork 2
/
01_m2m_openapi.py
119 lines (97 loc) · 3.89 KB
/
01_m2m_openapi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import rdflib
import yaml
import json
import fileinput
from rdflib import OWL, RDFS, Namespace
from urllib.parse import urlparse
from collections import defaultdict
IN = 'sample.ttl'
WOTDL = Namespace('http://vsr.informatik.tu-chemnitz.de/projects/2019/growth/wotdl#')
instance = rdflib.Graph()
instance.parse(IN, format='n3')
find_http_requests = """SELECT ?d ?device ?http_request ?name ?method ?url ?body
WHERE {
?d a ?device_subclass.
?device_subclass a owl:Class.
?device_subclass rdfs:subClassOf wotdl:Device.
OPTIONAL{ ?d wotdl:name ?device }
?http_request a wotdl:HttpRequest .
OPTIONAL{?http_request wotdl:name ?name}
?http_request wotdl:httpMethod ?method .
?http_request wotdl:url ?url .
OPTIONAL{?http_request wotdl:httpBody ?body}
{
?d wotdl:hasTransition ?t.
?t wotdl:hasActuation ?http_request.
}
UNION
{
?d wotdl:hasMeasurement ?http_request.
}
}
"""
# TODOs Mahda:
# Sensor1 missing name
# BaseURI missing
# URLs are inconsistent (10.0.1.113 and 10.1.13.14)
# Mime types (and maybe Message formats) are missing in Ontology
paths = defaultdict(dict)
http_requests = instance.query(find_http_requests, initNs={'wotdl': WOTDL, 'rdfs': RDFS, 'owl': OWL})
resources = defaultdict(list)
for device, devicename, http_request, name, method, url, body in http_requests:
print('%s %s %s %s %s %s' % (device, http_request, name, method, url, body))
url = urlparse(url)
resources[url.path].append(
{'method': method.lower(), 'device': devicename, 'name': name, 'query_params': url.query, 'body': body})
for resource in resources:
path_params = [p for p in resource.split('/')[1:] if p[0] == '{' and p[-1] == '}']
requests = resources[resource]
paths[str(resource)] = {}
responses = {}
for request in requests:
entry = {
'operationId': str(request['name']),
'summary': str(request['name']) + ' request on device ' + str(request['device'])
}
parameters = []
add_parameters = False
if request['query_params'] != '':
query_params = [q.split('=')[0] for q in request['query_params'].split('&')]
parameters += [{'in': 'query', 'name': p, 'schema': {'type': 'string'}} for p in query_params]
add_parameters = True
if len(path_params) > 0:
parameters += [{'in': 'path', 'name': p[1:-1], 'required': True, 'schema': {'type': 'string'}} for p in path_params]
add_parameters = True
if add_parameters:
entry['parameters'] = parameters
if request['body'] != None:
entry['requestBody'] = yaml.load(str(request['body']))
if request['method'] == 'get':
responses['200'] = {'description': 'OK'}
elif request['method'] == 'post':
responses['201'] = {'description': 'Created'}
entry['responses'] = responses
paths[str(resource)][str(request['method'])] = entry
port = 9000
open_api = {
'openapi': '3.0.0',
'info': {
'version': '0.0.1',
'title': 'TODO',
},
'servers': [{'url': 'https://10.0.1.200:' + str(port) + '/api'}],
'paths': dict(paths)
}
with open('api.yaml', 'w') as yamlfile:
yaml.dump(open_api, stream=yamlfile)
api_name = 'wot_api'
with open('config.json', 'w') as configfile:
json.dump({'packageName': api_name, 'defaultController': api_name + '_controller', 'serverPort': port}, fp=configfile)
with fileinput.FileInput('02_m2t_openapi_flask.sh', inplace=True) as m2tfile:
for line in m2tfile:
if line[:5] == 'PORT=':
print('PORT=' + str(port))
elif line[:7] == 'MODULE=':
print('MODULE=' + api_name)
else:
print(line, end='')