From: demx8as6 Date: Sun, 12 Apr 2020 14:37:43 +0000 (+0200) Subject: Yang Linter in bash X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?p=scp%2Foam%2Fmodeling.git;a=commitdiff_plain;h=deb96f16429ed7625a6a3cef48a856389faab97a Yang Linter in bash An init version of a yang linter using bash. IssueID: OAM-101 Change-Id: I91dcfe9ca84aff5222cf841a54fb25fd529a561c Signed-off-by: demx8as6 --- diff --git a/.gitignore b/.gitignore index e1e63c6..9455bca 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,5 @@ etc/ node_modules/ .fbExcludeFilterFile *.md.html +_3GPP + diff --git a/data-model/tools/yang-linter/README.md b/data-model/tools/yang-linter/README.md new file mode 100644 index 0000000..c2009cc --- /dev/null +++ b/data-model/tools/yang-linter/README.md @@ -0,0 +1,45 @@ +# YANG linter + +As much as possible automatic yang linter + +## Usage + +``` +./src/main/bash/yang-linter [options] +``` + +### Example + +``` +./src/main/bash/yang-linter --3gpp +``` + +## Options + +``` + -3, --3gpp Clone and lint 3GPP SA5 yang modules and exit + -h, --help Show this help message and exit + + -i=IMPORTDIR, --imports=IMPORTDiR + Defines an import directory for standard yang modules + + -s, --status Show app status information and exit + + -t=TARGETDIR, --target=TARGETDIR + Defines the target output directory + + -v, --version Show version number and exit + + The folder containng yang modules +``` + +## Open Items + +Ideas for additional pyang plugins: + +- pyang --format yang --yang-contact +- pyang --format yang --yang-organization +- pyang --format yang --yang-reference +- pyang --format yang --yang-revision-order +- pyang --format yang --yang-revision-default +- pyang --format yang --yang-description diff --git a/data-model/tools/yang-linter/src/main/bash/yang-linter.sh b/data-model/tools/yang-linter/src/main/bash/yang-linter.sh new file mode 100755 index 0000000..d9e0185 --- /dev/null +++ b/data-model/tools/yang-linter/src/main/bash/yang-linter.sh @@ -0,0 +1,165 @@ +#!/bin/bash +################################################################################ +# +# Copyright 2020 highstreet technologies GmbH and others +# +# 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. + +################################################################################ +# Script linting 3GPP yang modules + +# globals +args=("$@") + VERSION=0.1 + DIR=${args[$# - 1]} + IMPORT="${DIR}/external-yams" + TARGET=./target +_3GPP_REPO=https://forge.etsi.org/rep/3GPP/SA5/data-models + _3GPP_DIR=yang-models + +# functions +function show-help { + echo "Usage: yang-linter [options] " + echo + echo "As much as possible automatic yang linter" + echo + echo "Options" + echo " -3, --3gpp Clone and lint 3GPP SA5 yang modules and exit" + echo " -h, --help Show this help message and exit" + echo + echo " -i=IMPORTDIR, --imports=IMPORTDiR" + echo " Defines an import directory for standard yang modules" + echo + echo " -s, --status Show app status information and exit" + echo + echo " -t=TARGETDIR, --target=TARGETDIR" + echo " Defines the target output directory" + echo + echo " -v, --version Show version number and exit" + echo + echo " The folder containng yang modules" + quit +} + +function clone-3gpp { + message " INFO | Clone from $_3GPP_REPO" + # rm -rf ./_3GPP + # git clone "$_3GPP_REPO" "./_3GPP" + DIR="./_3GPP"/"$_3GPP_DIR" + IMPORT="${DIR}/external-yams" +} + +function lint () { + FNAME="$(basename ${@})" + echo " INFO | Linting ... ($FNAME)" + + # pyang --strict --path "${IMPORT}" --path "${DIR}" $@ + pyang --format yang --yang-canonical --yang-remove-unused-imports --output "${TARGET}/${FNAME}" --path "${IMPORT}" --path "${DIR}" $@ +} + +function message () { + echo >&2 "$@" +} + +function error-message () { + message "ERROR | $@" + show-help + quit +} + +function show-status { + PYANG="$(pyang --version)" + GIT="$(git --version)" + if [[ $PYANG == pyang* ]] && [[ $GIT == git* ]] + then + echo "Status: OK " + echo " - ${PYANG}" + echo " - ${GIT}" + else + echo "Status: Please install latest pyang and git" + echo " - https://github.com/mbj4668/pyang" + echo " - sudo apt install git-all" + fi + quit +} + +function show-version { + echo "yang-linter $VERSION" + quit +} + +function quit { + exit 1 +} + + +# app +[[ "$#" != "0" ]] || error-message "1 argument required, $# provided" + +for i in "$@" +do +case $i in + -3|--3gpp) + clone-3gpp + ;; + -h|--help) + show-help + ;; + -s|--status) + show-status + ;; + -t=*|--target=*) + TARGET="${i#*=}" + ;; + -v|--version) + show-version + ;; + *) + # message " WARN | Unknown option: $i" + ;; +esac +done + +[[ -d "$DIR" ]] || error-message "Directory '$DIR' does not exist" + +echo DIR = ${DIR} +echo TARGET = ${TARGET} +rm -rf ${TARGET} +mkdir ${TARGET} + +for yang in ${DIR}/*.yang +do + [ -f "$yang" ] || break + lint ${yang} +done + +echo +quit + +# dont change blow +sequence="" +for key in "${!mapping[@]}" +do + sequence="$sequence s!@$key@!${mapping[$key]}!g; " +done +body=http-post-body.xml +sed -e "$sequence" http-post-body.template.xml > $body; + +uri=$protocol://$controller:$port/$path/node/$nodeId/yang-ext:mount/ietf-netconf:get + +body=http-post-body.xml +sed -e "$sequence" http-post-body.template.xml > $body; + +curl -i -k -u $basicAuth -H $content -H $accept -X POST -d @${body} $uri + +echo