PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/scripts/exampleYang.groovy

https://bitbucket.org/novakmi/yangbuilder
Groovy | 116 lines | 78 code | 16 blank | 22 comment | 4 complexity | 44170ff2b447ed0f285ef84c6515c13b MD5 | raw file
Possible License(s): MIT
  1. #!/usr/bin/env groovy
  2. //This is free software licensed under MIT License, see LICENSE file
  3. //(https://bitbucket.org/novakmi/yangbuilder/src/LICENSE)
  4. //If you have Internet connection, use groovy Grab to get dependencies (may take some time for the first time to download jars)
  5. //Run as ordinary groovy script with command 'groovy <scriptName>.groovy' (or as Linux executable script './<scriptName>.groovy')
  6. //Update nodebuilder, yangbuilder version numbers as needed
  7. //adoc-begin - documentation purpose comment
  8. @Grab(group = 'org.bitbucket.novakmi', module = 'nodebuilder', version = '1.1.1')
  9. @Grab(group = 'org.bitbucket.novakmi', module = 'yangbuilder', version = '1.3.0')
  10. // This script template represents example of usage without any plugin
  11. def builder = new org.bitbucket.novakmi.yangbuilder.YangBuilder(4) // create new builder, indent 4 (default is 2)
  12. builder.declareAlias('import_', 'import') //optional alias declaration //<1>
  13. //name of file to generate
  14. moduleName = "example-module" // do not use 'def' for script global variable
  15. def makeAddressPort(builder, kind = null) { //function with parameters can be used by the builder //<2>
  16. // in function all nodes have to be prefixed with 'builder.', except for child nodes
  17. builder.yngbuild("// IPv4 or IPv6 address", indent: true)
  18. builder.leaf("${kind ? kind + '-' : ''}address") { //output depends on parameters, not possible in yang
  19. type('inet:ip-address')
  20. }
  21. builder.yngbuild("// IP port", indent: true)
  22. builder.leaf("${kind ? kind + '-' : ''}port") {
  23. type('uint16')
  24. }
  25. }
  26. builder.yangroot { //<3>
  27. yngbuild('//adocy-begin - documentation purpose comment')//#$# for asciidoc
  28. yngbuild('// *<*1*>*') //#$# for asciidoc
  29. module(moduleName) {
  30. def makeGrouping = { // this is example how closure can be used by the builder //<4>
  31. yngbuild('// *<*2*>*') //#$# for asciidoc
  32. grouping('addressPort') {
  33. makeAddressPort(builder)
  34. }
  35. }
  36. yngbuild("/* This yang file was generated with groovy YangBuilder on ${new Date().toString()}",
  37. indent: true)
  38. yngbuild(' see http://bitbucket.org/novakmi/yangbuilder */', indent: true) //<5>
  39. yngbuild('') // new line
  40. namespace "http://bitbucket.org/novakmi/yangbuilder"
  41. prefix "example"
  42. yngbuild('')
  43. // 'import' alias is declared, it can be used instead of 'import' - workaround for clashing keyword //<6>
  44. import_('ietf-inet-types') {
  45. prefix 'inet'
  46. }
  47. yngbuild('')
  48. makeGrouping() // this behaves in the same way as if content of the closure is written here //<7>
  49. yngbuild('')
  50. yngbuild("/* neighbor */", indent: true)
  51. container('neighbor') {
  52. uses 'addressPort' // yang grouping reuse
  53. }
  54. yngbuild('')
  55. ['bgp', 'ospf', 'isis', 'rip'].each {k -> // create 3 containers in loop, not possible in yang //<8>
  56. if (k == 'bgp') {
  57. yngbuild('// *<*3*>*') //#$# for asciidoc
  58. }
  59. yngbuild("/* ${k} neighbor */", indent: true)
  60. container("${k}-neighbor") {
  61. // as if content of the function is written here, reuse (not possible in yang) //<9>
  62. if (k == 'bgp') {
  63. yngbuild('// *<*4*>*') //#$# for asciidoc
  64. }
  65. makeAddressPort(builder, k)
  66. }
  67. yngbuild('')
  68. }
  69. list('neighbors') {
  70. description('List of neighbors')
  71. key('neighbor')
  72. leaf('neighbor') {
  73. description "neighbor IP4 or IPv6 address"
  74. type 'inet:ip-address'
  75. }
  76. leaf('name') {
  77. description "neighbor name"
  78. type 'string'
  79. }
  80. }
  81. }
  82. yngbuild('//adocy-callout - documentation purpose comment') //#$# for asciidoc
  83. yngbuild('*<*1*>* indent 4') //#$# for asciidoc
  84. yngbuild('*<*2*>* call to +makeGrouping+') //#$# for asciidoc
  85. yngbuild('*<*3*>* iteration') //#$# for asciidoc
  86. yngbuild('*<*4*>* expanded +makeAddressPort+') //#$# for asciidoc
  87. yngbuild('//adocy-end - documentation purpose comment') //#$# for asciidoc
  88. }
  89. builder.writeToFile("${builder.getYangName()}.yang")
  90. /* adoc-callout - documentation purpose comment
  91. <1> declaration of alias keywords (e.g. for keywords clashing with groovy keywords)
  92. <2> function
  93. <3> +yngroot+ is not needed, +builder.module(moduleName)+ can be directly used (it is here for documentation)
  94. <4> closure
  95. <5> closure called
  96. <6> alias is used
  97. <7> indentation
  98. <8> iteration
  99. <9> function called
  100. adoc-end - documentation purpose comment*/