Minor fixes in extract_modules script
[integration/distribution.git] / karaf-scripts / src / main / assembly / bin / extract_modules.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
4 #
5 # This program and the accompanying materials are made available under the
6 # terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 # and is available at http://www.eclipse.org/legal/epl-v10.html
8 #
9
10 #
11 # This script visits all jars within the OpenDaylight karaf distribution and extracts
12 # all production YANG modules (as located in META-INF/yang)
13 #
14 set -e
15 BIN_DIR=`dirname $0`
16 OUTPUT="$BIN_DIR/../opendaylight-models"
17 INPUT="$BIN_DIR/.."
18
19 # FIXME: also wipe output?
20 [ -d "$OUTPUT" ] || mkdir "$OUTPUT"
21
22 PROJECTS=`ls -d $INPUT/system/org/opendaylight/*`
23 for proj in $PROJECTS; do
24     proj=`basename $proj`
25     echo "Extracting yang modules from $proj"
26     JARS=`find $INPUT/system/org/opendaylight/$proj -type f -name '*.jar' | sort -u`
27     [ -d "$OUTPUT/$proj" ] || mkdir $OUTPUT/$proj
28     for jar in $JARS; do
29         unzip -l "$jar" | grep -q -e "\.yang$" &&
30         unzip -q "$jar" 'META-INF/yang/*' -d "$OUTPUT/$proj"
31     done
32     if [ -z "$(ls -A $OUTPUT/$proj)" ]; then
33         rm -rf $OUTPUT/$proj
34     else
35         rm -f $OUTPUT/$proj/*.yang
36         YANGS=`find $OUTPUT/$proj/META-INF/yang/ -type f -name '*.yang'`
37         for yang in $YANGS; do
38             name=`basename $yang`
39             less $yang | grep -q namespace.*opendaylight &&
40             mv $OUTPUT/$proj/META-INF/yang/$name $OUTPUT/$proj &&
41             echo "  $name"
42         done
43         rm -rf $OUTPUT/$proj/META-INF
44     fi
45 done
46 echo -e "\nYang Modules are extracted to `readlink -f $OUTPUT`"