-
Notifications
You must be signed in to change notification settings - Fork 0
/
candil-openapi-schemas-generator.py
1347 lines (1207 loc) · 79.5 KB
/
candil-openapi-schemas-generator.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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'''
pyang plugin -- CANDIL OpenAPI Schemas Generator.
Given one or several YANG modules, it dynamically generates the relative OpenAPI Schemas according to the OpenAPI specification for NGSI-LD API V1.6.1.
Version: 1.0.8.
Author: Networking and Virtualization Research Group (GIROS DIT-UPM) -- https://dit.upm.es/~giros
'''
import optparse
import pdb
import re
import sys
import json
import yaml
from pyang import plugin
from pyang import statements
### PLUGIN CONSTANTS ###
OPENAPI_URL = "https://raw.githubusercontent.com/giros-dit/python-ngsi-ld-client/1.6.1/schemas/ngsi-ld-api.yaml" # -> Consolidated OpenAPI spec. for NGSI-LD API v1.6.1
#OPENAPI_URL = "https://forge.etsi.org/rep/cim/NGSI-LD/-/raw/1.6.1/ngsi-ld-api.yaml" # -> Official OpenAPI spec. for NGSI-LD API v1.6.1
ENTITY_TYPE_LIST = [] # -> It includes all the different types of entities generated throughout all the YANG modules that are processed
### --- ###
def pyang_plugin_init():
plugin.register_plugin(CandilXmlParserGeneratorPlugin())
class CandilXmlParserGeneratorPlugin(plugin.PyangPlugin):
def __init__(self):
plugin.PyangPlugin.__init__(self, 'candil-openapi-schemas-generator')
def add_output_format(self, fmts):
self.multiple_modules = True
fmts['candil-openapi-schemas-generator'] = self
def add_opts(self, optparser):
optlist = [
optparse.make_option('--candil-openapi-schemas-generator-help', dest='candil_openapi_schemas_generator_help', action='store_true', help='Prints help and usage.'),
]
g = optparser.add_option_group('CANDIL OpenAPI Schemas Generator specific options')
g.add_options(optlist)
def setup_ctx(self, ctx):
if ctx.opts.candil_openapi_schemas_generator_help:
print_help()
sys.exit(0)
def setup_fmt(self, ctx):
ctx.implicit_errors = False
def emit(self, ctx, modules, fd):
generate_python_openapi_schemas_generator_code(ctx, modules, fd)
def print_help():
'''
Prints execution help.
'''
print('''
Pyang plugin - CANDIL OpenAPI Schemas Generator (candil-openapi-schemas-generator).
Given one or several YANG modules, this plugin generates the OpenAPI schemas.
Usage:
pyang -f candil-openapi-schemas-generator [OPTIONS] <base_module.yang> [augmenting_module_1.yang] [augmenting_module_2.yang] ... [augmenting_module_N.yang] [> <output_file.yaml>]
''')
def generate_python_openapi_schemas_generator_code(ctx, modules, fd):
'''
Processes YANG modules and generates the corresponding OpenAPI schemas generator code for data modeled by these YANG modules.
'''
# Use PDB to debug the code with pdb.set_trace().
# pdb.set_trace()
### FUNCTION CONSTANTS ###
YANG_PRIMITIVE_TYPES = [
"int8", "int16", "int32", "int64",
"uint8", "uint16", "uint32", "uint64",
"decimal64", "string", "boolean", "enumeration",
"bits", "binary", "empty", "union"
]
# NOTE: OpenAPI Schemas types.
BASE_YANG_TYPES_TO_OPENAPI_SCHEMAS_TYPES = {
'int8': 'integer',
'int16': 'integer',
'int32': 'integer',
'int64': 'integer',
'uint8': 'integer',
'uint16': 'integer',
'uint32': 'integer',
'uint64': 'integer',
'decimal64': 'number',
'string': 'string',
'boolean': 'boolean',
'enumeration': 'enum',
'bits': 'array',
'binary': 'string',
'empty': 'string',
'union': 'string',
'leafref': 'string'
}
# NOTE: OpenAPI Schemas formats.
BASE_YANG_TYPES_TO_OPENAPI_SCHEMAS_FORMATS = {
'int32': 'int32',
'int64': 'int64',
'bits': 'bits',
'binary': 'binary'
}
INDENTATION_BLOCK = ' '
### --- ###
### AUXILIARY FUNCTIONS ###
def to_camelcase(element_keyword: str, element_arg: str) -> str:
'''
Auxiliary function.
Returns the CamelCase representation of element_arg according to the YANG to NGSI-LD translation conventions.
'''
if (element_keyword is None) or (element_arg is None):
return element_arg
else:
if (element_keyword == 'module'):
return element_arg
if (element_keyword == 'container') or (element_keyword == 'list'):
return re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), element_arg.capitalize())
if (element_keyword == 'leaf') or (element_keyword == 'leaf-list') or (element_keyword == 'choice') or (element_keyword == 'case'):
return re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), element_arg)
def typedefs_discovering(modules) -> dict:
'''
Auxiliary function.
Given a set of YANG modules, finds all typedefs defined in them and returns a Python
dictionary with their conversions to primitive YANG types.
'''
defined_typedefs_dict = {}
primitive_typedefs_dict = {}
for module in modules: # First iteration retrieves the defined type.
typedefs = module.search('typedef')
if typedefs is not None:
for typedef in typedefs:
if typedef is not None:
typedef_name = str(typedef.arg)
typedef_type = str(typedef.search_one('type').arg).split(':')[-1]
defined_typedefs_dict[typedef_name] = typedef_type
for module in modules: # Second iteration retrieves the primitive type.
typedefs = module.search('typedef')
if typedefs is not None:
for typedef in typedefs:
if typedef is not None:
typedef_name = str(typedef.arg)
typedef_type = str(typedef.search_one('type').arg).split(':')[-1]
if (typedef_type not in YANG_PRIMITIVE_TYPES) and ('-ref' not in typedef_type ) and (typedef_type != 'leafref'):
primitive_typedefs_dict[typedef_name] = defined_typedefs_dict[typedef_type]
else:
primitive_typedefs_dict[typedef_name] = typedef_type
return primitive_typedefs_dict
def typedefs_pattern_discovering(modules) -> dict:
'''
Auxiliary function.
Given a set of YANG modules, finds the patterns of all typedefs defined in them and returns a Python
dictionary with their conversions to primitive YANG patterns.
'''
defined_typedefs_dict = {}
patterns_defined_typedefs_dict = {}
primitive_typedefs_dict = {}
pattern_primitive_typedefs_dict = {}
for module in modules: # First iteration retrieves the defined pattern.
typedefs = module.search('typedef')
if typedefs is not None:
for typedef in typedefs:
if typedef is not None:
typedef_name = str(typedef.arg)
typedef_type = str(typedef.search_one('type').arg).split(':')[-1]
defined_typedefs_dict[typedef_name] = typedef_type
typedef_pattern = typedef.search_one('type').search_one('pattern')
if typedef_pattern != None:
typedef_pattern = str(typedef_pattern.arg)
patterns_defined_typedefs_dict[typedef_name] = typedef_pattern
for module in modules: # Second iteration retrieves the primitive pattern.
typedefs = module.search('typedef')
if typedefs is not None:
for typedef in typedefs:
if typedef is not None:
typedef_name = str(typedef.arg)
typedef_type = str(typedef.search_one('type').arg).split(':')[-1]
typedef_pattern = typedef.search_one('type').search_one('pattern')
if (typedef_type not in YANG_PRIMITIVE_TYPES) and ('-ref' not in typedef_type) and (typedef_type != 'leafref'):
primitive_typedefs_dict[typedef_name] = defined_typedefs_dict[typedef_type]
if typedef_pattern != None:
typedef_pattern = str(typedef_pattern.arg)
pattern_primitive_typedefs_dict[typedef_name] = patterns_defined_typedefs_dict[typedef_name]
else:
primitive_typedefs_dict[typedef_name] = typedef_type
if typedef_pattern != None:
typedef_pattern = str(typedef_pattern.arg)
pattern_primitive_typedefs_dict[typedef_name] = typedef_pattern
return pattern_primitive_typedefs_dict
def yang_to_openapi_schemas_types_conversion(element_type: str, typedefs_dict: dict) -> str:
'''
Auxiliary function.
Returns the OpenAPI schemas type given the YANG type of an element/node in a YANG module.
'''
if (element_type == 'identityref'):
return 'string'
else:
base_yang_type = ''
if (typedefs_dict.get(element_type) is not None):
base_yang_type = typedefs_dict[element_type]
else:
base_yang_type = element_type
return BASE_YANG_TYPES_TO_OPENAPI_SCHEMAS_TYPES[base_yang_type]
def yang_to_openapi_schemas_formats_conversion(element_type: str) -> str:
'''
Auxiliary function.
Returns the OpenAPI schemas format given the YANG type of an element/node in a YANG module.
'''
if BASE_YANG_TYPES_TO_OPENAPI_SCHEMAS_FORMATS.get(element_type) is not None:
return str(BASE_YANG_TYPES_TO_OPENAPI_SCHEMAS_FORMATS.get(element_type))
else:
return None
def is_enclosing_container(element) -> bool:
'''
Auxiliary function.
Checks if an element is an "enclosing container":
- It is a container AND
- It has one child or more AND
- Each of one of them is either a container or a list.
'''
result = False
individual_results = 0
if (element.keyword != 'container'):
return False
else:
if (len(element.i_children) >= 1):
for subelement in element.i_children:
if (subelement.keyword in ['container', 'list']):
individual_results += 1
if (len(element.i_children) == individual_results):
result = True
return result
def is_deprecated(element) -> bool:
'''
Auxiliary function.
Checks if an element is deprecated.
'''
result = False
status = element.search_one('status')
if (status is not None) and (status.arg == 'deprecated'):
result = True
return result
def is_entity(element) -> bool:
'''
Auxiliary function.
Checks if an element matches the YANG to NGSI-LD translation convention for an Entity.
'''
result = False
if (element.keyword in ['container', 'list']):
result = True
return result
def is_choice(element) -> bool:
'''
Auxiliary function.
Checks if an element is a YANG choice.
'''
result = False
if (str(element.keyword) == 'choice'):
result = True
return result
def is_property(element, typedefs_dict: dict) -> bool:
'''
Auxiliary function.
Checks if an element matches the YANG to NGSI-LD translation convention for a Property.
'''
result = False
if (element.keyword in ['leaf-list', 'leaf']):
element_type = str(element.search_one('type')).replace('type ', '').split(':')[-1]
if (element_type in YANG_PRIMITIVE_TYPES) or \
((typedefs_dict.get(element_type) is not None) and (('-ref' not in typedefs_dict.get(element_type)) and (typedefs_dict.get(element_type) != 'leafref'))):
result = True
return result
def is_relationship(element, typedefs_dict: dict) -> bool:
'''
Auxiliary function.
Checks if an element matches the YANG to NGSI-LD translation convention for a Relationship.
'''
result = False
if (element.keyword in ['leaf-list', 'leaf']):
element_type = str(element.search_one('type')).replace('type ', '').split(':')[-1]
if (element_type == 'leafref') or \
((typedefs_dict.get(element_type) is not None) and (('-ref' in typedefs_dict.get(element_type) or (typedefs_dict.get(element_type) == 'leafref')))):
result = True
return result
def is_datetime(element) -> bool:
'''
Auxiliary function.
Checks if an element typedef matches to date-and-time.
'''
result = False
if (element.keyword in ['leaf-list', 'leaf']):
element_type = str(element.search_one('type')).replace('type ', '').split(':')[-1]
if str(element_type) == "date-and-time":
result = True
return result
def has_pattern(element, typedefs_pattern_dict: dict) -> bool:
'''
Auxiliary function.
Checks if an element typedef has a pattern.
'''
result = False
if (element.keyword in ['leaf-list', 'leaf']):
element_type = str(element.search_one('type')).replace('type ', '').split(':')[-1]
if typedefs_pattern_dict.get(element_type) is not None:
result = True
return result
def get_yang_module_data_nodes(element, yang_data_nodes_list: list) -> list:
'''
Auxiliary recursive function.
Recursively gets all YANG data nodes.
'''
if element.keyword in ['container', 'list', 'choice']:
subelements = element.i_children
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords) and (is_deprecated(subelement) == False):
if str(subelement.keyword) == 'choice':
yang_data_nodes_list.append(subelement.arg)
cases = subelement.i_children
if (cases is not None):
for case in cases:
if (case is not None) and (case.keyword in statements.data_definition_keywords) and (is_deprecated(case) == False):
case_subelements = case.i_children
if (case_subelements is not None):
for case_subelement in case_subelements:
if (case_subelement is not None) and (case_subelement.keyword in statements.data_definition_keywords):
yang_data_nodes_list.append(case_subelement.arg)
get_yang_module_data_nodes(case_subelement, yang_data_nodes_list)
elif str(subelement.keyword) in ['container', 'list']:
yang_data_nodes_list.append(subelement.arg)
get_yang_module_data_nodes(subelement, yang_data_nodes_list)
else:
yang_data_nodes_list.append(subelement.arg)
elif element.keyword in ['leaf-list', 'leaf']:
yang_data_nodes_list.append(element.arg)
return yang_data_nodes_list
def is_yang_identity(element, typedefs_dict: dict) -> bool:
'''
Auxiliary function.
Checks if an element matches the YANG to NGSI-LD translation convention for a YANG Identity.
NOTE: YANG Identities are NGSI-LD Entities, but since they are either leaf-lists or leaves, they
have no children, and therefore they are processed differently.
'''
result = False
if (element.keyword in ['leaf-list', 'leaf']):
element_type = str(element.search_one('type')).replace('type ', '').split(':')[-1]
if (element_type == 'identityref') or \
((typedefs_dict.get(element_type) is not None) and (typedefs_dict.get(element_type) == 'identityref')):
result = True
return result
def generate_schemas(element, parent_element_arg, entity_path: str, camelcase_entity_path: str, depth_level: int, typedefs_dict: dict, typedefs_pattern_dict: dict, yang_data_nodes_list: list):
'''
Auxiliary function.
Recursively generates the JSON parser code.
'''
global ENTITY_TYPE_LIST
camelcase_element_arg = to_camelcase(str(element.keyword), str(element.arg))
element_namespace = str(element.i_module.search_one('namespace').arg)
yang_module_name = str(element.i_module.arg)
current_path = ''
if (entity_path is None):
current_path = str(element.arg) + '_'
else:
current_path = entity_path + str(element.arg) + '_'
### ENCLOSING CONTAINER IDENTIFICATION ###
if (is_enclosing_container(element) == True) and (is_deprecated(element) == False):
subelements = element.i_children
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords):
if parent_element_arg is None:
generate_schemas(subelement, None, None, None, depth_level, typedefs_dict, typedefs_pattern_dict, yang_data_nodes_list)
else:
current_camelcase_path = ''
if (camelcase_entity_path is None):
current_camelcase_path = to_camelcase(str(element.keyword), str(subelement.arg))
else:
current_camelcase_path = camelcase_entity_path + to_camelcase(str(element.keyword), str(element.arg))
if subelement.keyword == 'container':
generate_schemas(subelement, element.arg, current_path, current_camelcase_path, depth_level, typedefs_dict, typedefs_pattern_dict, yang_data_nodes_list)
elif subelement.keyword == 'list':
generate_schemas(subelement, parent_element_arg, current_path, current_camelcase_path, depth_level, typedefs_dict, typedefs_pattern_dict, yang_data_nodes_list)
### NGSI-LD ENTITY IDENTIFICATION ###
elif (is_entity(element) == True) and (is_deprecated(element) == False):
current_camelcase_path = ''
if (camelcase_entity_path is None):
current_camelcase_path = to_camelcase(str(element.keyword), str(element.arg))
else:
current_camelcase_path = camelcase_entity_path + to_camelcase(str(element.keyword), str(element.arg))
if current_camelcase_path not in ENTITY_TYPE_LIST:
ENTITY_TYPE_LIST.append(current_camelcase_path)
if (parent_element_arg is None): # 1st level Entity.
if element.keyword in ['container']:
depth_level = 2
fd.write('\n' + INDENTATION_BLOCK * depth_level + current_camelcase_path + ":")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: |")
depth_level += 1
if element.search_one('description') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + str(element.search_one('description').arg).replace('\n', '\n ').replace(' ', ' '))
if element.search_one('reference') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'Reference: ' + str(element.search_one('reference').arg).replace('\n', '\n ').replace(' ', ' '))
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'YANG module: ' + yang_module_name + '.yang')
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "allOf:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- $ref: \'" + OPENAPI_URL + "#/components/schemas/Entity\'")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- type: object")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "properties:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: NGSI-LD Entity identifier. It has to be " + current_camelcase_path + ".")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type: string")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "enum:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + current_camelcase_path)
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "default: " + current_camelcase_path)
depth_level -= 1
subelements = element.i_children
subelement_list = []
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords) and (subelement.keyword not in ['container', 'list']):
if str(subelement.arg) == "type":
name_subelement = str(element.arg + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + name_subelement + ":")
depth_level += 1
ref_subelement = (current_camelcase_path + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + ref_subelement + "\'")
else:
camelcase_subelement_arg = to_camelcase(str(subelement.keyword), str(subelement.arg))
fd.write('\n' + INDENTATION_BLOCK * depth_level + camelcase_subelement_arg + ":")
depth_level += 1
if yang_data_nodes_list.count(str(subelement.arg)) > 1:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + current_camelcase_path + str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize())) + "\'")
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize()) + "\'")
depth_level -= 1
depth_level -= 2
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- required:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- type")
if 'name' in subelement_list:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- name")
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords) and (subelement.keyword not in ['container', 'list']):
mandatory = subelement.search_one('mandatory')
if mandatory != None:
if str(mandatory.arg) == "true":
if str(subelement.arg) == "type":
name_subelement = str(element.arg + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + name_subelement)
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + to_camelcase(str(subelement.keyword), str(subelement.arg)))
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords):
generate_schemas(subelement, element.arg, current_path, current_camelcase_path, depth_level, typedefs_dict, typedefs_pattern_dict, yang_data_nodes_list)
elif element.keyword in ['list']:
depth_level = 2
fd.write('\n' + INDENTATION_BLOCK * depth_level + current_camelcase_path + ":")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: |")
depth_level += 1
if element.search_one('description') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + str(element.search_one('description').arg).replace('\n', '\n ').replace(' ', ' '))
if element.search_one('reference') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'Reference: ' + str(element.search_one('reference').arg).replace('\n', '\n ').replace(' ', ' '))
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'YANG module: ' + yang_module_name + '.yang')
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "allOf:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- $ref: \'" + OPENAPI_URL + "#/components/schemas/Entity\'")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- type: object")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "properties:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: NGSI-LD Entity identifier. It has to be " + current_camelcase_path + ".")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type: string")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "enum:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + current_camelcase_path)
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "default: " + current_camelcase_path)
depth_level -= 1
subelements = element.i_children
subelement_list = []
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords) and (subelement.keyword not in ['container', 'list']):
if str(subelement.arg) == "type":
name_subelement = str(element.arg + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + name_subelement + ":")
depth_level += 1
ref_subelement = (current_camelcase_path + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + ref_subelement + "\'")
else:
camelcase_subelement_arg = to_camelcase(str(subelement.keyword), str(subelement.arg))
subelement_list.append(camelcase_subelement_arg)
fd.write('\n' + INDENTATION_BLOCK * depth_level + camelcase_subelement_arg + ":")
depth_level += 1
if yang_data_nodes_list.count(str(subelement.arg)) > 1:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + current_camelcase_path + str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize())) + "\'")
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize()) + "\'")
depth_level -= 1
depth_level -= 2
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- required:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- type")
if 'name' in subelement_list:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- name")
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords) and (subelement.keyword not in ['container', 'list']):
mandatory = subelement.search_one('mandatory')
if mandatory != None:
if str(mandatory.arg) == "true":
if str(subelement.arg) == "type":
name_subelement = str(element.arg + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + name_subelement)
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + to_camelcase(str(subelement.keyword), str(subelement.arg)))
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords):
generate_schemas(subelement, element.arg, current_path, current_camelcase_path, depth_level, typedefs_dict, typedefs_pattern_dict, yang_data_nodes_list)
else: # 2nd level Entity onwards.
if element.keyword in ['container']:
depth_level = 2
fd.write('\n' + INDENTATION_BLOCK * depth_level + current_camelcase_path + ":")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: |")
depth_level += 1
if element.search_one('description') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + str(element.search_one('description').arg).replace('\n', '\n ').replace(' ', ' '))
if element.search_one('reference') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'Reference: ' + str(element.search_one('reference').arg).replace('\n', '\n ').replace(' ', ' '))
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'YANG module: ' + yang_module_name + '.yang')
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "allOf:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- $ref: \'" + OPENAPI_URL + "#/components/schemas/Entity\'")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- type: object")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "properties:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: NGSI-LD Entity identifier. It has to be " + current_camelcase_path + ".")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type: string")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "enum:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + current_camelcase_path)
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "default: " + current_camelcase_path)
depth_level -= 1
subelements = element.i_children
subelement_list = []
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords) and (subelement.keyword not in ['container', 'list']):
if str(subelement.arg) == "type":
name_subelement = str(element.arg + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + name_subelement + ":")
depth_level += 1
ref_subelement = (current_camelcase_path + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + ref_subelement + "\'")
else:
camelcase_subelement_arg = to_camelcase(str(subelement.keyword), str(subelement.arg))
fd.write('\n' + INDENTATION_BLOCK * depth_level + camelcase_subelement_arg + ":")
depth_level += 1
if yang_data_nodes_list.count(str(subelement.arg)) > 1:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + current_camelcase_path + str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize())) + "\'")
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize()) + "\'")
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "isPartOf:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/IsPartOf\'")
if(is_enclosing_container(element.parent) == True):
entity_type_parent = re.sub(r'_([^_]*)_$', '_', entity_path)
if(is_enclosing_container(element.parent.parent) == True):
parent = element.parent.parent
while True:
if(is_enclosing_container(parent) == True):
entity_type_parent = re.sub(r'_([^_]*)_$', '_', entity_type_parent)
parent = parent.parent
else:
break
entity_type_parent = re.sub(r'(_)(\w)', lambda m: m.group(2).upper(), entity_type_parent.capitalize())
entity_type_parent = re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), entity_type_parent)
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: isPartOf Relationship with Entity type " + entity_type_parent.replace('_','') + ".")
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: isPartOf Relationship with Entity type " + camelcase_entity_path + ".")
depth_level -= 1
depth_level -= 2
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- required:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- type")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- isPartOf")
if 'name' in subelement_list:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- name")
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords) and (subelement.keyword not in ['container', 'list']):
mandatory = subelement.search_one('mandatory')
if mandatory != None:
if str(mandatory.arg) == "true":
if str(subelement.arg) == "type":
name_subelement = str(element.arg + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + name_subelement)
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + to_camelcase(str(subelement.keyword), str(subelement.arg)))
subelements = element.i_children
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords):
generate_schemas(subelement, element.arg, current_path, current_camelcase_path, depth_level, typedefs_dict, typedefs_pattern_dict, yang_data_nodes_list)
elif element.keyword in ['list']:
depth_level = 2
fd.write('\n' + INDENTATION_BLOCK * depth_level + current_camelcase_path + ":")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: |")
depth_level += 1
if element.search_one('description') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + str(element.search_one('description').arg).replace('\n', '\n ').replace(' ', ' '))
if element.search_one('reference') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'Reference: ' + str(element.search_one('reference').arg).replace('\n', '\n ').replace(' ', ' '))
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'YANG module: ' + yang_module_name + '.yang')
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "allOf:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- $ref: \'" + OPENAPI_URL + "#/components/schemas/Entity\'")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- type: object")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "properties:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: NGSI-LD Entity identifier. It has to be " + current_camelcase_path + ".")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type: string")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "enum:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + current_camelcase_path)
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "default: " + current_camelcase_path)
depth_level -= 1
subelements = element.i_children
subelement_list = []
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords) and (subelement.keyword not in ['container', 'list']):
if str(subelement.arg) == "type":
name_subelement = str(element.arg + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + name_subelement + ":")
depth_level += 1
ref_subelement = (current_camelcase_path + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + ref_subelement + "\'")
else:
camelcase_subelement_arg = to_camelcase(str(subelement.keyword), str(subelement.arg))
fd.write('\n' + INDENTATION_BLOCK * depth_level + str(camelcase_subelement_arg) + ":")
depth_level += 1
if yang_data_nodes_list.count(str(subelement.arg)) > 1:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + current_camelcase_path + str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize())) + "\'")
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/" + re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize()) + "\'")
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "isPartOf:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "$ref: \'#/components/schemas/IsPartOf\'")
if(is_enclosing_container(element.parent) == True):
entity_type_parent = re.sub(r'_([^_]*)_$', '_', entity_path)
if(is_enclosing_container(element.parent.parent) == True):
parent = element.parent.parent
while True:
if(is_enclosing_container(parent) == True):
entity_type_parent = re.sub(r'_([^_]*)_$', '_', entity_type_parent)
parent = parent.parent
else:
break
entity_type_parent = re.sub(r'(_)(\w)', lambda m: m.group(2).upper(), entity_type_parent.capitalize())
entity_type_parent = re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), entity_type_parent)
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: isPartOf Relationship with Entity type " + entity_type_parent.replace('_','') + ".")
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: isPartOf Relationship with Entity type " + camelcase_entity_path + ".")
depth_level -= 1
depth_level -= 2
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- required:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- type")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- isPartOf")
if 'name' in subelement_list:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- name")
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords) and (subelement.keyword not in ['container', 'list']):
mandatory = subelement.search_one('mandatory')
if mandatory != None:
if str(mandatory.arg) == "true":
if str(subelement.arg) == "type":
name_subelement = str(element.arg + str(subelement.arg.capitalize())).replace('-','')
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + name_subelement)
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + to_camelcase(str(subelement.keyword), str(subelement.arg)))
subelements = element.i_children
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords):
generate_schemas(subelement, element.arg, current_path, current_camelcase_path, depth_level, typedefs_dict, typedefs_pattern_dict, yang_data_nodes_list)
### --- ###
### YANG CHOICE IDENTIFICATION: IT CONTAINS NGSI-LD PROPERTIES ###
elif (is_choice(element) == True) and (is_deprecated(element) == False):
depth_level = 2
current_camelcase_path = ''
if (yang_data_nodes_list.count(str(element.arg)) > 1) or (str(element.arg) == 'type'):
current_camelcase_path = camelcase_entity_path + str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), element.arg.capitalize()))
else:
current_camelcase_path = str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), element.arg.capitalize()))
fd.write('\n' + INDENTATION_BLOCK * depth_level + current_camelcase_path + ":")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: |")
depth_level += 1
if element.search_one('description') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + str(element.search_one('description').arg).replace('\n', '\n ').replace(' ', ' '))
if element.search_one('reference') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'Reference: ' + str(element.search_one('reference').arg).replace('\n', '\n ').replace(' ', ' '))
if element.search_one('units') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'Units: ' + str(element.search_one('units').arg).replace('\n', '\n ').replace(' ', ' '))
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'YANG module: ' + yang_module_name + '.yang')
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "oneOf:")
depth_level += 1
cases = element.i_children
if (cases is not None):
for case in cases:
if (case is not None) and (case.keyword in statements.data_definition_keywords) and (str(case.keyword) == "case"):
subelements = case.i_children
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords):
choice_camelcase_path = ''
if (yang_data_nodes_list.count(str(subelement.arg)) > 1) or (str(subelement.arg) == 'type') or (is_entity(subelement) == True):
choice_camelcase_path = current_camelcase_path + str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize()))
else:
choice_camelcase_path = str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), subelement.arg.capitalize()))
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- $ref: \'#/components/schemas/" + choice_camelcase_path + "\'")
depth_level -= 1
if (cases is not None):
for case in cases:
if (case is not None) and (case.keyword in statements.data_definition_keywords):
subelements = case.i_children
if (subelements is not None):
for subelement in subelements:
if (subelement is not None) and (subelement.keyword in statements.data_definition_keywords):
generate_schemas(subelement, element.arg, current_path, current_camelcase_path, depth_level, typedefs_dict, typedefs_pattern_dict, yang_data_nodes_list)
### --- ###
### NGSI-LD PROPERTY IDENTIFICATION ###
elif (is_property(element, typedefs_dict) == True) and (is_deprecated(element) == False):
depth_level = 2
current_camelcase_path = ''
if (yang_data_nodes_list.count(str(element.arg)) > 1) or (str(element.arg) == 'type'):
current_camelcase_path = camelcase_entity_path + str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), element.arg.capitalize()))
else:
current_camelcase_path = str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), element.arg.capitalize()))
openapi_schema_type = yang_to_openapi_schemas_types_conversion(str(element.search_one('type')).replace('type ', '').split(":")[-1], typedefs_dict)
openapi_schema_format = yang_to_openapi_schemas_formats_conversion(str(element.search_one('type')).replace('type ', '').split(":")[-1])
fd.write('\n' + INDENTATION_BLOCK * depth_level + current_camelcase_path + ":")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "description: |")
depth_level += 1
if element.search_one('description') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + str(element.search_one('description').arg).replace('\n', '\n ').replace(' ', ' '))
if element.search_one('reference') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'Reference: ' + str(element.search_one('reference').arg).replace('\n', '\n ').replace(' ', ' '))
if element.search_one('units') != None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'Units: ' + str(element.search_one('units').arg).replace('\n', '\n ').replace(' ', ' '))
fd.write('\n' + INDENTATION_BLOCK * depth_level + '\n ' + 'YANG module: ' + yang_module_name + '.yang')
depth_level -= 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "additionalProperties: false")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "allOf:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- $ref: \'" + OPENAPI_URL + "#/components/schemas/Property\'")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- type: object")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "properties:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "value:")
depth_level += 1
if str(openapi_schema_type) == "enum":
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type: " + "string")
fd.write('\n' + INDENTATION_BLOCK * depth_level + "enum:")
element_enums = element.search_one('type').search('enum')
if len(element_enums) == 0:
element_enums = element.search_one('type').i_typedef.search_one('type').search('enum')
depth_level += 1
for element_enum in element_enums:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- " + str(element_enum.arg))
depth_level -= 1
if element.search_one('default') is not None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "default: " + str(element.search_one('default').arg))
else:
if str(openapi_schema_type) == "array":
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type: " + str(openapi_schema_type))
fd.write('\n' + INDENTATION_BLOCK * depth_level + "items:" )
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type: string")
else:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "type: " + str(openapi_schema_type))
if is_datetime(element) == True:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "format: datetime")
if openapi_schema_format is not None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "format: " + openapi_schema_format)
if has_pattern(element, typedefs_pattern_dict) == True:
element_type = str(element.search_one('type')).replace('type ', '').split(':')[-1]
fd.write('\n' + INDENTATION_BLOCK * depth_level + "pattern: \'" + str(typedefs_pattern_dict.get(element_type)) + "\'")
if element.search_one('default') is not None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "default: " + str(element.search_one('default').arg))
'''
if element.search_one('maximum') is not None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "maximum: " + str(element.search_one('maximum').arg))
if element.search_one('minimum') is not None:
fd.write('\n' + INDENTATION_BLOCK * depth_level + "minimum: " + str(element.search_one('minimum').arg))
'''
if element.search_one('type').search_one('range') != None:
element_type_range = str(element.search_one('type').search_one('range').arg)
minimum = element_type_range.split("..")[0]
if '|' in minimum:
minimum = minimum.split("|")[0]
fd.write('\n' + INDENTATION_BLOCK * depth_level + "minimum: " + minimum)
maximum = element_type_range.split("..")[1]
if '|' in maximum:
maximum = maximum.split("|")[1]
fd.write('\n' + INDENTATION_BLOCK * depth_level + "maximum: " + maximum)
if str(openapi_schema_type) == "array":
depth_level -=1
depth_level -= 2
fd.write('\n' + INDENTATION_BLOCK * depth_level + "required:")
depth_level += 1
fd.write('\n' + INDENTATION_BLOCK * depth_level + "- value")
depth_level -= 4
### --- ###
### NGSI-LD RELATIONSHIP IDENTIFICATION ###
elif (is_relationship(element, typedefs_dict) == True) and (is_deprecated(element) == False):
depth_level = 2
current_camelcase_path = ''
if (yang_data_nodes_list.count(str(element.arg))) > 1 or (str(element.arg) == 'type'):
current_camelcase_path = camelcase_entity_path + str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), element.arg.capitalize()))
else:
current_camelcase_path = str(re.sub(r'(-)(\w)', lambda m: m.group(2).upper(), element.arg.capitalize()))
pointer = element.i_leafref_ptr[0]
pointer_parent = pointer.parent
camelcase_pointer_parent = to_camelcase(str(pointer_parent.keyword), str(pointer_parent.arg))
matches = [] # Best match is always the first element appended into the list: index 0.
for camelcase_entity in ENTITY_TYPE_LIST:
if camelcase_pointer_parent == camelcase_entity_path + camelcase_entity:
matches.append(camelcase_entity)
if len(matches) == 0:
childs = element.parent.i_children
matched_childs = 0
if (childs is not None) and (camelcase_entity_path + camelcase_pointer_parent) != current_camelcase_path:
has_childs = True
element_aux = element
iterations = 0
while has_childs == True:
for child in childs:
if child.arg == str(pointer_parent.arg):
matched_childs += 1
break
if matched_childs == 0:
for child in childs:
if is_entity(child) == True:
subchilds = child.i_children
if (subchilds is not None):
iterations += 1
for subchild in subchilds:
if subchild.arg == str(pointer_parent.arg):
matched_childs += 1
if matched_childs > 0:
if iterations > 0:
for camelcase_entity in ENTITY_TYPE_LIST:
if camelcase_pointer_parent == camelcase_entity or camelcase_pointer_parent in camelcase_entity:
matches.append(camelcase_entity)
if len(matches) == 0:
relationship_camelcase_path = camelcase_pointer_parent
else:
relationship_camelcase_path = matches[0]
else:
relationship_camelcase_path = camelcase_entity_path + camelcase_pointer_parent
ENTITY_TYPE_LIST.append(relationship_camelcase_path)
has_childs = False
else:
iterations += 1
element_aux = element_aux.parent
if element_aux.parent is not None:
childs = element_aux.parent.i_children
if (childs is None):
has_childs = False
relationship_camelcase_path = camelcase_pointer_parent
else:
has_childs = False
relationship_camelcase_path = camelcase_pointer_parent
else:
for camelcase_entity in ENTITY_TYPE_LIST:
if camelcase_pointer_parent == camelcase_entity or camelcase_pointer_parent in camelcase_entity:
matches.append(camelcase_entity)