-
Notifications
You must be signed in to change notification settings - Fork 346
/
yang2dsdl
executable file
·289 lines (267 loc) · 8.85 KB
/
yang2dsdl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#!/bin/sh
# Copyright (c) 2010 by Ladislav Lhotka, CZ.NIC <[email protected]>
#
# Translates YANG module(s) to DSDL schemas and validates instances
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# Defaults for arguments
target="data"
basename=""
dir="."
instance=""
pyang_opts="--dsdl-no-documentation --dsdl-no-dublin-core"
gen_rng_parms=""
noschema=0
onlydata=0
hello=0
jing=0
xslt_dir=${PYANG_XSLT_DIR:-/usr/local/share/yang/xslt}
schema_dir=${PYANG_RNG_LIBDIR:-/usr/local/share/yang/schema}
usage() {
cat <<EOF
Usage: 1. yang2dsdl [-t <target>] [-d <dir>] [-b <basename>] \\
[-j] [-v <instance>] <module> ...
2. yang2dsdl -L [-t <target>] [-d <dir>] [-b <basename>] \\
[-j] [-v <instance>] <server-hello>
3. yang2dsdl -s [-t target] [-d <dir>] [-j] -b <basename> \\
-v <instance>
4. yang2dsdl -h
Generates DSDL schemas from one or more YANG modules and
validates instance documents.
Options:
-h Displays this help message and exits.
-t <target> Specifies the validation target. The argument <target>
must be one of: "data" (default), "config", "get-reply",
"get-config-reply", "edit-config", "rpc", "rpc-reply"
"get-data-reply" or "notification".
-b <basename> Specifies the basename of files in which the output
schemas are stored. The default is the concatenation
of the names of all input YANG modules connected
with the underscore character "_".
-L Use server hello message instead of YANG modules as
the only input file.
(By default, all are considered available.)
-d <dir> Specifies the directory for output files
(current directory by default).
-v <instance> Validates the instance document in file <instance>.
-j Use jing for RELAX NG validation instead of xmllint.
-s Performs just validation, without (re)generating
the schemas. Only allowed together with -v and -b.
-x Try to translate modules written in unsupported YANG
versions. If the module doesn't use any constructs
introduced after YANG version 1, this may work.
This option may produce unexpected results. Use at own risk.
-c Use only definitions with status "current" in the YANG
module.
EOF
}
gen_relaxng() {
echo "== Generating RELAX NG schema '$btname.rng'"
rngparms="$XSLT_OPTS $gen_rng_parms --stringparam target $target \
--stringparam basename $basename \
--stringparam schema-dir $schema_dir \
$xslt_dir/gen-relaxng.xsl $hybs"
xsltproc --output $btname.rng $rngparms
[ $? -eq 0 ] || exit 2
xsltproc --output $gdefsname --stringparam gdefs-only 1 $rngparms
[ $? -eq 0 ] || exit 2
echo "Done."
}
gen_schematron() {
printf "\n== Generating Schematron schema '$btname.sch'\n"
xsltproc $XSLT_OPTS --output $btname.sch \
--stringparam target $target $xslt_dir/gen-schematron.xsl $hybs
[ $? -eq 0 ] || exit 2
echo "Done."
}
gen_dsrl() {
printf "\n== Generating DSRL schema '$btname.dsrl'\n"
xsltproc $XSLT_OPTS --output $btname.dsrl \
--stringparam target $target $xslt_dir/gen-dsrl.xsl $hybs
[ $? -eq 0 ] || exit 2
echo "Done."
}
validate() {
printf "\n== Validating grammar and datatypes ...\n"
if [ -f $btname.rng ] ; then
if [ "$jing" = "0" ] ; then
xmllint --noout --relaxng $btname.rng $instance
[ $? -eq 0 ] || exit 3
else
jing $btname.rng $instance
[ $? -eq 0 ] || exit 3
echo $instance validates.
fi
else
echo "RELAX NG schema '$btname.rng' not found."
exit 2
fi
if [ "$target" = "edit-config" ] ; then
return
fi
printf "\n== Adding default values... "
if [ -f $btname.dsrl ] ; then
xsltproc -o $dsrlxsl $xslt_dir/dsrl2xslt.xsl $btname.dsrl
[ $? -eq 0 ] || exit 2
xsltproc -o $instwdef $dsrlxsl $instance
[ $? -eq 0 ] || exit 3
inst4sch=$instwdef
printf "done.\n"
else
echo "DSRL schema '$btname.dsrl' not found, no defaults added ..."
inst4sch=$instance
fi
printf "\n== Validating semantic constraints ...\n"
if [ -f $btname.sch ] ; then
xsltproc $xslt_dir/iso_abstract_expand.xsl $btname.sch | \
xsltproc -o $schxsl $xslt_dir/iso_svrl_for_xslt1.xsl -
xsltproc $schxsl $inst4sch | xsltproc $xslt_dir/svrl2text.xsl -
[ $? -eq 0 ] || exit 3
else
echo "Schematron schema '$btname.sch' not found, skipping ..."
fi
}
cleanup () {
rm -f $hybs $schxsl $dsrlxsl $instwdef
}
rtfm () {
echo 'Please check the manual page or run "yang2dsdl -h"' >&2
exit 1
}
while getopts ":ht:d:b:v:jLsxc" opt ; do
case $opt in
h)
usage
exit 0
;;
t)
target=$OPTARG
if [ "$target" != "data" ] && [ "$target" != "config" ] && \
[ "$target" != "get-reply" ] && \
[ "$target" != "get-data-reply" ] && \
[ "$target" != "get-config-reply" ] \
&& [ "$target" != "rpc" ] && [ "$target" != "rpc-reply" ] && \
[ "$target" != "edit-config" ] && [ "$target" != "notification" ]
then
echo "Invalid argument for -t: $target."
rtfm
fi
;;
d)
dir=$OPTARG
if [ ! -d $dir ] ; then
echo "Directory '$dir' doesn't exist."
exit 1
fi
;;
b)
basename=$OPTARG
;;
v)
instance=$OPTARG
if [ ! -f $instance ] ; then
echo "File '$instance' not found."
exit 1
fi
;;
j)
jing=1
;;
L)
hello=1
;;
s)
noschema=1
;;
x)
pyang_opts="${pyang_opts} --dsdl-lax-yang-version"
;;
c)
pyang_opts="${pyang_opts} --max-status current"
;;
\?)
echo "Invalid option: -$OPTARG." >&2
rtfm
;;
:)
echo "Option -$OPTARG requires an argument."
rtfm
;;
esac
done
if [ "$noschema" = "1" ] && [ "$instance" = "" ] ; then
echo "Option -s may only be used together with -v."
exit 1
fi
shift $(($OPTIND-1))
infiles=$*
randext=$(mktemp -u .XXXXXXXX)
hybs=$dir/dsdl$randext
schxsl=$dir/sch$randext
dsrlxsl=$dir/dsrl$randext
instwdef=$dir/inst$randext
trap "cleanup" 0 1 2 3 15
if [ "$noschema" = "0" ] ; then
if [ "$infiles" = "" ] ; then
echo "No input file(s) given." >&2
rtfm
elif [ ! -w $dir ] ; then
echo "Directory '$dir' not writable."
exit 1
fi
if [ "$hello" = "1" ] ; then
printf "\n== Validating hello file ...\n"
hellorng=$schema_dir/hello-server.rng
if [ "$jing" = "0" ] ; then
xmllint --noout --relaxng $hellorng $infiles
[ $? -eq 0 ] || exit 3
else
jing $hellorng $infiles
[ $? -eq 0 ] || exit 3
echo $infiles validates.
fi
pyang_opts="${pyang_opts} --hello"
printf "\n"
fi
${PYANG:-pyang} -f dsdl -o $hybs $pyang_opts $infiles
[ $? -eq 0 ] || exit 2
if [ "$basename" = "" ] ; then
basename=$(xsltproc $xslt_dir/basename.xsl $hybs)
fi
elif [ "$basename" = "" ] ; then
echo "-b <basename> is required with -s option." >&2
rtfm
fi
btname=$dir/$basename-$target
if [ "$target" = "get-config-reply" ] || [ "$target" = "config" ]
then
gdefsname=$dir/$basename-gdefs-config.rng
elif [ "$target" = "edit-config" ]
then
gdefsname=$dir/$basename-gdefs-edit.rng
else
gdefsname=$dir/$basename-gdefs.rng
fi
if [ "$noschema" = "0" ] ; then
gen_relaxng
if [ "$target" != "edit-config" ] ; then
gen_schematron
gen_dsrl
fi
else
echo "== Using pre-generated schemas"
fi
if [ "$instance" != "" ] ; then
validate
fi
exit 0