Skip to content

Instantly share code, notes, and snippets.

@lamehost
Last active February 3, 2022 12:45
Show Gist options
  • Save lamehost/59ba5bf61c5ba47efb3a89ede105dc43 to your computer and use it in GitHub Desktop.
Save lamehost/59ba5bf61c5ba47efb3a89ede105dc43 to your computer and use it in GitHub Desktop.
Generates python3 protobuffer modules for the OpenConfig YANG files for Cisco IOS-XR
#!/bin/bash
# For this to work you need the following packages installed on your computer:
# - protobuf-compiler
# - git
# - go
# - python3
BASEDIR=$(pwd)
OUTDIR=$1
if [["x$OUTDIR" == "x" ]]; then
OUTDIR=/yang/proto
fi
OUTDIR=$BASEDIR/$OUTDIR
VERSION=$2
if [[ "x$VERSION" == "x" ]]; then
VERSION="*"
fi
mkdir -p $OUTDIR
touch $OUTDIR/__init__.py
echo
echo "Downlaoding YANG models..."
if [[ ! -d yang ]]; then
git clone https://github.com/YangModels/yang
else
echo "The 'yang' directory is already in current path."
fi
echo
echo "Installing ygot..."
if [[ ! -d $GOPATH/pkg/mod/github.com/openconfig/ygot@v0.14.0 ]]; then
go get -d github.com/openconfig/ygot@v0.14.0
CURRENTDIR="$(pwd)"
cd $GOPATH/pkg/mod/github.com/openconfig/ygot@v0.14.0
go get -t -d ./...
cd $BASEDIR
else
echo "The 'github.com/openconfig/ygot' module is already in $GOPATH/src/"
fi
echo
echo -n "Generating openconfig proto files for XR..."
cd $GOPATH/pkg/mod/github.com/openconfig/ygot@v0.14.0
for DIR in $BASEDIR/yang/vendor/cisco/xr/$VERSION; do
VERSION=$(basename $DIR)
echo
if [[ "x$(ls $DIR/openconfig-*.yang 2> /dev/null)" = "x" ]] ; then
echo " No openconfig files found for version $VERSION"
continue
else
echo " Processing openconfig files for version $VERSION..."
fi
mkdir -p $OUTDIR/xr/$VERSION/{yext,ywrapper}
touch $OUTDIR/xr/$VERSION/{yext,ywrapper}/__init__.py
chmod -R u+w $OUTDIR/xr/$VERSION/
cp -r ./proto/yext/yext.proto $OUTDIR/xr/$VERSION/yext
cp -r ./proto/ywrapper/ywrapper.proto $OUTDIR/xr/$VERSION/ywrapper
for FILE in $DIR/openconfig-*.yang; do
echo -n " Generating .proto file for $(basename $FILE): "
go run $GOPATH/pkg/mod/github.com/openconfig/ygot@v0.14.0/proto_generator/protogenerator.go \
-path=$DIR \
-output_dir=$OUTDIR/xr/$VERSION \
-enum_package_name=enums \
-yext_path=yext \
-ywrapper_path=ywrapper \
$FILE 2> /dev/null
if [[ $? == 0 ]]; then
echo "done"
else
echo "failed"
fi
done
echo
for FILE in $(find $OUTDIR/xr/$VERSION -name *.proto); do
echo -n " Generating .py files for $(basename $FILE): "
PROTOFILE="${FILE%.*}".proto
protoc \
-I $OUTDIR/xr/$VERSION \
--python_out=$OUTDIR/xr/$VERSION \
$FILE 2> /dev/null
if [[ $? == 0 ]]; then
echo "done"
else
echo "failed"
fi
done
for SUBDIR in $(find $OUTDIR/xr/$VERSION -type d); do
touch $SUBDIR/__init__.py
done
done
cd $BASEDIR
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment