blob: 9820b324c65909c729136a42d92f046480f9dfde [file] [log] [blame]
tom6a9f2722014-09-13 17:00:02 -07001#!/bin/bash
2# ONOS developer BASH profile conveniences
tom5a18e802014-09-18 12:38:15 -07003# Simply include in your own .bash_aliases or .bash_profile
tom6a9f2722014-09-13 17:00:02 -07004
5# Root of the ONOS source tree
Thomas Vachuska045c01d2014-12-04 00:18:06 -08006export ONOS_ROOT=${ONOS_ROOT:-~/onos}
Ray Milkey8a0c7c72017-07-18 11:37:04 -07007# This is a hack to remove symlinks from the ONOS_ROOT path. To be removed when
8# the protobuf buck rules can handle symlinks
9ONOS_ROOT=$(pushd $ONOS_ROOT >/dev/null && pwd -P && popd >/dev/null)
tom6a9f2722014-09-13 17:00:02 -070010
11# Setup some environmental context for developers
Pavlin Radoslavov93617212014-10-16 09:54:04 -070012if [ -z "${JAVA_HOME}" ]; then
13 if [ -x /usr/libexec/java_home ]; then
Thomas Vachuskad8531152014-11-03 14:39:57 -080014 export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
15 elif [ -d /usr/lib/jvm/java-8-oracle ]; then
16 export JAVA_HOME="/usr/lib/jvm/java-8-oracle"
Jonathan Harte3c951e2015-07-14 13:28:35 -070017 elif [ -d /usr/lib/jvm/java-8-openjdk-amd64 ]; then
18 export JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"
Pavlin Radoslavov93617212014-10-16 09:54:04 -070019 fi
20fi
Thomas Vachuska7b652ad2014-10-30 14:10:51 -070021
Jian Li98dc9ad2016-01-06 14:05:47 -080022export MAVEN=${MAVEN:-~/Applications/apache-maven-3.3.9}
Thomas Vachuska7b652ad2014-10-30 14:10:51 -070023
Jon Hallb84df5d2017-01-31 11:19:48 -080024export KARAF_VERSION=${KARAF_VERSION:-3.0.8}
Thomas Vachuska255b65f2014-11-23 14:25:31 -080025export KARAF_ROOT=${KARAF_ROOT:-~/Applications/apache-karaf-$KARAF_VERSION}
26export KARAF_LOG=$KARAF_ROOT/data/log/karaf.log
tom6a9f2722014-09-13 17:00:02 -070027
28# Setup a path
Thomas Vachuska4ccc7d32015-09-03 13:39:40 -070029export PATH="$PATH:$ONOS_ROOT/tools/dev/bin"
30export PATH="$PATH:$ONOS_ROOT/tools/test/bin:$ONOS_ROOT/tools/test/scenarios/bin"
Devin Lim0d944e22017-06-23 15:17:53 -070031export RUN_PACK_PATH=${RUN_PACK_PATH:-$ONOS_ROOT/tools/package/runtime/bin}
32export PATH="$RUN_PACK_PATH:$PATH"
tom1cd74ae2014-10-01 14:58:32 -070033export PATH="$PATH:$ONOS_ROOT/tools/build"
Thomas Vachuska255b65f2014-11-23 14:25:31 -080034export PATH="$PATH:$MAVEN/bin:$KARAF_ROOT/bin"
tom6a9f2722014-09-13 17:00:02 -070035
Roan Huang0fa4bc42015-05-01 15:38:20 +080036# Setup cell enviroment
37export ONOS_CELL=${ONOS_CELL:-local}
38
Thomas Vachuskac3c969a2015-08-19 16:51:16 -070039# Setup default web user/password
40export ONOS_WEB_USER=onos
41export ONOS_WEB_PASS=rocks
Thomas Vachuskaaf0ee532015-08-19 14:17:36 -070042
43# Setup default location of test scenarios
Thomas Vachuskae76f6532015-07-08 09:40:53 -070044export ONOS_SCENARIOS=$ONOS_ROOT/tools/test/scenarios
45
Carmelo Casconeb7524272017-06-05 16:53:13 -040046# Setup path to Mininet custom scripts
47export ONOS_MN_PY=$ONOS_ROOT/tools/dev/mininet/onos.py
48export BMV2_MN_PY=$ONOS_ROOT/tools/dev/mininet/bmv2.py
49
tom6a9f2722014-09-13 17:00:02 -070050# Convenience utility to warp to various ONOS source projects
51# e.g. 'o api', 'o dev', 'o'
52function o {
Ray Milkey33d81c62016-04-01 13:18:02 -070053 cd $(find $ONOS_ROOT/ -type d -and \( -name 'buck-out' -o -name '.git' -o -name 'target' -o -name 'gen-src' -o -name 'src' \) -prune -o -type d | \
Thomas Vachuska49367a62017-08-29 09:18:19 -070054 egrep "${1:-$ONOS_ROOT}" | awk '{print length($1)"\t"$1}' | sort -n | cut -f2 | head -n 1)
tom6a9f2722014-09-13 17:00:02 -070055}
56
Thomas Vachuska6066dff2016-10-26 13:57:36 -070057# Buck (if "buck" is not on the PATH)
58[ -z "$(which buck)" ] && alias buck="onos-buck"
tom6a9f2722014-09-13 17:00:02 -070059
tom82d6bde2014-09-23 17:33:58 -070060# Short-hand for ONOS build, package and test.
61alias ob='onos-build'
Aaron Kruglikovc9390372017-06-16 13:20:42 -070062alias obf='(cd $ONOS_ROOT && onos-buck build onos)'
Thomas Vachuska7cbfd4f2014-11-04 18:22:49 -080063alias obd='onos-build-docs'
tom82d6bde2014-09-23 17:33:58 -070064alias op='onos-package'
tomcaf3bf72014-09-23 13:20:53 -070065alias ot='onos-test'
Thomas Vachuska6066dff2016-10-26 13:57:36 -070066
67alias deprecatedAlias='echo "This alias has been deprecated."'
68alias obi=deprecatedAlias
69alias obs=deprecatedAlias
70
Aaron Kruglikovc9390372017-06-16 13:20:42 -070071alias ok='NO_BUCKD=1 onos-buck run onos-local --'
Thomas Vachuska6066dff2016-10-26 13:57:36 -070072alias oh='onos localhost halt'
73
tom0511a522014-10-04 12:06:02 -070074alias ol='onos-log'
toma6897792014-10-08 22:21:05 -070075alias ow='onos-watch'
Thomas Vachuska3cd677d2015-05-20 11:25:49 -070076alias ocl='onos-check-logs'
Thomas Vachuska85314292014-11-14 17:52:12 -080077alias oi='setPrimaryInstance'
tom6a9f2722014-09-13 17:00:02 -070078
Thomas Vachuska152f9fd2015-04-02 16:28:13 -070079# Short-hand for tailing and searching the ONOS (karaf) log
tom1cd74ae2014-10-01 14:58:32 -070080alias tl='$ONOS_ROOT/tools/dev/bin/onos-local-log'
Thomas Vachuska152f9fd2015-04-02 16:28:13 -070081alias gl='grep $KARAF_LOG --colour=auto -E -e '
82
Charles M.C. Chan870cc032015-04-24 04:57:42 +080083function filterLocalLog {
Thomas Vachuska152f9fd2015-04-02 16:28:13 -070084 tl | grep --colour=always -E -e "${1-org.onlab|org.onosproject}"
85}
Charles M.C. Chan870cc032015-04-24 04:57:42 +080086alias tlo='filterLocalLog'
Charles M.C. Chane39f3142015-05-22 18:35:24 +080087alias tle='tlo "ERROR|WARN|Exception|Error"'
Charles M.C. Chan870cc032015-04-24 04:57:42 +080088
89function filterLog {
90 ol | grep --colour=always -E -e "${1-org.onlab|org.onosproject}"
91}
92alias olo='filterLog'
Thomas Vachuska3cd677d2015-05-20 11:25:49 -070093alias ole='olo "ERROR|WARN|Exception|Error"'
tom6a9f2722014-09-13 17:00:02 -070094
95# Pretty-print JSON output
96alias pp='python -m json.tool'
97
Thomas Vachuskaaf0ee532015-08-19 14:17:36 -070098# Short-hand to launch Java API docs, REST API docs and ONOS GUI
Thomas Vachuska24c849c2014-10-27 09:53:05 -070099alias docs='open $ONOS_ROOT/docs/target/site/apidocs/index.html'
Thomas Vachuskaaf0ee532015-08-19 14:17:36 -0700100alias rsdocs='onos-rsdocs'
tom1a2908c2014-09-23 16:37:39 -0700101alias gui='onos-gui'
tom5c255702014-09-18 06:57:39 -0700102
Thomas Vachuska6066dff2016-10-26 13:57:36 -0700103# Short-hand for 'mvn clean install' for us lazy folk
104alias mci='mvn clean install'
105alias mcis='mvn clean install -DskipTests -Dcheckstyle.skip'
106alias mis='mvn install -DskipTests -Dcheckstyle.skip'
107
108# Git annotated one-line log
109alias gil='git log --oneline --decorate=short'
tom5c255702014-09-18 06:57:39 -0700110
tom5a18e802014-09-18 12:38:15 -0700111# Test related conveniences
112
tom5a18e802014-09-18 12:38:15 -0700113# SSH to a specified ONOS instance
tom0872a172014-09-23 11:24:26 -0700114alias sshctl='onos-ssh'
115alias sshnet='onos-ssh $OCN'
tom5a18e802014-09-18 12:38:15 -0700116
Thomas Vachuskae1125352016-11-09 14:06:51 -0800117# Runs a sequence of STC scenarios until the first failure.
118function stcs {
Thomas Vachuska8cc53182016-11-14 13:06:54 -0800119 for s in "$@"; do
120 if [[ $s =~ ^[0-9]*$ ]]; then
121 printf "Waiting %d seconds...\n" $s
122 sleep $s
123 else
124 printf "Running scenario %s...\n" $s
125 stc $s || return 1
126 fi
127 done
Thomas Vachuskae1125352016-11-09 14:06:51 -0800128}
Thomas Vachuska785f5812015-03-19 01:11:00 -0700129
Thomas Vachuskacc0b7d62016-07-12 14:03:11 -0700130# Applies the settings in the specified topology recipe file or lists current
131# topo recipe definition if no topo recipe file is given.
132function topo {
133 topo=${1:-""}
134 case "$topo" in
135 "")
136 env | egrep "ONOS_TOPO"
137 env | egrep "(OTD|OTL|OTH)="
138 ;;
139
140 *)
141 [ ! -f $ONOS_ROOT/tools/test/topos/$1.recipe ] && echo "No such topo recipe: $1" >&2 && return 1
142 unset ONOS_TOPO OTD OTL OTH ONOS_DEVICES ONOS_HOSTS
Ray Milkeya47073b2018-03-09 08:57:52 -0800143 ONOS_DEVICES=""
144 ONOS_HOSTS=""
Thomas Vachuskacc0b7d62016-07-12 14:03:11 -0700145 unset $(env | sed -n 's:\(^OT[DLH][0-9]\{1,\}\)=.*:\1 :g p')
146 export ONOS_TOPO=$1
147 . $ONOS_ROOT/tools/test/topos/$1.recipe
148 let d=1; while [ $d -le $OTD ]; do
149 dev="$(printf 'of:%016x' $d)"
150 export OTD$d=$dev; export ONOS_DEVICES="$ONOS_DEVICES $dev"
151 let d=d+1;
152 done
153 let h=1; while [ $h -le $OTH ]; do
154 host="$(printf '00:00:00:00:00:%02x/-1' $h)"
155 export OTH$h=$host; export ONOS_HOSTS="$ONOS_HOSTS $host"
156 let h=h+1
157 done
158 topo
159 esac
160}
161
162# Lists available topo recipes
163function topos {
164 for topo in $(ls -1 $ONOS_ROOT/tools/test/topos/*.recipe); do
165 name=$(basename $topo .recipe)
166 printf "%-16s %s\n" \
167 "$([ $name = $ONOS_TOPO ] && echo $name '*' || echo $name)" \
168 "$(grep '^#' $topo | head -n 1)"
169 done
170}
171
Thomas Vachuska785f5812015-03-19 01:11:00 -0700172# Sets the primary instance to the specified instance number.
173function setPrimaryInstance {
Jon Hallee85e2f2016-06-07 10:36:33 -0700174 export ONOS_INSTANCES=$(env | grep 'OC[0-9]*=' | sort | cut -d= -f2)
Thomas Vachuska785f5812015-03-19 01:11:00 -0700175 export OCI=$(env | egrep "OC[0-9]+" | sort | egrep OC${1:-1} | cut -d= -f2)
176 echo $OCI
177}
178
Jordan Halterman980a8c12017-09-22 18:01:19 -0700179# Sets minority (OCMI) and majority (OCMA) variables
180function setMinorityMajority {
181 nodes=($(env | grep 'OC[0-9]*=' | sort | cut -d= -f2))
182 middle=$(expr "${#nodes[@]}" / "2")
183 index=0
184 min=1
185 maj=1
186 for node in "${nodes[@]}"; do
187 if [ "$index" -gt "$middle" ]; then
188 export OCMI${min}=${node}
189 min=$(expr $min + 1)
190 else
191 export OCMA${maj}=${node}
192 maj=$(expr $maj + 1)
193 fi
194 index=$(expr $index + 1)
195 done
196}
197
Thomas Vachuska1eff3a62016-05-03 01:07:24 -0700198# ON.Lab shared test cell warden address
Thomas Vachuskad2535d72017-12-05 13:39:27 -0800199export CELL_WARDEN="10.192.19.72"
200export CELL_SLAVES="$CELL_WARDEN 10.192.19.71 10.192.19.70"
Thomas Vachuska1eff3a62016-05-03 01:07:24 -0700201
Thomas Vachuskafdeda922016-05-16 11:37:00 -0700202# Clears cell environment
203function clearCell {
204 unset ONOS_CELL ONOS_NIC ONOS_IP ONOS_APPS ONOS_BOOT_FEATURES
205 unset OCI OCN OCT ONOS_INSTANCES ONOS_FEATURES
Charles Chanc7a60ce2017-05-05 13:56:27 -0700206 unset ONOS_USER ONOS_GROUP ONOS_WEB_USER ONOS_WEB_PASS
Thomas Vachuskafdeda922016-05-16 11:37:00 -0700207 unset $(env | sed -n 's:\(^OC[0-9]\{1,\}\)=.*:\1 :g p')
Jordan Halterman980a8c12017-09-22 18:01:19 -0700208 unset $(env | sed -n 's:\(^OCMI[0-9]\{1,\}\)=.*:\1 :g p')
209 unset $(env | sed -n 's:\(^OCMA[0-9]\{1,\}\)=.*:\1 :g p')
Thomas Vachuskafdeda922016-05-16 11:37:00 -0700210}
211
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700212# Applies the settings in the specified cell file or lists current cell definition
213# if no cell file is given.
214function cell {
Jon Hall7dc59d62016-05-10 14:30:16 -0700215 cell=${1:-""}
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700216 case "$cell" in
217 "borrow")
Thomas Vachuskafdeda922016-05-16 11:37:00 -0700218 clearCell
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700219 aux="/tmp/cell-$$"
Thomas Vachuskafdeda922016-05-16 11:37:00 -0700220 duration=${2:-0}
Thomas Vachuska5420ba32016-05-13 14:45:25 -0400221 spec=${3:-3+1}
222 spec=${spec/+/%2B}
223 user=${4:-$(id -un)}
Ray Milkey1f64bbf2018-03-09 09:52:29 -0800224 hint=${5:-""}
Thomas Vachuska05e47a32016-10-24 09:35:11 -0700225 query="duration=$duration&spec=$spec&user=$user&cellNameHint=$hint"
Thomas Vachuskafdeda922016-05-16 11:37:00 -0700226 curl -sS -X POST "http://$CELL_WARDEN:4321/?$query" -d "$(cat ~/.ssh/id_rsa.pub)" > $aux
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700227 . $aux
228 rm -f $aux
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700229 setPrimaryInstance 1 >/dev/null
Jordan Halterman980a8c12017-09-22 18:01:19 -0700230 setMinorityMajority >/dev/null
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700231 onos-verify-cell
Thomas Vachuskacc0b7d62016-07-12 14:03:11 -0700232 topo default >/dev/null
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700233 ;;
234 "return")
235 curl -sS -X DELETE "http://$CELL_WARDEN:4321/?user=${2:-$(id -un)}"
Thomas Vachuskafdeda922016-05-16 11:37:00 -0700236 clearCell
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700237 ;;
238
239 "status")
240 curl -sS "http://$CELL_WARDEN:4321/" | sort
241 ;;
242
243 "")
Charles Chan9b152fc2016-11-21 12:06:49 -0800244 env | egrep "^ONOS_CELL"
245 env | egrep "^OCI"
246 env | egrep "^OC[0-9]+" | sort
247 env | egrep "^OC[NT]"
248 env | egrep "^ONOS_" | egrep -v 'ONOS_ROOT|ONOS_CELL|ONOS_INSTANCES|ONOS_DEVICES|ONOS_HOSTS' | sort
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700249 ;;
250
251 *)
Thomas Vachuskafdeda922016-05-16 11:37:00 -0700252 [ ! -f $ONOS_ROOT/tools/test/cells/$1 ] && echo "No such cell: $1" >&2 && return 1
253 clearCell
Charles Chanc7a60ce2017-05-05 13:56:27 -0700254 export ONOS_USER=sdn
255 export ONOS_GROUP=sdn
256 export ONOS_WEB_USER=onos
257 export ONOS_WEB_PASS=rocks
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700258 export ONOS_CELL=$1
259 . $ONOS_ROOT/tools/test/cells/$1
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700260 setPrimaryInstance 1 >/dev/null
261 cell
Thomas Vachuskacc0b7d62016-07-12 14:03:11 -0700262 topo default >/dev/null
Thomas Vachuskaf07ec212016-05-10 12:17:40 -0700263 esac
264}
tomecd0fbd2014-09-19 08:47:05 -0700265
Thomas Vachuskae6185572016-05-04 19:26:15 -0700266[ -n "$ONOS_CELL" -a "$ONOS_CELL" != "borrow" ] && cell $ONOS_CELL > /dev/null
tomecd0fbd2014-09-19 08:47:05 -0700267
268# Lists available cells
269function cells {
tom2482e6f2014-10-01 16:51:48 -0700270 for cell in $(ls -1 $ONOS_ROOT/tools/test/cells); do
Thomas Vachuska785f5812015-03-19 01:11:00 -0700271 printf "%-16s %s\n" \
Yi Tseng7f425d52017-02-02 13:33:59 -0800272 "$([ ! -z $ONOS_CELL ] && [ $cell = $ONOS_CELL ] && echo $cell '*' || echo $cell)" \
tom2482e6f2014-10-01 16:51:48 -0700273 "$(grep '^#' $ONOS_ROOT/tools/test/cells/$cell | head -n 1)"
274 done
tomecd0fbd2014-09-19 08:47:05 -0700275}
tom5a18e802014-09-18 12:38:15 -0700276
Thomas Vachuska9507d2a2015-12-01 20:45:57 -0800277# Find a process by regex
tom5c255702014-09-18 06:57:39 -0700278function spy {
279 ps -ef | egrep "$@" | grep -v egrep
280}
281
Thomas Vachuska9507d2a2015-12-01 20:45:57 -0800282# Kill a process by regex
tom5c255702014-09-18 06:57:39 -0700283function nuke {
tom85258ee2014-10-07 00:10:02 -0700284 spy "$@" | cut -c7-11 | xargs kill
tom5c255702014-09-18 06:57:39 -0700285}
Ayaka Koshibea9985132014-12-18 14:55:22 -0800286
Thomas Vachuskaea2d9fd2015-09-23 13:13:25 -0700287# Edit a cell file by providing a cell name; opens the cell file in $EDITOR.
288function vicell {
289 local apply=false
290 local create=false
Ayaka Koshibe77acdc72016-02-03 17:19:24 -0800291 local ${cdf:=$ONOS_CELL}
Thomas Vachuskaea2d9fd2015-09-23 13:13:25 -0700292 local cpath="${ONOS_ROOT}/tools/test/cells/"
Ayaka Koshibea9985132014-12-18 14:55:22 -0800293
Ayaka Koshibe77acdc72016-02-03 17:19:24 -0800294 if [ "$1" = "-h" ] ; then
Thomas Vachuskaea2d9fd2015-09-23 13:13:25 -0700295 printf "usage: vicell [file] [options]\n\noptions:\n"
Ayaka Koshibe77acdc72016-02-03 17:19:24 -0800296 printf "\t[file]: cell name (default: current cell)\n"
Thomas Vachuskaea2d9fd2015-09-23 13:13:25 -0700297 printf "\t-a: apply the cell after editing\n"
Ayaka Koshibe77acdc72016-02-03 17:19:24 -0800298 printf "\t-e: [editor] set EDITOR to [editor] (default: *vi*)\n"
Thomas Vachuskaea2d9fd2015-09-23 13:13:25 -0700299 printf "\t-c: create cell file if none exist\n\n"
300 return 1
301 fi
Ayaka Koshibea9985132014-12-18 14:55:22 -0800302
Thomas Vachuskaea2d9fd2015-09-23 13:13:25 -0700303 while [ $# -gt 0 ]; do
304 case "$1" in
305 -a) apply=true ;;
306 -e) EDITOR=$2; shift ;;
307 -c) create=true ;;
308 *) cdf="$1" ;;
309 esac
310 shift
311 done
Ayaka Koshibea9985132014-12-18 14:55:22 -0800312
Ayaka Koshibe77acdc72016-02-03 17:19:24 -0800313 if [ ! -e "${cpath}${cdf}" ] && ! ($create) ; then
Thomas Vachuskaea2d9fd2015-09-23 13:13:25 -0700314 printf "${cdf} : no such cell\n" && return 1
315 fi
Ayaka Koshibea9985132014-12-18 14:55:22 -0800316
Thomas Vachuskaea2d9fd2015-09-23 13:13:25 -0700317 if [ -z "${EDITOR}" ] || [ -x "$(which ${EDITOR})" ]; then
318 unset EDITOR && vi ${cpath}${cdf}
319 else
320 $EDITOR ${cpath}${cdf}
321 fi
322 ($apply) && cell ${cdf}
Ayaka Koshibea9985132014-12-18 14:55:22 -0800323}
Ayaka Koshibeaec629622015-01-05 20:33:29 -0800324
Thomas Vachuska9507d2a2015-12-01 20:45:57 -0800325# Autocomplete for certain utilities
Ayaka Koshibeaec629622015-01-05 20:33:29 -0800326. ${ONOS_ROOT}/tools/test/bin/ogroup-opts
Thomas Vachuska9507d2a2015-12-01 20:45:57 -0800327
328
329# Load AT&T MPLS topo GEO data
Ayaka Koshibe77acdc72016-02-03 17:19:24 -0800330alias atttopo='onos-netcfg $OCI $ONOS_ROOT/tools/test/topos/attmpls-cfg.json'
Simon Huntcf7e3b52016-02-29 23:26:16 -0800331
332# Load UK topo GEO data
333alias uktopo='onos-netcfg $OCI $ONOS_ROOT/tools/test/topos/uk-cfg.json'