forked from GianmarcoBruno/json-yang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate
executable file
·167 lines (143 loc) · 4.25 KB
/
validate
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
#!/bin/bash
# gianmarco.bruno@ericsson.com, carlo.perocchio@ericsson.com
print_usage() {
echo "usage: validate -j <JSON_FILE> -w <WHAT> [-y <YANG_DIR>] [-f]"
echo "WHAT one of: data, config"
echo "YANG_DIR is the local directory unless specified"
echo "if -f (FETCH) is specified, validation is made using modules"
echo "specified in the JSON as e.g.:"
echo "\"// __REFERENCE_DRAFTS__\": {
\"ietf-network@2017-12-18\": \"draft-ietf-i2rs-yang-network-topo-20\"",
echo ""
echo "exiting: did nothing"
exit 1
}
JSON_FILE=""
WHAT=""
YANG_DIR=""
: YANG_DIR=${YANG_DIR:=.}
THIS_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
TEMP_DIR=$(mktemp -d)
FETCH=false
# to suppress pushd/popd messages
pushd () {
command pushd "$@" > /dev/null
}
popd () {
command popd "$@" > /dev/null
}
function all_yang_files_in_dir {
ALL_YANG_FILES="`ls ${YANG_DIR}/*.yang`"
}
function exit_if_failed {
if [ $? -ne 0 ]
then
echo $1
exit 1
fi
}
function fetch_yang_models {
if [ "$FETCH" = true ]
then
echo "..will download models from the network"
$THIS_SCRIPT_DIR/scripts/fetchModels $1
else
echo "..will use local models for validation"
fi
}
# pseudo-comments has been provisionally defined in the IETF TEAS Transport NBI
# Design Team in JSON as fictitious key/value pairs on a single line:
# "// TEXT" :" OTHER TEXT", for example "// router-id": "NODE-S3-ID"
function cleanup {
rm -rf $TEMP_DIR
}
process_args() {
while [ "$1" != "" ]; do
if [ "$1" == "-h" -o "$1" == "--help" ]; then
print_usage
exit 1
fi
if [ "$1" == "-j" ]; then
JSON_FILE="$2"
fi
if [ "$1" == "-y" ]; then
YANG_DIR="$2"
shift
fi
if [ "$1" == "-f" ]; then
FETCH=true
shift
fi
if [ "$1" == "-w" ]; then
WHAT="$2"
shift
fi
shift
done
}
main() {
process_args $@
# check for mandatory parameters
if [[ "$WHAT" == "" || "$JSON_FILE" == "" ]]; then
print_usage
exit 1
fi
if [ ! -f $JSON_FILE ]; then
echo "given file $JSON_FILE does not exists"
exit 1
fi
echo "(0) building list of all yang files"
fetch_yang_models $JSON_FILE
# results are in $ALL_YANG_FILES
all_yang_files_in_dir $YANG_DIR
exit_if_failed "unable to write list of yang files (perhaps empty?)"
sed "s/ /\n/g" <<< $ALL_YANG_FILES
echo "(1) writing jtox .."
JTOX_FILE=$TEMP_DIR/_temporary.jtox
pyang -p $YANG_DIR -f jtox -o $JTOX_FILE $ALL_YANG_FILES
exit_if_failed "writing jtox"
echo "preparing step (2) stripping pseudo-comments from JSON .."
# we want to keep the original JSON unchanged
JSON_TMP=${TEMP_DIR}/$(basename $JSON_FILE)
python $THIS_SCRIPT_DIR/scripts/Stripper.py -i=${JSON_FILE} -o=${JSON_TMP} --clean
exit_if_failed "stripping comments"
echo "(2) converting JSON to XML .."
XML_BASE=$(basename ${JSON_FILE%.json})
XML_TMP=$TEMP_DIR/${XML_BASE}_tmp.xml
XML_FILE=$TEMP_DIR/${XML_BASE}.xml
json2xml -t $WHAT -o $XML_TMP $JTOX_FILE $JSON_TMP
exit_if_failed "converting JSON to XML"
# optionally beautify XML if xmllint is installed
if [ command -v xmllint >/dev/null 2>&1 ]
then
echo "I require xmllint but it's not installed. Skipping."
cp $XML_TMP $XML_FILE
else
echo "Beautifying XML"
xmllint --format $XML_TMP > $XML_FILE
exit_if_failed "beautifying XML"
fi
rm $XML_TMP
echo "(3) writing schemas .."
# It seems that yang2dsdl wants the yang modules in the dir it was called from
# So we move temporarily there and then resume from the current directory.
CWD=$(pwd)
pushd .
cd $YANG_DIR
pwd
yang2dsdl -t $WHAT -d $TEMP_DIR -b _placeholder *.yang
popd
exit_if_failed "writing schemas"
echo "(4) validating XML .."
# For same purpose we move temporarily to the directory where the schemas are placed.
pushd .
cd $TEMP_DIR
yang2dsdl -s -j -b _placeholder -t $WHAT -v $XML_FILE
popd
exit_if_failed "validation"
# do not write anything on stdout otherwise
# the test driver gets confused
cleanup
return 0
}
main "$@"