-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathjsonization.py
More file actions
340 lines (302 loc) · 14.5 KB
/
jsonization.py
File metadata and controls
340 lines (302 loc) · 14.5 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
import logging
from typing import Callable, Dict, Optional, Set, Type
from basyx.aas import model
from basyx.aas.adapter._generic import ASSET_KIND, ASSET_KIND_INVERSE, JSON_AAS_TOP_LEVEL_KEYS_TO_TYPES, PathOrIO
from basyx.aas.adapter.json import AASToJsonEncoder
from basyx.aas.adapter.json.json_deserialization import AASFromJsonDecoder, _get_ts, read_aas_json_file_into
import app.model as server_model
logger = logging.getLogger(__name__)
JSON_SERVER_AAS_TOP_LEVEL_KEYS_TO_TYPES = JSON_AAS_TOP_LEVEL_KEYS_TO_TYPES + (
("assetAdministrationShellDescriptors", server_model.AssetAdministrationShellDescriptor),
("submodelDescriptors", server_model.SubmodelDescriptor),
)
class ServerAASFromJsonDecoder(AASFromJsonDecoder):
@classmethod
def _get_aas_class_parsers(cls) -> Dict[str, Callable[[Dict[str, object]], object]]:
aas_class_parsers = super()._get_aas_class_parsers()
aas_class_parsers.update(
{
"AssetAdministrationShellDescriptor": cls._construct_asset_administration_shell_descriptor,
"SubmodelDescriptor": cls._construct_submodel_descriptor,
"AssetLink": cls._construct_asset_link,
"ProtocolInformation": cls._construct_protocol_information,
"Endpoint": cls._construct_endpoint,
}
)
return aas_class_parsers
# ##################################################################################################
# Utility Methods used in constructor methods to add general attributes (from abstract base classes)
# ##################################################################################################
@classmethod
def _amend_abstract_attributes(cls, obj: object, dct: Dict[str, object]) -> None:
super()._amend_abstract_attributes(obj, dct)
if isinstance(obj, server_model.Descriptor):
if "description" in dct:
obj.description = cls._construct_lang_string_set(
_get_ts(dct, "description", list), model.MultiLanguageTextType
)
if "displayName" in dct:
obj.display_name = cls._construct_lang_string_set(
_get_ts(dct, "displayName", list), model.MultiLanguageNameType
)
if "extensions" in dct:
for extension in _get_ts(dct, "extensions", list):
obj.extension.add(cls._construct_extension(extension))
@classmethod
def _construct_asset_administration_shell_descriptor(
cls, dct: Dict[str, object], object_class=server_model.AssetAdministrationShellDescriptor
) -> server_model.AssetAdministrationShellDescriptor:
ret = object_class(id_=_get_ts(dct, "id", str))
cls._amend_abstract_attributes(ret, dct)
if "administration" in dct:
ret.administration = cls._construct_administrative_information(_get_ts(dct, "administration", dict))
if "assetKind" in dct:
ret.asset_kind = ASSET_KIND_INVERSE[_get_ts(dct, "assetKind", str)]
if "assetType" in dct:
ret.asset_type = _get_ts(dct, "assetType", str)
global_asset_id = None
if "globalAssetId" in dct:
ret.global_asset_id = _get_ts(dct, "globalAssetId", str)
specific_asset_id = set()
if "specificAssetIds" in dct:
for desc_data in _get_ts(dct, "specificAssetIds", list):
specific_asset_id.add(cls._construct_specific_asset_id(desc_data, model.SpecificAssetId))
if "endpoints" in dct:
for endpoint_dct in _get_ts(dct, "endpoints", list):
if "protocolInformation" in endpoint_dct:
ret.endpoints.append(cls._construct_endpoint(endpoint_dct, server_model.Endpoint))
elif "href" in endpoint_dct:
protocol_info = server_model.ProtocolInformation(
href=_get_ts(endpoint_dct["href"], "href", str),
endpoint_protocol=(
_get_ts(endpoint_dct["href"], "endpointProtocol", str)
if "endpointProtocol" in endpoint_dct["href"]
else None
),
endpoint_protocol_version=(
_get_ts(endpoint_dct["href"], "endpointProtocolVersion", list)
if "endpointProtocolVersion" in endpoint_dct["href"]
else None
),
)
ret.endpoints.append(
server_model.Endpoint(
protocol_information=protocol_info, interface=_get_ts(endpoint_dct, "interface", str)
)
)
if "idShort" in dct:
ret.id_short = _get_ts(dct, "idShort", str)
if "submodelDescriptors" in dct:
for sm_dct in _get_ts(dct, "submodelDescriptors", list):
ret.submodel_descriptors.append(
cls._construct_submodel_descriptor(sm_dct, server_model.SubmodelDescriptor)
)
return ret
@classmethod
def _construct_protocol_information(
cls, dct: Dict[str, object], object_class=server_model.ProtocolInformation
) -> server_model.ProtocolInformation:
ret = object_class(
href=_get_ts(dct, "href", str),
endpoint_protocol=_get_ts(dct, "endpointProtocol", str) if "endpointProtocol" in dct else None,
endpoint_protocol_version=(
_get_ts(dct, "endpointProtocolVersion", list) if "endpointProtocolVersion" in dct else None
),
subprotocol=_get_ts(dct, "subprotocol", str) if "subprotocol" in dct else None,
subprotocol_body=_get_ts(dct, "subprotocolBody", str) if "subprotocolBody" in dct else None,
subprotocol_body_encoding=(
_get_ts(dct, "subprotocolBodyEncoding", str) if "subprotocolBodyEncoding" in dct else None
),
)
return ret
@classmethod
def _construct_endpoint(cls, dct: Dict[str, object], object_class=server_model.Endpoint) -> server_model.Endpoint:
ret = object_class(
protocol_information=cls._construct_protocol_information(
_get_ts(dct, "protocolInformation", dict), server_model.ProtocolInformation
),
interface=_get_ts(dct, "interface", str),
)
cls._amend_abstract_attributes(ret, dct)
return ret
@classmethod
def _construct_submodel_descriptor(
cls, dct: Dict[str, object], object_class=server_model.SubmodelDescriptor
) -> server_model.SubmodelDescriptor:
ret = object_class(id_=_get_ts(dct, "id", str), endpoints=[])
cls._amend_abstract_attributes(ret, dct)
for endpoint_dct in _get_ts(dct, "endpoints", list):
if "protocolInformation" in endpoint_dct:
ret.endpoints.append(cls._construct_endpoint(endpoint_dct, server_model.Endpoint))
elif "href" in endpoint_dct:
protocol_info = server_model.ProtocolInformation(
href=_get_ts(endpoint_dct["href"], "href", str),
endpoint_protocol=(
_get_ts(endpoint_dct["href"], "endpointProtocol", str)
if "endpointProtocol" in endpoint_dct["href"]
else None
),
endpoint_protocol_version=(
_get_ts(endpoint_dct["href"], "endpointProtocolVersion", list)
if "endpointProtocolVersion" in endpoint_dct["href"]
else None
),
)
ret.endpoints.append(
server_model.Endpoint(
protocol_information=protocol_info, interface=_get_ts(endpoint_dct, "interface", str)
)
)
if "administration" in dct:
ret.administration = cls._construct_administrative_information(_get_ts(dct, "administration", dict))
if "idShort" in dct:
ret.id_short = _get_ts(dct, "idShort", str)
if "semanticId" in dct:
ret.semantic_id = cls._construct_reference(_get_ts(dct, "semanticId", dict))
if "supplementalSemanticIds" in dct:
for ref in _get_ts(dct, "supplementalSemanticIds", list):
ret.supplemental_semantic_id.append(cls._construct_reference(ref))
return ret
@classmethod
def _construct_asset_link(
cls, dct: Dict[str, object], object_class=server_model.AssetLink
) -> server_model.AssetLink:
ret = object_class(name=_get_ts(dct, "name", str), value=_get_ts(dct, "value", str))
return ret
class ServerStrictAASFromJsonDecoder(ServerAASFromJsonDecoder):
"""
A strict version of the AASFromJsonDecoder class for deserializing Asset Administration Shell data from the
official JSON format
This version has set ``failsafe = False``, which will lead to Exceptions raised for every missing attribute or wrong
object type.
"""
failsafe = False
class ServerStrippedAASFromJsonDecoder(ServerAASFromJsonDecoder):
"""
Decoder for stripped JSON objects. Used in the HTTP adapter.
"""
stripped = True
class ServerStrictStrippedAASFromJsonDecoder(ServerStrictAASFromJsonDecoder, ServerStrippedAASFromJsonDecoder):
"""
Non-failsafe decoder for stripped JSON objects.
"""
pass
def read_server_aas_json_file_into(
object_store: model.AbstractObjectStore,
file: PathOrIO,
replace_existing: bool = False,
ignore_existing: bool = False,
failsafe: bool = True,
stripped: bool = False,
decoder: Optional[Type[AASFromJsonDecoder]] = None,
) -> Set[model.Identifier]:
return read_aas_json_file_into(
object_store=object_store,
file=file,
replace_existing=replace_existing,
ignore_existing=ignore_existing,
failsafe=failsafe,
stripped=stripped,
decoder=decoder,
keys_to_types=JSON_SERVER_AAS_TOP_LEVEL_KEYS_TO_TYPES,
)
class ServerAASToJsonEncoder(AASToJsonEncoder):
@classmethod
def _get_aas_class_serializers(cls) -> Dict[Type, Callable]:
serializers = super()._get_aas_class_serializers()
serializers.update(
{
server_model.AssetAdministrationShellDescriptor: cls._asset_administration_shell_descriptor_to_json,
server_model.SubmodelDescriptor: cls._submodel_descriptor_to_json,
server_model.Endpoint: cls._endpoint_to_json,
server_model.ProtocolInformation: cls._protocol_information_to_json,
server_model.AssetLink: cls._asset_link_to_json,
}
)
return serializers
@classmethod
def _abstract_classes_to_json(cls, obj: object) -> Dict[str, object]:
data: Dict[str, object] = super()._abstract_classes_to_json(obj)
if isinstance(obj, server_model.Descriptor):
if obj.description:
data["description"] = obj.description
if obj.display_name:
data["displayName"] = obj.display_name
if obj.extension:
data["extensions"] = list(obj.extension)
return data
@classmethod
def _asset_administration_shell_descriptor_to_json(
cls, obj: server_model.AssetAdministrationShellDescriptor
) -> Dict[str, object]:
"""
serialization of an object from class AssetAdministrationShell to json
:param obj: object of class AssetAdministrationShell
:return: dict with the serialized attributes of this object
"""
data = cls._abstract_classes_to_json(obj)
data.update(cls._namespace_to_json(obj))
data["id"] = obj.id
if obj.administration:
data["administration"] = obj.administration
if obj.asset_kind:
data["assetKind"] = ASSET_KIND[obj.asset_kind]
if obj.asset_type:
data["assetType"] = obj.asset_type
if obj.global_asset_id:
data["globalAssetId"] = obj.global_asset_id
if obj.specific_asset_id:
data["specificAssetIds"] = list(obj.specific_asset_id)
if obj.endpoints:
data["endpoints"] = list(obj.endpoints)
if obj.id_short:
data["idShort"] = obj.id_short
if obj.submodel_descriptors:
data["submodelDescriptors"] = list(obj.submodel_descriptors)
return data
@classmethod
def _protocol_information_to_json(cls, obj: server_model.ProtocolInformation) -> Dict[str, object]:
data = cls._abstract_classes_to_json(obj)
data["href"] = obj.href
if obj.endpoint_protocol:
data["endpointProtocol"] = obj.endpoint_protocol
if obj.endpoint_protocol_version:
data["endpointProtocolVersion"] = obj.endpoint_protocol_version
if obj.subprotocol:
data["subprotocol"] = obj.subprotocol
if obj.subprotocol_body:
data["subprotocolBody"] = obj.subprotocol_body
if obj.subprotocol_body_encoding:
data["subprotocolBodyEncoding"] = obj.subprotocol_body_encoding
return data
@classmethod
def _endpoint_to_json(cls, obj: server_model.Endpoint) -> Dict[str, object]:
data = cls._abstract_classes_to_json(obj)
data["protocolInformation"] = cls._protocol_information_to_json(obj.protocol_information)
data["interface"] = obj.interface
return data
@classmethod
def _submodel_descriptor_to_json(cls, obj: server_model.SubmodelDescriptor) -> Dict[str, object]:
"""
serialization of an object from class Submodel to json
:param obj: object of class Submodel
:return: dict with the serialized attributes of this object
"""
data = cls._abstract_classes_to_json(obj)
data["id"] = obj.id
data["endpoints"] = [cls._endpoint_to_json(ep) for ep in obj.endpoints]
if obj.id_short:
data["idShort"] = obj.id_short
if obj.administration:
data["administration"] = obj.administration
if obj.semantic_id:
data["semanticId"] = obj.semantic_id
if obj.supplemental_semantic_id:
data["supplementalSemanticIds"] = list(obj.supplemental_semantic_id)
return data
@classmethod
def _asset_link_to_json(cls, obj: server_model.AssetLink) -> Dict[str, object]:
data = cls._abstract_classes_to_json(obj)
data["name"] = obj.name
data["value"] = obj.value
return data