| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | ## |
| 4 | # Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U. |
| tierno | 9a61c6b | 2016-09-08 10:57:02 +0200 | [diff] [blame] | 5 | # This file is part of openvim |
| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 6 | # All Rights Reserved. |
| 7 | # |
| 8 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 9 | # not use this file except in compliance with the License. You may obtain |
| 10 | # a copy of the License at |
| 11 | # |
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | # |
| 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 16 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 17 | # License for the specific language governing permissions and limitations |
| 18 | # under the License. |
| 19 | # |
| 20 | # For those usages not covered by the Apache License, Version 2.0 please |
| 21 | # contact with: nfvlabs@tid.es |
| 22 | ## |
| 23 | |
| 24 | # |
| 25 | #author Alfonso Tierno |
| 26 | # |
| 27 | #script to test openflow connector with the creation of rules |
| 28 | # |
| 29 | |
| 30 | function usage(){ |
| 31 | echo -e "usage: ${BASH_SOURCE[0]} [OPTIONS] \n test openflow connector " |
| 32 | echo -e " OPTIONS:" |
| 33 | echo -e " -f --force does not prompt for confirmation" |
| 34 | echo -e " -v --same-vlan use this if the parameter 'of_controller_nets_with_same_vlan'" |
| 35 | echo -e " is not false at openvimd.cfg to avoid test unrealizable openflow nets" |
| 36 | echo -e " -d --debug show debug information at each command. It is quite verbose" |
| 37 | echo -e " -h --help shows this help" |
| 38 | } |
| 39 | |
| 40 | |
| 41 | function delete_and_exit(){ |
| 42 | echo |
| 43 | [[ $force != y ]] && read -e -p " Press enter to delete the deployed things " kk |
| 44 | echo |
| 45 | for f in $TODELETE |
| 46 | do |
| 47 | if [[ $f == restore ]] |
| 48 | then |
| 49 | printf "%-50s" "restoring back old rules: " |
| 50 | result=`openflow install ./test_openflow_old_rules.bk $debug ` |
| 51 | if [[ $? != 0 ]] |
| 52 | then |
| 53 | echo "FAIL cannot install old rules:" |
| 54 | echo "$result" |
| 55 | else |
| 56 | rm ./test_openflow_old_rules.bk |
| 57 | echo OK "./test_openflow_old_rules.bk deleted" |
| 58 | fi |
| 59 | else |
| 60 | printf "%-50s" "removing $f rule: " |
| 61 | result=`openflow delete $f -f $debug` |
| 62 | [[ $? != 0 ]] && echo "FAIL cannot delete" && echo "$result" || echo OK |
| 63 | fi |
| 64 | done |
| 65 | exit $1 |
| 66 | } |
| 67 | |
| 68 | force=n |
| 69 | same_vlan=n |
| 70 | debug="" |
| 71 | TODELETE="" |
| 72 | #detect if is called with a source to use the 'exit'/'return' command for exiting |
| 73 | [[ ${BASH_SOURCE[0]} != $0 ]] && echo "Do not execute this script as SOURCE" >&2 && return 1 |
| 74 | |
| 75 | #check correct arguments |
| 76 | for param in $* |
| 77 | do |
| 78 | if [[ $param == -h ]] || [[ $param == --help ]] |
| 79 | then |
| 80 | usage |
| 81 | exit 0 |
| 82 | elif [[ $param == -d ]] || [[ $param == --debug ]] |
| 83 | then |
| 84 | debug="--debug" |
| 85 | elif [[ $param == -v ]] || [[ $param == --same-vlan ]] |
| 86 | then |
| 87 | same_vlan=y |
| 88 | elif [[ $param == -f ]] || [[ $param == --force ]] |
| 89 | then |
| 90 | force=y |
| 91 | else |
| 92 | echo "invalid argument '$param'?. See $0 --help" && exit 1 |
| 93 | fi |
| 94 | done |
| 95 | |
| 96 | #detect if environment variables are set |
| 97 | fail="" |
| 98 | [[ -z $OF_CONTROLLER_TYPE ]] && echo "OF_CONTROLLER_TYPE not defined" >&2 && fail=1 |
| 99 | [[ -z $OF_CONTROLLER_IP ]] && echo "OF_CONTROLLER_IP not defined" >&2 && fail=1 |
| 100 | [[ -z $OF_CONTROLLER_PORT ]] && echo "OF_CONTROLLER_PORT not defined" >&2 && fail=1 |
| 101 | [[ -z $OF_CONTROLLER_DPID ]] && echo "OF_CONTROLLER_DPID not defined" >&2 && fail=1 |
| 102 | [[ -n $fail ]] && exit 1 |
| 103 | |
| 104 | |
| 105 | export _exit=delete_and_exit |
| 106 | if [[ $force != y ]] |
| 107 | then |
| 108 | echo " This will remove temporally the existing openflow rules and restored back a the end" |
| 109 | read -e -p "Press enter to continue, CTRL+C to abort " kk |
| 110 | fi |
| 111 | |
| 112 | |
| 113 | printf "%-50s" "obtain port list: " |
| 114 | result=`openflow port-list $debug | gawk '/^ /{print substr($1,0,length($1)-1)}'` |
| 115 | [[ $? != 0 ]] && echo "FAIL" && echo "$result" && $_exit 1 |
| 116 | ports=`echo $result | wc -w` |
| 117 | [[ $ports -lt 4 ]] && echo "FAIL not enough ports managed by this DPID, needed at least 4" && $_exit 1 |
| 118 | echo OK $ports ports |
| 119 | port0=`echo $result | cut -d" " -f1` |
| 120 | port1=`echo $result | cut -d" " -f2` |
| 121 | port2=`echo $result | cut -d" " -f3` |
| 122 | port3=`echo $result | cut -d" " -f4` |
| 123 | |
| 124 | |
| 125 | printf "%-50s" "saving the current rules: " |
| 126 | openflow list $debug > ./test_openflow_old_rules.bk |
| 127 | [[ $? != 0 ]] && echo "FAIL cannot obtain existing rules" && $_exit 1 |
| 128 | echo OK "> ./test_openflow_old_rules.bk" |
| 129 | |
| 130 | printf "%-50s" "clearing all current rules: " |
| 131 | openflow clear -f $debug |
| 132 | [[ $? != 0 ]] && echo "FAIL cannot clear existing rules" && $_exit 1 |
| 133 | result=`openflow list | wc -l` |
| 134 | [[ $result != 1 ]] && echo "FAIL rules not completely cleared" && $_exit 1 |
| 135 | echo OK |
| 136 | TODELETE="restore" |
| 137 | |
| 138 | printf "%-50s" "clearing again all rules: " |
| 139 | openflow clear -f $debug |
| 140 | [[ $? != 0 ]] && echo "FAIL when there are not any rules" && $_exit 1 |
| 141 | result=`openflow list | wc -l` |
| 142 | [[ $result != 1 ]] && echo "FAIL rules not completely cleared" && $_exit 1 |
| 143 | echo OK |
| 144 | TODELETE="restore" |
| 145 | |
| 146 | |
| 147 | printf "%-50s" "new rule vlan,mac -> no vlan: " |
| 148 | rule_name=fromVlanMac_to_NoVlan1 |
| tierno | d03ce28 | 2016-12-02 14:40:59 +0100 | [diff] [blame] | 149 | rule_name=`openflow add $rule_name --priority 1000 --matchmac "aa:bb:cc:dd:ee:ff" --matchvlan 500 --inport $port0 --stripvlan --out $port1 $debug --print-id` |
| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 150 | [[ $? != 0 ]] && echo "FAIL cannot insert new rule" && $_exit 1 |
| 151 | expected="$OF_CONTROLLER_DPID 1000 $rule_name $port0 aa:bb:cc:dd:ee:ff 500 vlan=None,out=$port1" |
| 152 | result=`openflow list | grep $rule_name` |
| 153 | [[ $? != 0 ]] && echo "FAIL rule bad inserted" && $_exit 1 |
| 154 | result=`echo $result` #remove blanks |
| 155 | [[ "$result" != "$expected" ]] && echo "FAIL" && echo " expected: $expected\n obtained: $result" && $_exit 1 |
| 156 | echo OK $rule_name |
| 157 | TODELETE="$rule_name $TODELETE" |
| 158 | |
| 159 | printf "%-50s" "new rule mac -> vlan: " |
| 160 | rule_name=fromMac_to_Vlan2 |
| tierno | d03ce28 | 2016-12-02 14:40:59 +0100 | [diff] [blame] | 161 | rule_name=`openflow add $rule_name --priority 1001 --matchmac "ff:ff:ff:ff:ff:ff" --inport $port1 --setvlan 501 --out $port2 --out $port3 $debug --print-id` |
| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 162 | [[ $? != 0 ]] && echo "FAIL cannot insert new rule" && $_exit 1 |
| 163 | expected="$OF_CONTROLLER_DPID 1001 $rule_name $port1 ff:ff:ff:ff:ff:ff any vlan=501,out=$port2,out=$port3" |
| 164 | result=`openflow list | grep $rule_name` |
| 165 | [[ $? != 0 ]] && echo "FAIL rule bad inserted" && $_exit 1 |
| 166 | result=`echo $result` #remove blanks |
| 167 | [[ "$result" != "$expected" ]] && echo "FAIL" && echo " expected: $expected\n obtained: $result" && $_exit 1 |
| 168 | echo OK $rule_name |
| 169 | TODELETE="$rule_name $TODELETE" |
| 170 | |
| 171 | printf "%-50s" "new rule None -> None: " |
| 172 | rule_name=fromNone_to_None |
| tierno | d03ce28 | 2016-12-02 14:40:59 +0100 | [diff] [blame] | 173 | rule_name=`openflow add $rule_name --priority 1002 --inport $port2 --out $port0 $debug --print-id` |
| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 174 | [[ $? != 0 ]] && echo "FAIL cannot insert new rule" && $_exit 1 |
| 175 | expected="$OF_CONTROLLER_DPID 1002 $rule_name $port2 any any out=$port0" |
| 176 | result=`openflow list | grep $rule_name` |
| 177 | [[ $? != 0 ]] && echo "FAIL rule bad inserted" && $_exit 1 |
| 178 | result=`echo $result` #remove blanks |
| 179 | [[ "$result" != "$expected" ]] && echo "FAIL" && echo " expected: $expected\n obtained: $result" && $_exit 1 |
| 180 | echo OK $rule_name |
| 181 | TODELETE="$rule_name $TODELETE" |
| 182 | |
| 183 | printf "%-50s" "new rule vlan -> vlan: " |
| 184 | rule_name=fromVlan_to_Vlan1 |
| tierno | d03ce28 | 2016-12-02 14:40:59 +0100 | [diff] [blame] | 185 | rule_name=`openflow add $rule_name --priority 1003 --matchvlan 504 --inport $port3 --setvlan 505 --out $port0 $debug --print-id` |
| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 186 | [[ $? != 0 ]] && echo "FAIL cannot insert new rule" && $_exit 1 |
| 187 | expected="$OF_CONTROLLER_DPID 1003 $rule_name $port3 any 504 vlan=505,out=$port0" |
| 188 | result=`openflow list | grep $rule_name` |
| 189 | [[ $? != 0 ]] && echo "FAIL rule bad inserted" && $_exit 1 |
| 190 | result=`echo $result` #remove blanks |
| 191 | [[ "$result" != "$expected" ]] && echo "FAIL" && echo " expected: $expected\n obtained: $result" && $_exit 1 |
| 192 | echo OK $rule_name |
| 193 | TODELETE="$rule_name $TODELETE" |
| 194 | |
| 195 | |
| 196 | if [[ $same_vlan == n ]] |
| 197 | then |
| 198 | |
| 199 | printf "%-50s" "new rule Vlan -> Vlan_Vlan: " |
| 200 | rule_name=fromVlan_to_Vlan1Vlan1 |
| tierno | d03ce28 | 2016-12-02 14:40:59 +0100 | [diff] [blame] | 201 | rule_name=`openflow add $rule_name --priority 1005 --inport $port3 --matchvlan 505 --setvlan 510 --out $port0 --setvlan 511 --out $port1 --stripvlan --out=$port2 $debug --print-id` |
| tierno | f7aa8c4 | 2016-09-06 16:43:04 +0200 | [diff] [blame] | 202 | [[ $? != 0 ]] && echo "FAIL cannot insert new rule" && $_exit 1 |
| 203 | expected="$OF_CONTROLLER_DPID 1005 $rule_name $port3 any 505 vlan=510,out=$port0,vlan=511,out=$port1,vlan=None,out=$port2" |
| 204 | result=`openflow list | grep $rule_name` |
| 205 | [[ $? != 0 ]] && echo "FAIL rule bad inserted" && $_exit 1 |
| 206 | result=`echo $result` #remove blanks |
| 207 | [[ "$result" != "$expected" ]] && echo "FAIL" && echo " expected: $expected\n obtained: $result" && $_exit 1 |
| 208 | echo OK $rule_name |
| 209 | TODELETE="$rule_name $TODELETE" |
| 210 | |
| 211 | fi |
| 212 | |
| 213 | echo |
| 214 | echo DONE |
| 215 | |
| 216 | $_exit 0 |
| 217 | |