d9e0185a496a300d1fab2d8600f7d4e793146871
[scp/oam/modeling.git] / data-model / tools / yang-linter / src / main / bash / yang-linter.sh
1 #!/bin/bash
2 ################################################################################
3 #
4 # Copyright 2020 highstreet technologies GmbH and others
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9
10 #     http://www.apache.org/licenses/LICENSE-2.0
11
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 ################################################################################
19 # Script linting 3GPP yang modules
20
21 # globals
22 args=("$@")
23    VERSION=0.1
24        DIR=${args[$# - 1]}
25     IMPORT="${DIR}/external-yams"
26     TARGET=./target
27 _3GPP_REPO=https://forge.etsi.org/rep/3GPP/SA5/data-models
28  _3GPP_DIR=yang-models
29
30 # functions
31 function show-help {
32   echo "Usage: yang-linter [options] <directory>"
33   echo 
34   echo "As much as possible automatic yang linter"
35   echo 
36   echo "Options"
37   echo "  -3, --3gpp            Clone and lint 3GPP SA5 yang modules and exit"
38   echo "  -h, --help            Show this help message and exit"
39   echo
40   echo "  -i=IMPORTDIR, --imports=IMPORTDiR"
41   echo "                        Defines an import directory for standard yang modules"
42   echo
43   echo "  -s, --status          Show app status information and exit"
44   echo
45   echo "  -t=TARGETDIR, --target=TARGETDIR"
46   echo "                        Defines the target output directory"
47   echo
48   echo "  -v, --version         Show version number and exit"
49   echo 
50   echo "  <directory>           The folder containng yang modules"
51   quit
52 }
53
54 function clone-3gpp {
55   message " INFO | Clone from $_3GPP_REPO"
56   # rm -rf ./_3GPP
57   # git clone "$_3GPP_REPO" "./_3GPP" 
58   DIR="./_3GPP"/"$_3GPP_DIR"
59   IMPORT="${DIR}/external-yams"
60 }
61
62 function lint () {
63   FNAME="$(basename ${@})"
64   echo " INFO | Linting ... ($FNAME)"
65
66   # pyang --strict --path "${IMPORT}" --path "${DIR}" $@
67   pyang --format yang --yang-canonical --yang-remove-unused-imports --output "${TARGET}/${FNAME}" --path "${IMPORT}" --path "${DIR}" $@
68 }
69
70 function message () {
71   echo >&2 "$@"
72 }
73
74 function error-message () {
75   message "ERROR | $@"
76   show-help
77   quit
78 }
79
80 function show-status {
81   PYANG="$(pyang --version)"
82   GIT="$(git --version)"
83   if [[ $PYANG == pyang* ]] && [[ $GIT == git* ]]
84   then
85     echo "Status: OK "
86     echo "        - ${PYANG}"
87     echo "        - ${GIT}"
88   else
89     echo "Status: Please install latest pyang and git"
90     echo "        - https://github.com/mbj4668/pyang"
91     echo "        - sudo apt install git-all"
92   fi
93   quit
94 }
95
96 function show-version {
97   echo "yang-linter $VERSION"
98   quit
99 }
100
101 function quit {
102   exit 1
103 }
104
105
106 # app
107 [[ "$#" != "0" ]] || error-message "1 argument required, $# provided"
108
109 for i in "$@"
110 do
111 case $i in
112     -3|--3gpp)
113     clone-3gpp
114     ;;
115     -h|--help)
116     show-help
117     ;;
118     -s|--status)
119     show-status
120     ;;
121     -t=*|--target=*)
122     TARGET="${i#*=}"
123     ;;
124     -v|--version)
125     show-version
126     ;;
127     *)
128       # message " WARN | Unknown option: $i"
129     ;;
130 esac
131 done
132
133 [[ -d "$DIR" ]] || error-message "Directory '$DIR' does not exist"
134
135 echo    DIR = ${DIR}
136 echo TARGET = ${TARGET}
137 rm -rf ${TARGET}
138 mkdir ${TARGET}
139
140 for yang in ${DIR}/*.yang
141 do
142     [ -f "$yang" ] || break
143     lint ${yang}
144 done
145
146 echo
147 quit
148
149 # dont change blow
150 sequence=""
151 for key in "${!mapping[@]}"
152 do
153   sequence="$sequence s!@$key@!${mapping[$key]}!g; "
154 done
155 body=http-post-body.xml
156 sed -e "$sequence" http-post-body.template.xml > $body;
157
158 uri=$protocol://$controller:$port/$path/node/$nodeId/yang-ext:mount/ietf-netconf:get
159
160 body=http-post-body.xml
161 sed -e "$sequence" http-post-body.template.xml > $body;
162
163 curl -i -k -u $basicAuth -H $content -H $accept -X POST -d @${body} $uri 
164
165 echo