blob: 2c9b5041cb41adde069f49954e756a01efc43441 [file] [log] [blame]
tiernof7aa8c42016-09-06 16:43:04 +02001#!/bin/bash
2
3##
4# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
tierno9a61c6b2016-09-08 10:57:02 +02005# This file is part of openvim
tiernof7aa8c42016-09-06 16:43:04 +02006# 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
30function 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
41function 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
68force=n
69same_vlan=n
70debug=""
71TODELETE=""
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
76for param in $*
77do
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
94done
95
96#detect if environment variables are set
97fail=""
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
105export _exit=delete_and_exit
106if [[ $force != y ]]
107then
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
110fi
111
112
113printf "%-50s" "obtain port list: "
114result=`openflow port-list $debug | gawk '/^ /{print substr($1,0,length($1)-1)}'`
115[[ $? != 0 ]] && echo "FAIL" && echo "$result" && $_exit 1
116ports=`echo $result | wc -w`
117[[ $ports -lt 4 ]] && echo "FAIL not enough ports managed by this DPID, needed at least 4" && $_exit 1
118echo OK $ports ports
119port0=`echo $result | cut -d" " -f1`
120port1=`echo $result | cut -d" " -f2`
121port2=`echo $result | cut -d" " -f3`
122port3=`echo $result | cut -d" " -f4`
123
124
125printf "%-50s" "saving the current rules: "
126openflow list $debug > ./test_openflow_old_rules.bk
127[[ $? != 0 ]] && echo "FAIL cannot obtain existing rules" && $_exit 1
128echo OK "> ./test_openflow_old_rules.bk"
129
130printf "%-50s" "clearing all current rules: "
131openflow clear -f $debug
132[[ $? != 0 ]] && echo "FAIL cannot clear existing rules" && $_exit 1
133result=`openflow list | wc -l`
134[[ $result != 1 ]] && echo "FAIL rules not completely cleared" && $_exit 1
135echo OK
136TODELETE="restore"
137
138printf "%-50s" "clearing again all rules: "
139openflow clear -f $debug
140[[ $? != 0 ]] && echo "FAIL when there are not any rules" && $_exit 1
141result=`openflow list | wc -l`
142[[ $result != 1 ]] && echo "FAIL rules not completely cleared" && $_exit 1
143echo OK
144TODELETE="restore"
145
146
147printf "%-50s" "new rule vlan,mac -> no vlan: "
148rule_name=fromVlanMac_to_NoVlan1
tiernod03ce282016-12-02 14:40:59 +0100149rule_name=`openflow add $rule_name --priority 1000 --matchmac "aa:bb:cc:dd:ee:ff" --matchvlan 500 --inport $port0 --stripvlan --out $port1 $debug --print-id`
tiernof7aa8c42016-09-06 16:43:04 +0200150[[ $? != 0 ]] && echo "FAIL cannot insert new rule" && $_exit 1
151expected="$OF_CONTROLLER_DPID 1000 $rule_name $port0 aa:bb:cc:dd:ee:ff 500 vlan=None,out=$port1"
152result=`openflow list | grep $rule_name`
153[[ $? != 0 ]] && echo "FAIL rule bad inserted" && $_exit 1
154result=`echo $result` #remove blanks
155[[ "$result" != "$expected" ]] && echo "FAIL" && echo " expected: $expected\n obtained: $result" && $_exit 1
156echo OK $rule_name
157TODELETE="$rule_name $TODELETE"
158
159printf "%-50s" "new rule mac -> vlan: "
160rule_name=fromMac_to_Vlan2
tiernod03ce282016-12-02 14:40:59 +0100161rule_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`
tiernof7aa8c42016-09-06 16:43:04 +0200162[[ $? != 0 ]] && echo "FAIL cannot insert new rule" && $_exit 1
163expected="$OF_CONTROLLER_DPID 1001 $rule_name $port1 ff:ff:ff:ff:ff:ff any vlan=501,out=$port2,out=$port3"
164result=`openflow list | grep $rule_name`
165[[ $? != 0 ]] && echo "FAIL rule bad inserted" && $_exit 1
166result=`echo $result` #remove blanks
167[[ "$result" != "$expected" ]] && echo "FAIL" && echo " expected: $expected\n obtained: $result" && $_exit 1
168echo OK $rule_name
169TODELETE="$rule_name $TODELETE"
170
171printf "%-50s" "new rule None -> None: "
172rule_name=fromNone_to_None
tiernod03ce282016-12-02 14:40:59 +0100173rule_name=`openflow add $rule_name --priority 1002 --inport $port2 --out $port0 $debug --print-id`
tiernof7aa8c42016-09-06 16:43:04 +0200174[[ $? != 0 ]] && echo "FAIL cannot insert new rule" && $_exit 1
175expected="$OF_CONTROLLER_DPID 1002 $rule_name $port2 any any out=$port0"
176result=`openflow list | grep $rule_name`
177[[ $? != 0 ]] && echo "FAIL rule bad inserted" && $_exit 1
178result=`echo $result` #remove blanks
179[[ "$result" != "$expected" ]] && echo "FAIL" && echo " expected: $expected\n obtained: $result" && $_exit 1
180echo OK $rule_name
181TODELETE="$rule_name $TODELETE"
182
183printf "%-50s" "new rule vlan -> vlan: "
184rule_name=fromVlan_to_Vlan1
tiernod03ce282016-12-02 14:40:59 +0100185rule_name=`openflow add $rule_name --priority 1003 --matchvlan 504 --inport $port3 --setvlan 505 --out $port0 $debug --print-id`
tiernof7aa8c42016-09-06 16:43:04 +0200186[[ $? != 0 ]] && echo "FAIL cannot insert new rule" && $_exit 1
187expected="$OF_CONTROLLER_DPID 1003 $rule_name $port3 any 504 vlan=505,out=$port0"
188result=`openflow list | grep $rule_name`
189[[ $? != 0 ]] && echo "FAIL rule bad inserted" && $_exit 1
190result=`echo $result` #remove blanks
191[[ "$result" != "$expected" ]] && echo "FAIL" && echo " expected: $expected\n obtained: $result" && $_exit 1
192echo OK $rule_name
193TODELETE="$rule_name $TODELETE"
194
195
196if [[ $same_vlan == n ]]
197then
198
199 printf "%-50s" "new rule Vlan -> Vlan_Vlan: "
200 rule_name=fromVlan_to_Vlan1Vlan1
tiernod03ce282016-12-02 14:40:59 +0100201 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`
tiernof7aa8c42016-09-06 16:43:04 +0200202 [[ $? != 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
211fi
212
213echo
214echo DONE
215
216$_exit 0
217