PageRenderTime 28ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/scripts/exampleCompactYang.groovy

https://bitbucket.org/novakmi/yangbuilder
Groovy | 89 lines | 40 code | 14 blank | 35 comment | 0 complexity | 999ceee0deb77923bc4648f85c847d41 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. import org.bitbucket.novakmi.yangbuilder.CompactYangPlugin
  5. //If you have Internet connection, use groovy Grab to get dependencies (may take some time for the first time to download jars)
  6. //Run as ordinary groovy script with command 'groovy <scriptName>.groovy' (or as Linux executable script './<scriptName>.groovy')
  7. //Update nodebuilder, yangbuilder version numbers as needed
  8. //adoc-begin - documentation purpose comment
  9. @Grab(group = 'org.bitbucket.novakmi', module = 'nodebuilder', version = '1.1.1')
  10. @Grab(group = 'org.bitbucket.novakmi', module = 'yangbuilder', version = '1.3.0')
  11. // This script template represents example of usage with Compact yang plugin
  12. // (syntax is more different from yang, but more compact)
  13. // create new builder, pass plugin or list of plugins as constructor parameter,
  14. // default indent of 2 has to be specified if we pass plugin (or list of plugins) in constructor
  15. // (other option is to use builder.registerPlugin(new CompactYangPlugin()))
  16. def builder = new org.bitbucket.novakmi.yangbuilder.YangBuilder(2, new CompactYangPlugin()) //<1>
  17. //name of file to generate
  18. moduleName = "example-compact-module" // do not use 'def' for script global variable
  19. //this is example how function can be used by the builder, parameters can be used
  20. def makeAddressPort(builder, kind = null) {
  21. // in function all nodes have to be prefixed with 'builder.', except for child nodes
  22. builder.yngbuild("// IPv4 or IPv6 address", indent: true)
  23. //output depends on parameters, not possible in yang
  24. builder.leaf("${kind ? kind + '-' : ''}address", type: 'inet:ip-address', description: "IPv4 or IPv6 address")
  25. builder.yngbuild("// IP port", indent: true)
  26. //with compact plugin 'type' can be declared as param. of leaf. 'description' as param of element //<2>
  27. builder.leaf("${kind ? kind + '-' : ''}port", type: 'uint16', description: "IP port")
  28. }
  29. //module's prefix and namespace as attributes '_nl' means new line
  30. builder.module(moduleName, pnl_namespace:'http://bitbucket.org/novakmi/yangbuilder', prefix_nl: 'example') { //<3>
  31. def makeGrouping = { // this is example how closure can be called be used by the builder
  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. // 'nl:true adds new line (you can also use nl:1, etc.) //<4>
  39. yngbuild(' see http://bitbucket.org/novakmi/yangbuilder */', indent: true, nl: true)
  40. //with compact yang 'prefix' can be added as attribute of import,
  41. // nl:1 means add new line after ending '}' of import
  42. 'import'('ietf-inet-types', prefix: 'inet', nl: 1) //<5>
  43. makeGrouping() // this behaves in the same way as if content of the closure is written here
  44. yngbuild('')
  45. yngbuild("/* neighbor */", indent: true)
  46. container('neighbor', pnl: 1, nl: 1) { // pnl - prefix new line - new line before node processing //<6>
  47. uses 'addressPort' // yang grouping reuse
  48. }
  49. ['bgp', 'ospf', 'isis', 'rip'].each {k -> // create 4 containers in loop, not possible in yang
  50. yngbuild("/* ${k} neighbor */", indent: true)
  51. container("${k}-neighbor", description: "${k}-neighbor container", nl: 1) {
  52. // as if content of function is written here, yangbuilder reuse (not possible in yang)
  53. makeAddressPort(builder, k)
  54. }
  55. }
  56. // key leaf can be attribute of list statement
  57. list('neighbors', key: 'neighbor', description: 'List of neighbors') { //<7>
  58. leaf('neighbor', type: 'inet:ip-address', description: 'neighbor IP4 or IPv6 address')
  59. leaf('name', type: 'string', description: 'neighbor name')
  60. }
  61. }
  62. builder.writeToFile("${builder.getYangName()}.yang")
  63. /* adoc-callout - documentation purpose comment
  64. <1> The plugin is registered with the +builder+ in constructor or with method +registerPlugin+. When registered
  65. in constructor, it is passed as second attribute after +indent level+ value. It is possible to pass list of
  66. plugins to register several plugins at once.
  67. <2> +type+ and +description+ can be added as +leaf+ attribute
  68. <3> +namespace+ and +prefix+ can be added as +module+ attribute.
  69. +pnl_namespace+ means +\n+ before a +namespace+ element,
  70. +prefix_nl+ means +\n+ after a +prefix+ element
  71. <4> if +nl+ attribute evaluates to +true+, it means +\n+ after element is added (no need for +yngbuild('')+)
  72. <5> +nl+ can be shortened/replaced with +prefix_nl+, e.g. +'import'('ietf-inet-types', prefix_nl: 'inet')+
  73. <6> +nl+ attribute evaluating to +true+ means +\n+ before element (no need for +yngbuild('')+)
  74. <7> +key+ and +description+ can be added as +list+ attributes
  75. adoc-end - documentation purpose comment*/