aboutsummaryrefslogtreecommitdiffstats
path: root/src/tools/vppapigen/vppapigen_c.py
blob: 4227cae1ece41954e8aa1014a02036d7faac0f14 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
#
# Copyright (c) 2020 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# Provide two classes FromJSON and TOJSON that converts between JSON and VPP's
# binary API format
#

'''
This module creates C code for core VPP, VPP plugins and client side VAT and
VAT2 tests.
'''

import datetime
import os
import time
import sys
from io import StringIO
import shutil

process_imports = False


###############################################################################
class ToJSON():
    '''Class to generate functions converting from VPP binary API to JSON.'''
    _dispatch = {}
    noprint_fields = {'_vl_msg_id': None,
                      'client_index': None,
                      'context': None}
    is_number = {'u8': None,
                 'i8': None,
                 'u16': None,
                 'i16': None,
                 'u32': None,
                 'i32': None,
                 'u64': None,
                 'i64': None,
                 'f64': None,
                 }

    def __init__(self, module, types, defines, imported_types, stream):
        self.stream = stream
        self.module = module
        self.defines = defines
        self.types = types
        self.types_hash = {'vl_api_'+d.name+'_t':
                           d for d in types + imported_types}
        self.defines_hash = {d.name: d for d in defines}

    def header(self):
        '''Output the top boilerplate.'''
        write = self.stream.write
        write('#ifndef included_{}_api_tojson_h\n'.format(self.module))
        write('#define included_{}_api_tojson_h\n'.format(self.module))
        write('#include <vppinfra/cJSON.h>\n\n')
        write('#include <vat2/jsonconvert.h>\n\n')

    def footer(self):
        '''Output the bottom boilerplate.'''
        write = self.stream.write
        write('#endif\n')

    def get_base_type(self, t):
        vt_type = None
        try:
            vt = self.types_hash[t]
            if vt.type == 'Using' and 'length' not in vt.alias:
                vt_type = vt.alias['type']
        except KeyError:
            vt = t
        return vt, vt_type

    def get_json_func(self, t):
        '''Given the type, returns the function to use to create a
        cJSON object'''
        vt, vt_type = self.get_base_type(t)

        if t in self.is_number or vt_type in self.is_number:
            return 'cJSON_AddNumberToObject', '', False
        if t == 'bool':
            return 'cJSON_AddBoolToObject', '', False

        # Lookup type name check if it's enum
        if vt.type == 'Enum' or vt.type == 'EnumFlag':
            return '{t}_tojson'.format(t=t), '', True
        return '{t}_tojson'.format(t=t), '&', True

    def get_json_array_func(self, t):
        '''Given a type returns the function to create a cJSON object
        for arrays.'''
        if t in self.is_number:
            return 'cJSON_CreateNumber', ''
        if t == 'bool':
            return 'cJSON_CreateBool', ''
        vt, vt_type = self.get_base_type(t)
        if vt.type == 'Enum' or vt.type == 'EnumFlag':
            return '{t}_tojson'.format(t=t), ''
        return '{t}_tojson'.format(t=t), '&'

    def print_string(self, o):
        '''Create cJSON object from vl_api_string_t'''
        write = self.stream.write
        if o.modern_vla:
            write('    vl_api_string_cJSON_AddToObject(o, "{n}", &a->{n});\n'
                  .format(n=o.fieldname))
        else:

            write('    cJSON_AddStringToObject(o, "{n}", (char *)a->{n});\n'
                  .format(n=o.fieldname))

    def print_field(self, o):
        '''Called for every field in a typedef or define.'''
        write = self.stream.write
        if o.fieldname in self.noprint_fields:
            return

        f, p, newobj = self.get_json_func(o.fieldtype)

        if newobj:
            write('    cJSON_AddItemToObject(o, "{n}", {f}({p}a->{n}));\n'
                  .format(f=f, p=p, n=o.fieldname))
        else:
            write('    {f}(o, "{n}", {p}a->{n});\n'
                  .format(f=f, p=p, n=o.fieldname))

    _dispatch['Field'] = print_field

    def print_array(self, o):
        '''Converts a VPP API array to cJSON array.'''
        write = self.stream.write

        forloop = '''\
    {{
        int i;
        cJSON *array = cJSON_AddArrayToObject(o, "{n}");
        for (i = 0; i < {lfield}; i++) {{
            cJSON_AddItemToArray(array, {f}({p}a->{n}[i]));
        }}
    }}
'''

        if o.fieldtype == 'string':
            self.print_string(o)
            return

        lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
        if o.fieldtype == 'u8':
            write('    {\n')
            # What is length field doing here?
            write('    u8 *s = format(0, "0x%U", format_hex_bytes, '
                  '&a->{n}, {lfield});\n'
                  .format(n=o.fieldname, lfield=lfield))
            write('    cJSON_AddStringToObject(o, "{n}", (char *)s);\n'
                  .format(n=o.fieldname))
            write('    vec_free(s);\n')
            write('    }\n')
            return

        f, p = self.get_json_array_func(o.fieldtype)
        write(forloop.format(lfield=lfield,
                             t=o.fieldtype,
                             n=o.fieldname,
                             f=f,
                             p=p))

    _dispatch['Array'] = print_array

    def print_enum(self, o):
        '''Create cJSON object (string) for VPP API enum'''
        write = self.stream.write
        write('static inline cJSON *vl_api_{name}_t_tojson '
              '(vl_api_{name}_t a) {{\n'.format(name=o.name))

        write("    switch(a) {\n")
        for b in o.block:
            write("    case %s:\n" % b[1])
            write('        return cJSON_CreateString("{}");\n'.format(b[0]))
        write('    default: return cJSON_CreateString("Invalid ENUM");\n')
        write('    }\n')
        write('    return 0;\n')
        write('}\n')

    _dispatch['Enum'] = print_enum

    def print_enum_flag(self, o):
        '''Create cJSON object (string) for VPP API enum'''
        write = self.stream.write
        write('static inline cJSON *vl_api_{name}_t_tojson '
              '(vl_api_{name}_t a) {{\n'.format(name=o.name))
        write('    cJSON *array = cJSON_CreateArray();\n')

        for b in o.block:
            write('    if (a & {})\n'.format(b[0]))
            write('       cJSON_AddItemToArray(array, cJSON_CreateString("{}"));\n'.format(b[0]))
        write('    return array;\n')
        write('}\n')

    _dispatch['EnumFlag'] = print_enum_flag

    def print_typedef(self, o):
        '''Create cJSON (dictionary) object from VPP API typedef'''
        write = self.stream.write
        write('static inline cJSON *vl_api_{name}_t_tojson '
              '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
        write('    cJSON *o = cJSON_CreateObject();\n')

        for t in o.block:
            self._dispatch[t.type](self, t)

        write('    return o;\n')
        write('}\n')

    def print_define(self, o):
        '''Create cJSON (dictionary) object from VPP API define'''
        write = self.stream.write
        write('static inline cJSON *vl_api_{name}_t_tojson '
              '(vl_api_{name}_t *a) {{\n'.format(name=o.name))
        write('    cJSON *o = cJSON_CreateObject();\n')
        write('    cJSON_AddStringToObject(o, "_msgname", "{}");\n'
              .format(o.name))

        for t in o.block:
            self._dispatch[t.type](self, t)

        write('    return o;\n')
        write('}\n')

    def print_using(self, o):
        '''Create cJSON (dictionary) object from VPP API aliased type'''
        if o.manual_print:
            return

        write = self.stream.write
        write('static inline cJSON *vl_api_{name}_t_tojson '
              '(vl_api_{name}_t *a) {{\n'.format(name=o.name))

        write('    u8 *s = format(0, "%U", format_vl_api_{}_t, a);\n'
              .format(o.name))
        write('    cJSON *o = cJSON_CreateString((char *)s);\n')
        write('    vec_free(s);\n')
        write('    return o;\n')
        write('}\n')

    _dispatch['Typedef'] = print_typedef
    _dispatch['Define'] = print_define
    _dispatch['Using'] = print_using
    _dispatch['Union'] = print_typedef

    def generate_function(self, t):
        '''Main entry point'''
        write = self.stream.write
        if t.manual_print:
            write('/* Manual print {} */\n'.format(t.name))
            return
        self._dispatch[t.type](self, t)

    def generate_types(self):
        '''Main entry point'''
        for t in self.types:
            self.generate_function(t)

    def generate_defines(self):
        '''Main entry point'''
        for t in self.defines:
            self.generate_function(t)


class FromJSON():
    '''
    Parse JSON objects into VPP API binary message structures.
    '''
    _dispatch = {}
    noprint_fields = {'_vl_msg_id': None,
                      'client_index': None,
                      'context': None}
    is_number = {'u8': None,
                 'i8': None,
                 'u16': None,
                 'i16': None,
                 'u32': None,
                 'i32': None,
                 'u64': None,
                 'i64': None,
                 'f64': None,
                 }

    def __init__(self, module, types, defines, imported_types, stream):
        self.stream = stream
        self.module = module
        self.defines = defines
        self.types = types
        self.types_hash = {'vl_api_'+d.name+'_t':
                           d for d in types + imported_types}
        self.defines_hash = {d.name: d for d in defines}

    def header(self):
        '''Output the top boilerplate.'''
        write = self.stream.write
        write('#ifndef included_{}_api_fromjson_h\n'.format(self.module))
        write('#define included_{}_api_fromjson_h\n'.format(self.module))
        write('#include <vppinfra/cJSON.h>\n\n')
        write('#include <vat2/jsonconvert.h>\n\n')
        write('#pragma GCC diagnostic ignored "-Wunused-label"\n')

    def is_base_type(self, t):
        '''Check if a type is one of the VPP API base types'''
        if t in self.is_number:
            return True
        if t == 'bool':
            return True
        return False

    def footer(self):
        '''Output the bottom boilerplate.'''
        write = self.stream.write
        write('#endif\n')

    def print_string(self, o, toplevel=False):
        '''Convert JSON string to vl_api_string_t'''
        write = self.stream.write

        msgvar = "a" if toplevel else "*mp"
        msgsize = "l" if toplevel else "*len"

        if o.modern_vla:
            write('    char *p = cJSON_GetStringValue(item);\n')
            write('    size_t plen = strlen(p);\n')
            write('    {msgvar} = realloc({msgvar}, {msgsize} + plen);\n'
                  .format(msgvar=msgvar, msgsize=msgsize))
            write('    if ({msgvar} == 0) goto error;\n'.format(msgvar=msgvar))
            write('    vl_api_c_string_to_api_string(p, (void *){msgvar} + '
                  '{msgsize} - sizeof(vl_api_string_t));\n'
                  .format(msgvar=msgvar, msgsize=msgsize))
            write('    {msgsize} += plen;\n'.format(msgsize=msgsize))
        else:
            write('    strncpy_s((char *)a->{n}, sizeof(a->{n}), '
                  'cJSON_GetStringValue(item), sizeof(a->{n}) - 1);\n'
                  .format(n=o.fieldname))

    def print_field(self, o, toplevel=False):
        '''Called for every field in a typedef or define.'''
        write = self.stream.write
        if o.fieldname in self.noprint_fields:
            return
        is_bt = self.is_base_type(o.fieldtype)
        t = 'vl_api_{}'.format(o.fieldtype) if is_bt else o.fieldtype

        msgvar = "(void **)&a" if toplevel else "mp"
        msgsize = "&l" if toplevel else "len"

        if is_bt:
            write('    vl_api_{t}_fromjson(item, &a->{n});\n'
                  .format(t=o.fieldtype, n=o.fieldname))
        else:
            write('    if ({t}_fromjson({msgvar}, '
                  '{msgsize}, item, &a->{n}) < 0) goto error;\n'
                  .format(t=t, n=o.fieldname, msgvar=msgvar, msgsize=msgsize))

    _dispatch['Field'] = print_field

    def print_array(self, o, toplevel=False):
        '''Convert JSON array to VPP API array'''
        write = self.stream.write

        forloop = '''\
    {{
        int i;
        cJSON *array = cJSON_GetObjectItem(o, "{n}");
        int size = cJSON_GetArraySize(array);
        if (size != {lfield}) goto error;
        for (i = 0; i < size; i++) {{
            cJSON *e = cJSON_GetArrayItem(array, i);
            {call}
        }}
    }}
'''
        forloop_vla = '''\
    {{
        int i;
        cJSON *array = cJSON_GetObjectItem(o, "{n}");
        int size = cJSON_GetArraySize(array);
        {lfield} = size;
        {realloc} = realloc({realloc}, {msgsize} + sizeof({t}) * size);
        {t} *d = (void *){realloc} + {msgsize};
        {msgsize} += sizeof({t}) * size;
        for (i = 0; i < size; i++) {{
            cJSON *e = cJSON_GetArrayItem(array, i);
            {call}
        }}
    }}
'''
        t = o.fieldtype
        if o.fieldtype == 'string':
            self.print_string(o, toplevel)
            return

        lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
        msgvar = "(void **)&a" if toplevel else "mp"
        realloc = "a" if toplevel else "*mp"
        msgsize = "l" if toplevel else "*len"

        if o.fieldtype == 'u8':
            if o.lengthfield:
                write('    s = u8string_fromjson(o, "{}");\n'
                      .format(o.fieldname))
                write('    if (!s) goto error;\n')
                write('    {} = vec_len(s);\n'.format(lfield))

                write('    {realloc} = realloc({realloc}, {msgsize} + '
                      'vec_len(s));\n'.format(msgvar=msgvar, msgsize=msgsize, realloc=realloc))
                write('    memcpy((void *){realloc} + {msgsize}, s, '
                      'vec_len(s));\n'.format(realloc=realloc, msgsize=msgsize))
                write('    {msgsize} += vec_len(s);\n'.format(msgsize=msgsize))

                write('    vec_free(s);\n')
            else:
                write('    if (u8string_fromjson2(o, "{n}", a->{n}) < 0) goto error;\n'
                      .format(n=o.fieldname))
            return

        is_bt = self.is_base_type(o.fieldtype)

        if o.lengthfield:
            if is_bt:
                call = ('vl_api_{t}_fromjson(e, &d[i]);'
                        .format(t=o.fieldtype))
            else:
                call = ('if ({t}_fromjson({msgvar}, len, e, &d[i]) < 0) goto error; '
                        .format(t=o.fieldtype, msgvar=msgvar))
            write(forloop_vla.format(lfield=lfield,
                                     t=o.fieldtype,
                                     n=o.fieldname,
                                     call=call,
                                     realloc=realloc,
                                     msgsize=msgsize))
        else:
            if is_bt:
                call = ('vl_api_{t}_fromjson(e, &a->{n}[i]);'
                        .format(t=t, n=o.fieldname))
            else:
                call = ('if ({}_fromjson({}, len, e, &a->{}[i]) < 0) goto error;'
                        .format(t, msgvar, o.fieldname))
            write(forloop.format(lfield=lfield,
                                 t=t,
                                 n=o.fieldname,
                                 call=call,
                                 msgvar=msgvar,
                                 realloc=realloc,
                                 msgsize=msgsize))

    _dispatch['Array'] = print_array

    def print_enum(self, o):
        '''Convert to JSON enum(string) to VPP API enum (int)'''
        write = self.stream.write
        write('static inline int vl_api_{n}_t_fromjson'
              '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
              .format(n=o.name))
        write('    char *p = cJSON_GetStringValue(o);\n')
        for b in o.block:
            write('    if (strcmp(p, "{}") == 0) {{*a = {}; return 0;}}\n'
                  .format(b[0], b[1]))
        write('    *a = 0;\n')
        write('    return -1;\n')
        write('}\n')

    _dispatch['Enum'] = print_enum

    def print_enum_flag(self, o):
        '''Convert to JSON enum(string) to VPP API enum (int)'''
        write = self.stream.write
        write('static inline int vl_api_{n}_t_fromjson '
              '(void **mp, int *len, cJSON *o, vl_api_{n}_t *a) {{\n'
              .format(n=o.name))
        write('   int i;\n')
        write('   *a = 0;\n')
        write('   for (i = 0; i < cJSON_GetArraySize(o); i++) {\n')
        write('       cJSON *e = cJSON_GetArrayItem(o, i);\n')
        write('       char *p = cJSON_GetStringValue(e);\n')
        write('       if (!p) return -1;\n')
        for b in o.block:
            write('       if (strcmp(p, "{}") == 0) *a |= {};\n'
                  .format(b[0], b[1]))
        write('    }\n')
        write('   return 0;\n')
        write('}\n')

    _dispatch['EnumFlag'] = print_enum_flag

    def print_typedef(self, o):
        '''Convert from JSON object to VPP API binary representation'''
        write = self.stream.write

        write('static inline int vl_api_{name}_t_fromjson (void **mp, '
              'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
              .format(name=o.name))
        write('    cJSON *item __attribute__ ((unused));\n')
        write('    u8 *s __attribute__ ((unused));\n')
        for t in o.block:
            if t.type == 'Field' and t.is_lengthfield:
                continue
            write('\n    item = cJSON_GetObjectItem(o, "{}");\n'
                  .format(t.fieldname))
            write('    if (!item) goto error;\n')
            self._dispatch[t.type](self, t)

        write('\n    return 0;\n')
        write('\n  error:\n')
        write('    return -1;\n')
        write('}\n')

    def print_union(self, o):
        '''Convert JSON object to VPP API binary union'''
        write = self.stream.write

        write('static inline int vl_api_{name}_t_fromjson (void **mp, '
              'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
              .format(name=o.name))
        write('    cJSON *item __attribute__ ((unused));\n')
        write('    u8 *s __attribute__ ((unused));\n')
        for t in o.block:
            if t.type == 'Field' and t.is_lengthfield:
                continue
            write('    item = cJSON_GetObjectItem(o, "{}");\n'
                  .format(t.fieldname))
            write('    if (item) {\n')
            self._dispatch[t.type](self, t)
            write('    };\n')
        write('\n    return 0;\n')
        write('\n  error:\n')
        write('    return -1;\n')
        write('}\n')

    def print_define(self, o):
        '''Convert JSON object to VPP API message'''
        write = self.stream.write
        error = 0
        write('static inline vl_api_{name}_t *vl_api_{name}_t_fromjson '
              '(cJSON *o, int *len) {{\n'.format(name=o.name))
        write('    cJSON *item __attribute__ ((unused));\n')
        write('    u8 *s __attribute__ ((unused));\n')
        write('    int l = sizeof(vl_api_{}_t);\n'.format(o.name))
        write('    vl_api_{}_t *a = malloc(l);\n'.format(o.name))
        write('\n')

        for t in o.block:
            if t.fieldname in self.noprint_fields:
                continue
            if t.type == 'Field' and t.is_lengthfield:
                continue
            write('    item = cJSON_GetObjectItem(o, "{}");\n'
                  .format(t.fieldname))
            write('    if (!item) goto error;\n')
            error += 1
            self._dispatch[t.type](self, t, toplevel=True)
            write('\n')

        write('    *len = l;\n')
        write('    return a;\n')

        if error:
            write('\n  error:\n')
            write('    free(a);\n')
            write('    return 0;\n')
        write('}\n')

    def print_using(self, o):
        '''Convert JSON field to VPP type alias'''
        write = self.stream.write

        if o.manual_print:
            return

        t = o.using
        write('static inline int vl_api_{name}_t_fromjson (void **mp, '
              'int *len, cJSON *o, vl_api_{name}_t *a) {{\n'
              .format(name=o.name))
        if 'length' in o.alias:
            if t.fieldtype != 'u8':
                raise ValueError("Error in processing type {} for {}"
                                 .format(t.fieldtype, o.name))
            write('    vl_api_u8_string_fromjson(o, (u8 *)a, {});\n'
                  .format(o.alias['length']))
        else:
            write('    vl_api_{t}_fromjson(o, ({t} *)a);\n'
                  .format(t=t.fieldtype))

        write('    return 0;\n')
        write('}\n')

    _dispatch['Typedef'] = print_typedef
    _dispatch['Define'] = print_define
    _dispatch['Using'] = print_using
    _dispatch['Union'] = print_union

    def generate_function(self, t):
        '''Main entry point'''
        write = self.stream.write
        if t.manual_print:
            write('/* Manual print {} */\n'.format(t.name))
            return
        self._dispatch[t.type](self, t)

    def generate_types(self):
        '''Main entry point'''
        for t in self.types:
            self.generate_function(t)

    def generate_defines(self):
        '''Main entry point'''
        for t in self.defines:
            self.generate_function(t)


def generate_tojson(s, modulename, stream):
    '''Generate all functions to convert from API to JSON'''
    write = stream.write

    write('/* Imported API files */\n')
    for i in s['Import']:
        f = i.filename.replace('plugins/', '')
        write('#include <{}_tojson.h>\n'.format(f))

    pp = ToJSON(modulename, s['types'], s['Define'], s['imported']['types'],
                stream)
    pp.header()
    pp.generate_types()
    pp.generate_defines()
    pp.footer()
    return ''


def generate_fromjson(s, modulename, stream):
    '''Generate all functions to convert from JSON to API'''
    write = stream.write
    write('/* Imported API files */\n')
    for i in s['Import']:
        f = i.filename.replace('plugins/', '')
        write('#include <{}_fromjson.h>\n'.format(f))

    pp = FromJSON(modulename, s['types'], s['Define'], s['imported']['types'],
                  stream)
    pp.header()
    pp.generate_types()
    pp.generate_defines()
    pp.footer()

    return ''

###############################################################################


DATESTRING = datetime.datetime.utcfromtimestamp(
    int(os.environ.get('SOURCE_DATE_EPOCH', time.time())))
TOP_BOILERPLATE = '''\
/*
 * VLIB API definitions {datestring}
 * Input file: {input_filename}
 * Automatically generated: please edit the input file NOT this file!
 */

#include <stdbool.h>
#if defined(vl_msg_id)||defined(vl_union_id) \\
    || defined(vl_printfun) ||defined(vl_endianfun) \\
    || defined(vl_api_version)||defined(vl_typedefs) \\
    || defined(vl_msg_name)||defined(vl_msg_name_crc_list) \\
    || defined(vl_api_version_tuple)
/* ok, something was selected */
#else
#warning no content included from {input_filename}
#endif

#define VL_API_PACKED(x) x __attribute__ ((packed))
'''

BOTTOM_BOILERPLATE = '''\
/****** API CRC (whole file) *****/

#ifdef vl_api_version
vl_api_version({input_filename}, {file_crc:#08x})

#endif
'''


def msg_ids(s):
    '''Generate macro to map API message id to handler'''
    output = '''\

/****** Message ID / handler enum ******/

#ifdef vl_msg_id
'''

    for t in s['Define']:
        output += "vl_msg_id(VL_API_%s, vl_api_%s_t_handler)\n" % \
                  (t.name.upper(), t.name)
    output += "#endif"

    return output


def msg_names(s):
    '''Generate calls to name mapping macro'''
    output = '''\

/****** Message names ******/

#ifdef vl_msg_name
'''

    for t in s['Define']:
        dont_trace = 0 if t.dont_trace else 1
        output += "vl_msg_name(vl_api_%s_t, %d)\n" % (t.name, dont_trace)
    output += "#endif"

    return output


def msg_name_crc_list(s, suffix):
    '''Generate list of names to CRC mappings'''
    output = '''\

/****** Message name, crc list ******/

#ifdef vl_msg_name_crc_list
'''
    output += "#define foreach_vl_msg_name_crc_%s " % suffix

    for t in s['Define']:
        output += "\\\n_(VL_API_%s, %s, %08x) " % \
                   (t.name.upper(), t.name, t.crc)
    output += "\n#endif"

    return output


def api2c(fieldtype):
    '''Map between API type names and internal VPP type names'''
    mappingtable = {'string': 'vl_api_string_t', }
    if fieldtype in mappingtable:
        return mappingtable[fieldtype]
    return fieldtype


def typedefs(filename):
    '''Include in the main files to the types file'''
    output = '''\

/****** Typedefs ******/

#ifdef vl_typedefs
#include "{include}.api_types.h"
#endif
'''.format(include=filename)
    return output


FORMAT_STRINGS = {'u8': '%u',
                  'bool': '%u',
                  'i8': '%d',
                  'u16': '%u',
                  'i16': '%d',
                  'u32': '%u',
                  'i32': '%ld',
                  'u64': '%llu',
                  'i64': '%lld',
                  'f64': '%.2f'}


class Printfun():
    '''Functions for pretty printing VPP API messages'''
    _dispatch = {}
    noprint_fields = {'_vl_msg_id': None,
                      'client_index': None,
                      'context': None}

    def __init__(self, stream):
        self.stream = stream

    @staticmethod
    def print_string(o, stream):
        '''Pretty print a vl_api_string_t'''
        write = stream.write
        if o.modern_vla:
            write('    if (vl_api_string_len(&a->{f}) > 0) {{\n'
                  .format(f=o.fieldname))
            write('        s = format(s, "\\n%U{f}: %U", '
                  'format_white_space, indent, '
                  'vl_api_format_string, (&a->{f}));\n'.format(f=o.fieldname))
            write('    } else {\n')
            write('        s = format(s, "\\n%U{f}:", '
                  'format_white_space, indent);\n'.format(f=o.fieldname))
            write('    }\n')
        else:
            write('    s = format(s, "\\n%U{f}: %s", '
                  'format_white_space, indent, a->{f});\n'
                  .format(f=o.fieldname))

    def print_field(self, o, stream):
        '''Pretty print API field'''
        write = stream.write
        if o.fieldname in self.noprint_fields:
            return
        if o.fieldtype in FORMAT_STRINGS:
            f = FORMAT_STRINGS[o.fieldtype]
            write('    s = format(s, "\\n%U{n}: {f}", '
                  'format_white_space, indent, a->{n});\n'
                  .format(n=o.fieldname, f=f))
        else:
            write('    s = format(s, "\\n%U{n}: %U", '
                  'format_white_space, indent, '
                  'format_{t}, &a->{n}, indent);\n'
                  .format(n=o.fieldname, t=o.fieldtype))

    _dispatch['Field'] = print_field

    def print_array(self, o, stream):
        '''Pretty print API array'''
        write = stream.write

        forloop = '''\
    for (i = 0; i < {lfield}; i++) {{
        s = format(s, "\\n%U{n}: %U",
                   format_white_space, indent, format_{t}, &a->{n}[i], indent);
    }}
'''

        forloop_format = '''\
    for (i = 0; i < {lfield}; i++) {{
        s = format(s, "\\n%U{n}: {t}",
                   format_white_space, indent, a->{n}[i]);
    }}
'''

        if o.fieldtype == 'string':
            self.print_string(o, stream)
            return

        if o.fieldtype == 'u8':
            if o.lengthfield:
                write('    s = format(s, "\\n%U{n}: %U", format_white_space, '
                      'indent, format_hex_bytes, a->{n}, a->{lfield});\n'
                      .format(n=o.fieldname, lfield=o.lengthfield))
            else:
                write('    s = format(s, "\\n%U{n}: %U", format_white_space, '
                      'indent, format_hex_bytes, a, {lfield});\n'
                      .format(n=o.fieldname, lfield=o.length))
            return

        lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
        if o.fieldtype in FORMAT_STRINGS:
            write(forloop_format.format(lfield=lfield,
                                        t=FORMAT_STRINGS[o.fieldtype],
                                        n=o.fieldname))
        else:
            write(forloop.format(lfield=lfield, t=o.fieldtype, n=o.fieldname))

    _dispatch['Array'] = print_array

    @staticmethod
    def print_alias(k, v, stream):
        '''Pretty print type alias'''
        write = stream.write
        if ('length' in v.alias and v.alias['length'] and
                v.alias['type'] == 'u8'):
            write('    return format(s, "%U", format_hex_bytes, a, {});\n'
                  .format(v.alias['length']))
        elif v.alias['type'] in FORMAT_STRINGS:
            write('    return format(s, "{}", *a);\n'
                  .format(FORMAT_STRINGS[v.alias['type']]))
        else:
            write('    return format(s, "{} (print not implemented)");\n'
                  .format(k))

    @staticmethod
    def print_enum(o, stream):
        '''Pretty print API enum'''
        write = stream.write
        write("    switch(*a) {\n")
        for b in o:
            write("    case %s:\n" % b[1])
            write('        return format(s, "{}");\n'.format(b[0]))
        write('    }\n')

    _dispatch['Enum'] = print_enum
    _dispatch['EnumFlag'] = print_enum

    def print_obj(self, o, stream):
        '''Entry point'''
        write = stream.write

        if o.type in self._dispatch:
            self._dispatch[o.type](self, o, stream)
        else:
            write('    s = format(s, "\\n{} {} {} (print not implemented");\n'
                  .format(o.type, o.fieldtype, o.fieldname))


def printfun(objs, stream, modulename):
    '''Main entry point for pretty print function generation'''
    write = stream.write

    h = '''\
/****** Print functions *****/
#ifdef vl_printfun
#ifndef included_{module}_printfun
#define included_{module}_printfun

#ifdef LP64
#define _uword_fmt \"%lld\"
#define _uword_cast (long long)
#else
#define _uword_fmt \"%ld\"
#define _uword_cast long
#endif

'''

    signature = '''\
static inline void *vl_api_{name}_t_print (vl_api_{name}_t *a, void *handle)
{{
    u8 *s = 0;
    u32 indent __attribute__((unused)) = 2;
    int i __attribute__((unused));
'''

    h = h.format(module=modulename)
    write(h)

    pp = Printfun(stream)
    for t in objs:
        if t.manual_print:
            write("/***** manual: vl_api_%s_t_print  *****/\n\n" % t.name)
            continue
        write(signature.format(name=t.name))
        write('    /* Message definition: vl_api_{}_t: */\n'.format(t.name))
        write("    s = format(s, \"vl_api_%s_t:\");\n" % t.name)
        for o in t.block:
            pp.print_obj(o, stream)
        write('    vec_add1(s, 0);\n')
        write('    vl_print (handle, (char *)s);\n')
        write('    vec_free (s);\n')
        write('    return handle;\n')
        write('}\n\n')

    write("\n#endif")
    write("\n#endif /* vl_printfun */\n")

    return ''


def printfun_types(objs, stream, modulename):
    '''Pretty print API types'''
    write = stream.write
    pp = Printfun(stream)

    h = '''\
/****** Print functions *****/
#ifdef vl_printfun
#ifndef included_{module}_printfun_types
#define included_{module}_printfun_types

'''
    h = h.format(module=modulename)
    write(h)

    signature = '''\
static inline u8 *format_vl_api_{name}_t (u8 *s, va_list * args)
{{
    vl_api_{name}_t *a = va_arg (*args, vl_api_{name}_t *);
    u32 indent __attribute__((unused)) = va_arg (*args, u32);
    int i __attribute__((unused));
    indent += 2;
'''

    for t in objs:
        if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag':
            write(signature.format(name=t.name))
            pp.print_enum(t.block, stream)
            write('    return s;\n')
            write('}\n\n')
            continue

        if t.manual_print:
            write("/***** manual: vl_api_%s_t_print  *****/\n\n" % t.name)
            continue

        if t.__class__.__name__ == 'Using':
            write(signature.format(name=t.name))
            pp.print_alias(t.name, t, stream)
            write('}\n\n')
            continue

        write(signature.format(name=t.name))
        for o in t.block:
            pp.print_obj(o, stream)

        write('    return s;\n')
        write('}\n\n')

    write("\n#endif")
    write("\n#endif /* vl_printfun_types */\n")


def generate_imports(imports):
    '''Add #include matching the API import statements'''
    output = '/* Imported API files */\n'
    output += '#ifndef vl_api_version\n'

    for i in imports:
        s = i.filename.replace('plugins/', '')
        output += '#include <{}.h>\n'.format(s)
    output += '#endif\n'
    return output


ENDIAN_STRINGS = {
    'u16': 'clib_net_to_host_u16',
    'u32': 'clib_net_to_host_u32',
    'u64': 'clib_net_to_host_u64',
    'i16': 'clib_net_to_host_i16',
    'i32': 'clib_net_to_host_i32',
    'i64': 'clib_net_to_host_i64',
    'f64': 'clib_net_to_host_f64',
}


def endianfun_array(o):
    '''Generate endian functions for arrays'''
    forloop = '''\
    for (i = 0; i < {length}; i++) {{
        a->{name}[i] = {format}(a->{name}[i]);
    }}
'''

    forloop_format = '''\
    for (i = 0; i < {length}; i++) {{
        {type}_endian(&a->{name}[i]);
    }}
'''

    output = ''
    if o.fieldtype == 'u8' or o.fieldtype == 'string' or o.fieldtype == 'bool':
        output += '    /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
    else:
        lfield = 'a->' + o.lengthfield if o.lengthfield else o.length
        if o.fieldtype in ENDIAN_STRINGS:
            output += (forloop
                       .format(length=lfield,
                               format=ENDIAN_STRINGS[o.fieldtype],
                               name=o.fieldname))
        else:
            output += (forloop_format
                       .format(length=lfield, type=o.fieldtype,
                               name=o.fieldname))
    return output


NO_ENDIAN_CONVERSION = {'client_index': None}


def endianfun_obj(o):
    '''Generate endian conversion function for type'''
    output = ''
    if o.type == 'Array':
        return endianfun_array(o)
    if o.type != 'Field':
        output += ('    s = format(s, "\\n{} {} {} (print not implemented");\n'
                   .format(o.type, o.fieldtype, o.fieldname))
        return output
    if o.fieldname in NO_ENDIAN_CONVERSION:
        output += '    /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)
        return output
    if o.fieldtype in ENDIAN_STRINGS:
        output += ('    a->{name} = {format}(a->{name});\n'
                   .format(name=o.fieldname,
                           format=ENDIAN_STRINGS[o.fieldtype]))
    elif o.fieldtype.startswith('vl_api_'):
        output += ('    {type}_endian(&a->{name});\n'
                   .format(type=o.fieldtype, name=o.fieldname))
    else:
        output += '    /* a->{n} = a->{n} (no-op) */\n'.format(n=o.fieldname)

    return output


def endianfun(objs, modulename):
    '''Main entry point for endian function generation'''
    output = '''\

/****** Endian swap functions *****/\n\
#ifdef vl_endianfun
#ifndef included_{module}_endianfun
#define included_{module}_endianfun

#undef clib_net_to_host_uword
#ifdef LP64
#define clib_net_to_host_uword clib_net_to_host_u64
#else
#define clib_net_to_host_uword clib_net_to_host_u32
#endif

'''
    output = output.format(module=modulename)

    signature = '''\
static inline void vl_api_{name}_t_endian (vl_api_{name}_t *a)
{{
    int i __attribute__((unused));
'''

    for t in objs:
        if t.__class__.__name__ == 'Enum' or t.__class__.__name__ == 'EnumFlag' :
            output += signature.format(name=t.name)
            if t.enumtype in ENDIAN_STRINGS:
                output += ('    *a = {}(*a);\n'
                           .format(ENDIAN_STRINGS[t.enumtype]))
            else:
                output += ('    /* a->{name} = a->{name} (no-op) */\n'
                           .format(name=t.name))

            output += '}\n\n'
            continue

        if t.manual_endian:
            output += "/***** manual: vl_api_%s_t_endian  *****/\n\n" % t.name
            continue

        if t.__class__.__name__ == 'Using':
            output += signature.format(name=t.name)
            if ('length' in t.alias and t.alias['length'] and
                    t.alias['type'] == 'u8'):
                output += ('    /* a->{name} = a->{name} (no-op) */\n'
                           .format(name=t.name))
            elif t.alias['type'] in FORMAT_STRINGS:
                output += ('    *a = {}(*a);\n'
                           .format(ENDIAN_STRINGS[t.alias['type']]))
            else:
                output += '    /* Not Implemented yet {} */'.format(t.name)
            output += '}\n\n'
            continue

        output += signature.format(name=t.name)

        for o in t.block:
            output += endianfun_obj(o)
        output += '}\n\n'

    output += "\n#endif"
    output += "\n#endif /* vl_endianfun */\n\n"

    return output


def version_tuple(s, module):
    '''Generate semantic version string'''
    output = '''\
/****** Version tuple *****/

#ifdef vl_api_version_tuple

'''
    if 'version' in s['Option']:
        v = s['Option']['version']
        (major, minor, patch) = v.split('.')
        output += "vl_api_version_tuple(%s, %s, %s, %s)\n" % \
                  (module, major, minor, patch)

    output += "\n#endif /* vl_api_version_tuple */\n\n"

    return output


def generate_include_enum(s, module, stream):
    '''Generate <name>.api_enum.h'''
    write = stream.write

    if 'Define' in s:
        write('typedef enum {\n')
        for t in s['Define']:
            write('   VL_API_{},\n'.format(t.name.upper()))
        write('   VL_MSG_{}_LAST\n'.format(module.upper()))
        write('}} vl_api_{}_enum_t;\n'.format(module))


def generate_include_counters(s, stream):
    '''Include file for the counter data model types.'''
    write = stream.write

    for counters in s:
        csetname = counters.name
        write('typedef enum {\n')
        for c in counters.block:
            write('   {}_ERROR_{},\n'
                  .format(csetname.upper(), c['name'].upper()))
        write('   {}_N_ERROR\n'.format(csetname.upper()))
        write('}} vl_counter_{}_enum_t;\n'.format(csetname))

        write('extern vlib_error_desc_t {}_error_counters[];\n'.format(csetname))


def generate_include_types(s, module, stream):
    '''Generate separate API _types file.'''
    write = stream.write

    write('#ifndef included_{module}_api_types_h\n'.format(module=module))
    write('#define included_{module}_api_types_h\n'.format(module=module))

    if 'version' in s['Option']:
        v = s['Option']['version']
        (major, minor, patch) = v.split('.')
        write('#define VL_API_{m}_API_VERSION_MAJOR {v}\n'
              .format(m=module.upper(), v=major))
        write('#define VL_API_{m}_API_VERSION_MINOR {v}\n'
              .format(m=module.upper(), v=minor))
        write('#define VL_API_{m}_API_VERSION_PATCH {v}\n'
              .format(m=module.upper(), v=patch))

    if 'Import' in s:
        write('/* Imported API files */\n')
        for i in s['Import']:
            filename = i.filename.replace('plugins/', '')
            write('#include <{}_types.h>\n'.format(filename))

    for o in s['types'] + s['Define']:
        tname = o.__class__.__name__
        if tname == 'Using':
            if 'length' in o.alias:
                write('typedef %s vl_api_%s_t[%s];\n' %
                      (o.alias['type'], o.name, o.alias['length']))
            else:
                write('typedef %s vl_api_%s_t;\n' % (o.alias['type'], o.name))
        elif tname == 'Enum' or tname == 'EnumFlag':
            if o.enumtype == 'u32':
                write("typedef enum {\n")
            else:
                write("typedef enum __attribute__((packed)) {\n")

            for b in o.block:
                write("    %s = %s,\n" % (b[0], b[1]))
            write('} vl_api_%s_t;\n' % o.name)
            if o.enumtype != 'u32':
                size1 = 'sizeof(vl_api_%s_t)' % o.name
                size2 = 'sizeof(%s)' % o.enumtype
                err_str = 'size of API enum %s is wrong' % o.name
                write('STATIC_ASSERT(%s == %s, "%s");\n'
                      % (size1, size2, err_str))
        else:
            if tname == 'Union':
                write("typedef union __attribute__ ((packed)) _vl_api_%s {\n"
                      % o.name)
            else:
                write(("typedef struct __attribute__ ((packed)) _vl_api_%s {\n")
                      % o.name)
            for b in o.block:
                if b.type == 'Option':
                    continue
                if b.type == 'Field':
                    write("    %s %s;\n" % (api2c(b.fieldtype),
                                            b.fieldname))
                elif b.type == 'Array':
                    if b.lengthfield:
                        write("    %s %s[0];\n" % (api2c(b.fieldtype),
                                                   b.fieldname))
                    else:
                        # Fixed length strings decay to nul terminated u8
                        if b.fieldtype == 'string':
                            if b.modern_vla:
                                write('    {} {};\n'
                                      .format(api2c(b.fieldtype),
                                              b.fieldname))
                            else:
                                write('    u8 {}[{}];\n'
                                      .format(b.fieldname, b.length))
                        else:
                            write("    %s %s[%s];\n" %
                                  (api2c(b.fieldtype), b.fieldname,
                                   b.length))
                else:
                    raise ValueError("Error in processing type {} for {}"
                                     .format(b, o.name))

            write('} vl_api_%s_t;\n' % o.name)

    for t in s['Define']:
        write('#define VL_API_{ID}_CRC "{n}_{crc:08x}"\n'
              .format(n=t.name, ID=t.name.upper(), crc=t.crc))

    write("\n#endif\n")


def generate_c_boilerplate(services, defines, counters, file_crc,
                           module, stream):
    '''VPP side plugin.'''
    write = stream.write
    define_hash = {d.name: d for d in defines}

    hdr = '''\
#define vl_endianfun		/* define message structures */
#include "{module}.api.h"
#undef vl_endianfun

/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
#include "{module}.api.h"
#undef vl_printfun

'''

    write(hdr.format(module=module))
    write('static u16\n')
    write('setup_message_id_table (void) {\n')
    write('   api_main_t *am = my_api_main;\n')
    write('   vl_msg_api_msg_config_t c;\n')
    write('   u16 msg_id_base = vl_msg_api_get_msg_ids ("{}_{crc:08x}", '
          'VL_MSG_{m}_LAST);\n'
          .format(module, crc=file_crc, m=module.upper()))

    for d in defines:
        write('   vl_msg_api_add_msg_name_crc (am, "{n}_{crc:08x}",\n'
              '                                VL_API_{ID} + msg_id_base);\n'
              .format(n=d.name, ID=d.name.upper(), crc=d.crc))
    for s in services:
        d = define_hash[s.caller]
        write('   c = (vl_msg_api_msg_config_t) '
              ' {{.id = VL_API_{ID} + msg_id_base,\n'
              '   .name = "{n}",\n'
              '   .handler = vl_api_{n}_t_handler,\n'
              '   .cleanup = vl_noop_handler,\n'
              '   .endian = vl_api_{n}_t_endian,\n'
              '   .print = vl_api_{n}_t_print,\n'
              '   .traced = 1,\n'
              '   .replay = 1,\n'
              '   .is_autoendian = {auto}}};\n'
              .format(n=s.caller, ID=s.caller.upper(),
                      auto=d.autoendian))
        write('   vl_msg_api_config (&c);\n')
        try:
            d = define_hash[s.reply]
            write('   c = (vl_msg_api_msg_config_t) '
                  '{{.id = VL_API_{ID} + msg_id_base,\n'
                  '  .name = "{n}",\n'
                  '  .handler = 0,\n'
                  '  .cleanup = vl_noop_handler,\n'
                  '  .endian = vl_api_{n}_t_endian,\n'
                  '  .print = vl_api_{n}_t_print,\n'
                  '  .is_autoendian = {auto}}};\n'
                  .format(n=s.reply, ID=s.reply.upper(),
                          auto=d.autoendian))
            write('   vl_msg_api_config (&c);\n')
        except KeyError:
            pass

    write('   return msg_id_base;\n')
    write('}\n')

    severity = {'error': 'VL_COUNTER_SEVERITY_ERROR',
                'info': 'VL_COUNTER_SEVERITY_INFO',
                'warn': 'VL_COUNTER_SEVERITY_WARN'}

    for cnt in counters:
        csetname = cnt.name
        write('vlib_error_desc_t {}_error_counters[] = {{\n'.format(csetname))
        for c in cnt.block:
            write('  {\n')
            write('   .name = "{}",\n'.format(c['name']))
            write('   .desc = "{}",\n'.format(c['description']))
            write('   .severity = {},\n'.format(severity[c['severity']]))
            write('  },\n')
        write('};\n')


def generate_c_test_boilerplate(services, defines, file_crc, module, plugin,
                                stream):
    '''Generate code for legacy style VAT. To be deleted.'''
    write = stream.write

    define_hash = {d.name: d for d in defines}

    hdr = '''\
#define vl_endianfun            /* define message structures */
#include "{module}.api.h"
#undef vl_endianfun

/* instantiate all the print functions we know about */
#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
#include "{module}.api.h"
#undef vl_printfun

'''

    write(hdr.format(module=module))
    for s in services:
        try:
            d = define_hash[s.reply]
        except KeyError:
            continue
        if d.manual_print:
            write('/*\n'
                  ' * Manual definition requested for: \n'
                  ' * vl_api_{n}_t_handler()\n'
                  ' */\n'
                  .format(n=s.reply))
            continue
        if not define_hash[s.caller].autoreply:
            write('/* Generation not supported (vl_api_{n}_t_handler()) */\n'
                  .format(n=s.reply))
            continue
        write('#ifndef VL_API_{n}_T_HANDLER\n'.format(n=s.reply.upper()))
        write('static void\n')
        write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'
              .format(n=s.reply))
        write('   vat_main_t * vam = {}_test_main.vat_main;\n'.format(module))
        write('   i32 retval = ntohl(mp->retval);\n')
        write('   if (vam->async_mode) {\n')
        write('      vam->async_errors += (retval < 0);\n')
        write('   } else {\n')
        write('      vam->retval = retval;\n')
        write('      vam->result_ready = 1;\n')
        write('   }\n')
        write('}\n')
        write('#endif\n')

        for e in s.events:
            if define_hash[e].manual_print:
                continue
            write('static void\n')
            write('vl_api_{n}_t_handler (vl_api_{n}_t * mp) {{\n'.format(n=e))
            write('    vl_print(0, "{n} event called:");\n'.format(n=e))
            write('    vl_api_{n}_t_print(mp, 0);\n'.format(n=e))
            write('}\n')

    write('static void\n')
    write('setup_message_id_table (vat_main_t * vam, u16 msg_id_base) {\n')
    for s in services:
        write('   vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
              '                           "{n}",\n'
              '                           vl_api_{n}_t_handler, '
              '                           vl_noop_handler,\n'
              '                           vl_api_{n}_t_endian, '
              '                           vl_api_{n}_t_print,\n'
              '                           sizeof(vl_api_{n}_t), 1);\n'
              .format(n=s.reply, ID=s.reply.upper()))
        write('   hash_set_mem (vam->function_by_name, "{n}", api_{n});\n'
              .format(n=s.caller))
        try:
            write('   hash_set_mem (vam->help_by_name, "{n}", "{help}");\n'
                  .format(n=s.caller,
                          help=define_hash[s.caller].options['vat_help']))
        except KeyError:
            pass

        # Events
        for e in s.events:
            write('   vl_msg_api_set_handlers(VL_API_{ID} + msg_id_base, '
                  '                          "{n}",\n'
                  '                           vl_api_{n}_t_handler, '
                  '                           vl_noop_handler,\n'
                  '                           vl_api_{n}_t_endian, '
                  '                           vl_api_{n}_t_print,\n'
                  '                           sizeof(vl_api_{n}_t), 1);\n'
                  .format(n=e, ID=e.upper()))

    write('}\n')
    if plugin:
        write('clib_error_t * vat_plugin_register (vat_main_t *vam)\n')
    else:
        write('clib_error_t * vat_{}_plugin_register (vat_main_t *vam)\n'
              .format(module))
    write('{\n')
    write('   {n}_test_main_t * mainp = &{n}_test_main;\n'.format(n=module))
    write('   mainp->vat_main = vam;\n')
    write('   mainp->msg_id_base = vl_client_get_first_plugin_msg_id '
          '                       ("{n}_{crc:08x}");\n'
          .format(n=module, crc=file_crc))
    write('   if (mainp->msg_id_base == (u16) ~0)\n')
    write('      return clib_error_return (0, "{} plugin not loaded...");\n'
          .format(module))
    write('   setup_message_id_table (vam, mainp->msg_id_base);\n')
    write('#ifdef VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE\n')
    write('    VL_API_LOCAL_SETUP_MESSAGE_ID_TABLE(vam);\n')
    write('#endif\n')
    write('   return 0;\n')
    write('}\n')


def apifunc(func):
    '''Check if a method is generated already.'''
    def _f(module, d, processed, *args):
        if d.name in processed:
            return None
        processed[d.name] = True
        return func(module, d, *args)
    return _f


def c_test_api_service(s, dump, stream):
    '''Generate JSON code for a service.'''
    write = stream.write

    req_reply_template = '''\
static cJSON *
api_{n} (cJSON *o)
{{
  vl_api_{n}_t *mp;
  int len;
  if (!o) return 0;
  mp = vl_api_{n}_t_fromjson(o, &len);
  if (!mp) {{
    fprintf(stderr, "Failed converting JSON to API\\n");
    return 0;
  }}

  mp->_vl_msg_id = vac_get_msg_index(VL_API_{N}_CRC);
  vl_api_{n}_t_endian(mp);
  vac_write((char *)mp, len);
  free(mp);

  /* Read reply */
  char *p;
  int l;
  vac_read(&p, &l, 5); // XXX: Fix timeout
  if (p == 0 || l == 0) return 0;
    // XXX Will fail in case of event received. Do loop
  if (ntohs(*((u16 *)p)) != vac_get_msg_index(VL_API_{R}_CRC)) {{
    fprintf(stderr, "Mismatched reply\\n");
    return 0;
  }}
  vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
  vl_api_{r}_t_endian(rmp);
  return vl_api_{r}_t_tojson(rmp);
}}

'''
    dump_details_template = '''\
static cJSON *
api_{n} (cJSON *o)
{{
  u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
  int len;
  if (!o) return 0;
  vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
  if (!mp) {{
      fprintf(stderr, "Failed converting JSON to API\\n");
      return 0;
  }}
  mp->_vl_msg_id = msg_id;
  vl_api_{n}_t_endian(mp);
  vac_write((char *)mp, len);
  free(mp);

  vat2_control_ping(123); // FIX CONTEXT
  cJSON *reply = cJSON_CreateArray();

  u16 ping_reply_msg_id = vac_get_msg_index(VL_API_CONTROL_PING_REPLY_CRC);
  u16 details_msg_id = vac_get_msg_index(VL_API_{R}_CRC);

  while (1) {{
    /* Read reply */
    char *p;
    int l;
    vac_read(&p, &l, 5); // XXX: Fix timeout
    if (p == 0 || l == 0) {{
      cJSON_free(reply);
      return 0;
    }}

    /* Message can be one of [_details, control_ping_reply
     * or unrelated event]
     */
    u16 reply_msg_id = ntohs(*((u16 *)p));
    if (reply_msg_id == ping_reply_msg_id) {{
        break;
    }}

    if (reply_msg_id == details_msg_id) {{
        if (l < sizeof(vl_api_{r}_t)) {{
            cJSON_free(reply);
            return 0;
        }}
        vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
        vl_api_{r}_t_endian(rmp);
        cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
    }}
  }}
  return reply;
}}

'''
    gets_details_reply_template = '''\
static cJSON *
api_{n} (cJSON *o)
{{
    u16 msg_id = vac_get_msg_index(VL_API_{N}_CRC);
  int len = 0;
  if (!o) return 0;
  vl_api_{n}_t *mp = vl_api_{n}_t_fromjson(o, &len);
  if (!mp) {{
    fprintf(stderr, "Failed converting JSON to API\\n");
    return 0;
  }}
  mp->_vl_msg_id = msg_id;

  vl_api_{n}_t_endian(mp);
  vac_write((char *)mp, len);
  free(mp);

  cJSON *reply = cJSON_CreateArray();

  u16 reply_msg_id = vac_get_msg_index(VL_API_{R}_CRC);
  u16 details_msg_id = vac_get_msg_index(VL_API_{D}_CRC);

  while (1) {{
    /* Read reply */
    char *p;
    int l;
    vac_read(&p, &l, 5); // XXX: Fix timeout

    /* Message can be one of [_details, control_ping_reply
     * or unrelated event]
     */
    u16 msg_id = ntohs(*((u16 *)p));
    if (msg_id == reply_msg_id) {{
        vl_api_{r}_t *rmp = (vl_api_{r}_t *)p;
        vl_api_{r}_t_endian(rmp);
        cJSON_AddItemToArray(reply, vl_api_{r}_t_tojson(rmp));
        break;
    }}

    if (msg_id == details_msg_id) {{
        vl_api_{d}_t *rmp = (vl_api_{d}_t *)p;
        vl_api_{d}_t_endian(rmp);
        cJSON_AddItemToArray(reply, vl_api_{d}_t_tojson(rmp));
    }}
  }}
  return reply;
}}

'''

    if dump:
        if s.stream_message:
            write(gets_details_reply_template
                  .format(n=s.caller, r=s.reply, N=s.caller.upper(),
                          R=s.reply.upper(), d=s.stream_message,
                          D=s.stream_message.upper()))
        else:
            write(dump_details_template.format(n=s.caller, r=s.reply,
                                               N=s.caller.upper(),
                                               R=s.reply.upper()))
    else:
        write(req_reply_template.format(n=s.caller, r=s.reply,
                                        N=s.caller.upper(),
                                        R=s.reply.upper()))


def generate_c_test2_boilerplate(services, defines, module, stream):
    '''Generate code for VAT2 plugin.'''
    write = stream.write

    define_hash = {d.name: d for d in defines}
    # replies = {}

    hdr = '''\
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#include <vppinfra/error.h>
#include <vnet/ip/ip_format_fns.h>
#include <vnet/ethernet/ethernet_format_fns.h>

#define vl_typedefs             /* define message structures */
#include <vlibmemory/vl_memory_api_h.h>
#include <vpp/api/vpe_types.api.h>
#include <vpp/api/vpe.api.h>
#undef vl_typedefs

#include "{module}.api_enum.h"
#include "{module}.api_types.h"

#define vl_endianfun		/* define message structures */
#include "{module}.api.h"
#undef vl_endianfun

#define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
#define vl_printfun
#include "{module}.api.h"
#undef vl_printfun

#include "{module}.api_tojson.h"
#include "{module}.api_fromjson.h"
#include <vpp-api/client/vppapiclient.h>

#include <vat2/vat2_helpers.h>

'''

    write(hdr.format(module=module))

    for s in services:
        if s.reply not in define_hash:
            continue
        c_test_api_service(s, s.stream, stream)

    write('void vat2_register_function(char *, cJSON * (*)(cJSON *), cJSON * (*)(void *));\n')
    # write('__attribute__((constructor))')
    write('clib_error_t *\n')
    write('vat2_register_plugin (void) {\n')
    for s in services:
        write('   vat2_register_function("{n}", api_{n}, (cJSON * (*)(void *))vl_api_{n}_t_tojson);\n'
              .format(n=s.caller))
    write('   return 0;\n')
    write('}\n')


#
# Plugin entry point
#
def run(args, apifilename, s):
    '''Main plugin entry point.'''
    stream = StringIO()

    if not args.outputdir:
        sys.stderr.write('Missing --outputdir argument')
        return None

    basename = os.path.basename(apifilename)
    filename, _ = os.path.splitext(basename)
    modulename = filename.replace('.', '_')
    filename_enum = os.path.join(args.outputdir + '/' + basename + '_enum.h')
    filename_types = os.path.join(args.outputdir + '/' + basename + '_types.h')
    filename_c = os.path.join(args.outputdir + '/' + basename + '.c')
    filename_c_test = os.path.join(args.outputdir + '/' + basename + '_test.c')
    filename_c_test2 = (os.path.join(args.outputdir + '/' + basename +
                                     '_test2.c'))
    filename_c_tojson = (os.path.join(args.outputdir +
                                      '/' + basename + '_tojson.h'))
    filename_c_fromjson = (os.path.join(args.outputdir + '/' +
                                        basename + '_fromjson.h'))

    # Generate separate types file
    st = StringIO()
    generate_include_types(s, modulename, st)
    with open(filename_types, 'w') as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    # Generate separate enum file
    st = StringIO()
    st.write('#ifndef included_{}_api_enum_h\n'.format(modulename))
    st.write('#define included_{}_api_enum_h\n'.format(modulename))
    generate_include_enum(s, modulename, st)
    generate_include_counters(s['Counters'], st)
    st.write('#endif\n')
    with open(filename_enum, 'w') as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    # Generate separate C file
    st = StringIO()
    generate_c_boilerplate(s['Service'], s['Define'], s['Counters'],
                           s['file_crc'], modulename, st)
    with open(filename_c, 'w') as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    # Generate separate C test file
    st = StringIO()
    plugin = bool('plugin' in apifilename)
    generate_c_test_boilerplate(s['Service'], s['Define'],
                                s['file_crc'],
                                modulename, plugin, st)
    with open(filename_c_test, 'w') as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    # Fully autogenerated VATv2 C test file
    st = StringIO()
    generate_c_test2_boilerplate(s['Service'], s['Define'],
                                 modulename, st)
    with open(filename_c_test2, 'w') as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()                  #

    # Generate separate JSON file
    st = StringIO()
    generate_tojson(s, modulename, st)
    with open(filename_c_tojson, 'w') as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()
    st = StringIO()
    generate_fromjson(s, modulename, st)
    with open(filename_c_fromjson, 'w') as fd:
        st.seek(0)
        shutil.copyfileobj(st, fd)
    st.close()

    output = TOP_BOILERPLATE.format(datestring=DATESTRING,
                                    input_filename=basename)
    output += generate_imports(s['Import'])
    output += msg_ids(s)
    output += msg_names(s)
    output += msg_name_crc_list(s, filename)
    output += typedefs(modulename)
    printfun_types(s['types'], stream, modulename)
    printfun(s['Define'], stream, modulename)
    output += stream.getvalue()
    stream.close()
    output += endianfun(s['types'] + s['Define'], modulename)
    output += version_tuple(s, basename)
    output += BOTTOM_BOILERPLATE.format(input_filename=basename,
                                        file_crc=s['file_crc'])

    return output