Bug 259 Support port_security in Openmano
[osm/SO.git] / examples / ping_pong_ns / stand_up_ping_pong
1 #!/bin/bash
2
3 # arguments
4 if [ -z $1 ] 
5 then 
6     echo "must supply ip for launchpad"
7     exit -1
8 else
9     lp_ip=${1}
10 fi
11
12 username=$(whoami)
13
14 # make sure we're in a rift shell
15 if [ -z $RIFT_ROOT ] 
16 then 
17     echo "must be in a rift-shell"
18     exit -1
19 fi
20
21 # make sure the system is up
22 system_is_up() {
23     response=$(curl --silent --insecure \
24                     -o /dev/null \
25                     --write-out "%{http_code}"\
26                     --user admin:admin \
27                     https://${lp_ip}:8008/api/config/launchpad-config \
28                     --request GET \
29             )
30
31    if [ ${response} -eq 200 ]
32    then
33        return 0
34    else
35        if [ ${response} -eq 404 ]
36        then
37            # not running a launchpad!
38            echo "riftware is running on ${lp_ip} but there is no launchpad-config"
39            exit -1
40        fi
41        return 1
42    fi
43 }
44
45 echo -n "wait for system"
46 while ! system_is_up
47 do
48     echo -n "."
49     sleep 5s
50 done
51
52 echo "system is up"
53
54 # setup the openstack account
55 echo -n "adding account"
56
57 post_json_to_rwrest() {
58    if [ -z "$1" ]
59    then
60      echo "must supply url"
61      exit -1
62    else
63        url=$1
64    fi
65
66    if [ -z "$2" ]
67    then
68      echo "must supply payload"
69      exit -1
70    else
71        payload="${2}"
72    fi
73
74    response=$(curl --silent --insecure \
75                    --header "content-type:application/vnd.yang.data+json" \
76                    --header "Accept:application/vnd.yang.data+json" \
77                    --user admin:admin \
78                    https://${lp_ip}:8008${url} \
79                    --request POST --data "${payload}" \
80            )
81
82     added_account=$(echo "${response}" | grep -e \"success|ok\" | wc -l)
83     already_exists=$(echo "${response}" | grep \"data-exists\" | wc -l)
84     success=$((added_account + already_exists))
85 }
86
87 account_payload=" {
88   \"account\": [
89     {
90       \"name\": \"OS\",
91       \"account-type\": \"openstack\",
92       \"openstack\": {
93         \"auth_url\": \"http://engstack.eng.riftio.com:5000/v3/\",
94         \"secret\": \"mypasswd\",
95         \"key\": \"${username}_automation\",
96         \"tenant\": \"${username}_automation\",
97         \"mgmt-network\": \"private\"
98       }
99     }
100   ]
101 }"
102
103 post_json_to_rwrest "/api/config/cloud/account" "${account_payload}"
104
105 if [ ${success} -ne 1 ];
106 then
107     echo -en "\r" # clear pending line
108     echo "failed to add cloud account:"
109     echo ${response}
110     exit 0
111 else
112     echo " success"
113 fi
114
115 # onboard descriptors
116 cd $RIFT_BUILD/modules/core/mano/src/core_mano-build/examples/ping_pong_ns
117
118 wait_for_package() {
119    if [ -z "$1" ]
120    then
121      echo "must supply transaction id to wait for"
122      exit -1
123    fi
124
125    response=$(curl --silent --insecure https://${lp_ip}:4567/api/upload/${transaction_id}/state)
126    transaction_state=$(echo ${response} | awk -F "status" '{print $2}' | awk '{print $2}')
127    transaction_state=${transaction_state:1:-2}
128    
129    if [ ${transaction_state} == "pending" ];
130    then
131        return 0
132    else
133        return 1
134    fi
135 }
136
137 upload_package() {
138    if [ -z "$1" ]
139    then
140      echo "must supply package to upload"
141      exit -1
142    else
143        package=$1
144    fi
145
146    echo -n "upload ${package} package"
147
148    response=$(curl --silent --insecure -F "descriptor=@${package}" https://${lp_ip}:4567/api/upload)
149    transaction_id=$(echo ${response} | awk '{print $2}')
150    transaction_id=${transaction_id:1:-2}
151
152    while wait_for_package transaction_id
153    do
154        echo -n "."
155        sleep 1s
156    done
157    if [ ${transaction_state} == "failure" ];
158    then
159        echo "failed"
160        status=1
161    else
162        echo "success"
163        status=0
164    fi
165
166 }
167
168 upload_package "ping_vnfd.tar.gz"
169 ping_status=${status}
170 upload_package "pong_vnfd.tar.gz"
171 pong_status=${status}
172
173 success=$((ping_status + pong_status))
174
175 if [ ${success} -ne 0 ];
176 then
177     echo -en "\r" # clear pending line
178     echo "cannot on-board nsd when a vnfd fails"
179     exit -1
180 fi
181
182 upload_package "ping_pong_nsd.tar.gz"
183 if [ ${status} -ne 0 ];
184 then
185     echo -en "\r" # clear pending line
186     echo "failed to on-board nsd"
187     exit -1
188 fi
189
190 # instantiate ping_pong_nsd
191 echo "instantiate ping pong nsd"
192
193 tag=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 4 | head -n 1)
194
195 tmpdir="/tmp/${tag}"
196 mkdir ${tmpdir}
197
198 tar -xf ping_pong_nsd.tar.gz -C ${tmpdir}
199
200 nsdfile="${tmpdir}/ping_pong_nsd/ping_pong_nsd.yaml"
201
202 nsd_id=$(cat ${nsdfile} | grep "nsd:id" | head -n1 | awk '{print $2}')
203
204 rm -r ${tmpdir}
205
206 nsr_id=$(cat /proc/sys/kernel/random/uuid)
207 nsd_payload="{
208     \"nsr\":[
209         {
210             \"id\":\"${nsr_id}\",
211             \"nsd-ref\":\"${nsd_id}\",
212             \"name\":\"${username}-${tag}-ping-pong-nsd\",
213             \"short-name\":\"pingpong\",
214             \"description\":\"ping pong nsd instantiated by ${username} with tag ${tag}\",
215             \"admin-status\":\"ENABLED\",
216             \"cloud-account\":\"OS\"
217         }
218     ]
219 }"
220
221 post_json_to_rwrest "/api/config/ns-instance-config" "${nsd_payload}"
222
223 if [ ${success} -ne 1 ];
224 then
225     echo -en "\r" # clear pending line
226     echo "failed to instantiate nsd:"
227     echo ${response}
228     exit -1
229 else
230     echo " success"
231 fi
232