networkop / yang-to-cue

Importing YANG definitions into CUE
9 stars 2 forks source link

Working with YANG-based APIs from CUE

  1. Initialise a Go module and add the ygot package
go mod init yang.to.cue
go get github.com/openconfig/ygot/generator
  1. Get a local copy of the required YANG models
git clone --depth=1 https://github.com/YangModels/yang.git
git clone https://github.com/openconfig/public openconfig
curl -o yang/arista.yang https://raw.githubusercontent.com/aristanetworks/yang/master/EOS-4.25.1F/release/openconfig/models/interfaces/arista-intf-augments.yang
  1. Generate types with ygot
mkdir pkg

go run github.com/openconfig/ygot/generator \
-path=openconfig \
-generate_fakeroot \
-fakeroot_name=device \
-output_file=pkg/yang.go \
-compress_paths=false \
-exclude_modules=ietf-interfaces,openconfig-mpls,openconfig-isis,openconfig-evpn \
-include_descriptions=false \
-include_model_data=false \
-package_name=yang \
-exclude_state \
openconfig/release/models/interfaces/openconfig-if-ip.yang \
openconfig/release/models/bgp/openconfig-bgp.yang \
openconfig/release/models/network-instance/openconfig-network-instance.yang \
yang/arista.yang
  1. (Optional) Generate a JSON payload using ygot
go run main.go

This generates the ./go.json file

  1. Modify YGOT types for CUE
# replace 'path' tags with 'json'
sed -i -E 's/path:"(\S+)"/json:"\1"/' pkg/yang.go
# replace Go maps with slices
sed -i -E 's/map\[.*\]\*(\S+)/\[\]\*\1/' pkg/yang.go
  1. Init CUE modules
cue mod init yang.cue
  1. Import Go types into CUE
cue get go yang.to.cue/pkg/...

The generated file is located in cue.mod/gen/yang.to.cue/pkg/yang_go_gen.cue

  1. Modify CUE definitions
# regenerate ygot package
go run github.com/openconfig/ygot/generator \
-path=openconfig \
-generate_fakeroot \
-fakeroot_name=device \
-output_file=pkg/yang.go \
-compress_paths=false \
-exclude_modules=ietf-interfaces,openconfig-mpls,openconfig-isis,openconfig-evpn \
-include_descriptions=false \
-include_model_data=false \
-package_name=yang \
-exclude_state \
openconfig/release/models/interfaces/openconfig-if-ip.yang \
openconfig/release/models/bgp/openconfig-bgp.yang \
openconfig/release/models/network-instance/openconfig-network-instance.yang \
yang/arista.yang

go run post-import.go

From here on, you work exclusively with CUE definitions and can safely delete ./openconfig and ./yang directories with YANG models and ./pkg containing ygot types.

  1. Write some CUE based on the generated definitions
cat values.cue
  1. Check the that values are correct by evaluating them
cue eval values.cue --out=json
  1. Check the that validation works against an incorrect data
# mismatch betweek YangList.key and YangList.Config.Key
cue eval bad/mismatch.cue --out=json

# duplicate entry in YangList
cue eval bad/duplicate.cue --out=json

# misspelled/incorrect entry
cue eval bad/undefined.cue --out=json
  1. Check and update the test device details
cat gnmi_tool.cue
  1. Apply the configuration
cue apply