Add test folder to the smo package installation
[it/dep.git] / smo-install / test / apex-policy-test / apex-policy-test.sh
1 #!/bin/bash
2
3 ###
4 # ============LICENSE_START=======================================================
5 # ORAN SMO Package
6 # ================================================================================
7 # Copyright (C) 2021 AT&T Intellectual Property. All rights
8 #                             reserved.
9 # ================================================================================
10 # Licensed under the Apache License, Version 2.0 (the "License");
11 # you may not use this file except in compliance with the License.
12 # You may obtain a copy of the License at
13 #
14 # http://www.apache.org/licenses/LICENSE-2.0
15 #
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS,
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 # See the License for the specific language governing permissions and
20 # limitations under the License.
21 # ============LICENSE_END============================================
22 # ===================================================================
23 #
24 ###
25 pap_port=6969
26 api_port=6969
27 dmaap_port=3904
28 sdnc_port=8282
29
30 pap_url=`kubectl get services policy-pap -n onap |grep policy-pap | awk '{print $3}'`:$pap_port
31 echo "Policy pap url: $pap_url"
32
33 api_url=`kubectl get services policy-api -n onap |grep policy-api | awk '{print $3}'`:$api_port
34 echo "Policy api url: $api_url"
35
36 dmaap_url=`kubectl get services message-router -n onap |grep message-router | awk '{print $3}'`:$dmaap_port
37 echo "Dmaap url: $dmaap_url"
38
39 sdnc_url=`kubectl get services -n onap | grep sdnc-oam | awk  '{print $3}'`:$sdnc_port
40 echo "SDNC url: $sdnc_url"
41 echo -e "\n"
42
43 verifyApexPolicyStatus(){
44     for i in {1..60}; do
45         curl  -s -o /dev/null -X POST -H accept:application/json -H Content-Type:application/json "http://$dmaap_url/events/unauthenticated.SEC_FAULT_OUTPUT/" -d @./data/LinkFailureEvent.json
46         sleep 3
47         res=`kubectl logs onap-policy-apex-pdp-0 -n onap | grep "Task Selection Execution: 'LinkMonitorPolicy:0.0.1:NULL:LinkFailureOrClearedState'" | wc -l`
48         if [[ $res != 0 ]]; then
49             echo -e "LinkFailureEvent sent to Dmaap\n"
50             break;
51         else
52             sleep 2
53         fi
54     done
55 }
56
57 checkStatus(){
58         echo "res:$1"
59         if [ "$1" == "$2" ]; then
60             echo -e "$3\n"
61         else
62             echo -e "Result is not as expected: $2\n"
63             exit;
64         fi
65 }
66
67 checkPolicyStatus(){
68     for i in {1..60}; do
69         res=$(curl -sk -u 'healthcheck:zb!XztG34' -X GET "https://$pap_url/policy/pap/v1/components/healthcheck")
70         apex_info=$(echo $res| cut -d'{' -f 4)
71
72         echo "Verify policy Pap"
73         if [[ $res == *"url\":\"https://$1:6969/policy/pap/v1/healthcheck\",\"healthy\":true"* ]]; then
74           echo "Policy Pap is ready"
75           echo "Verify policy api"
76           if [[ $res == *"url\":\"https://$2:6969/policy/api/v1/healthcheck\",\"healthy\":true"* ]]; then
77             echo "Policy API is ready"
78             echo "Verify policy Apex pdp"
79             if [[ $apex_info == *"instanceId\":\"apex"*  && $apex_info == *"healthy\":\"HEALTHY"* ]]; then
80               echo "Policy APEX pdp is ready"
81               break;
82             else
83               echo "Policy Apex pdp not ready yet. Wait for a while and retry."
84               sleep $i
85             fi
86           else
87             echo "Policy api not ready yet. Wait for a while and retry."
88             sleep $i
89           fi
90         else
91           echo "Policy Pap not ready yet. Wait for a while and retry."
92           sleep $i
93         fi
94     done
95 }
96
97 checkPolicyDeployment (){
98   res=$(curl -sk -u 'healthcheck:zb!XztG34' -X GET "https://$pap_url/policy/pap/v1/policies/status")
99   str1="onap.policies.native.apex.LinkMonitor\",\"version\":\"1.0.0\"},\"policyType\":{\"name\":\"onap.policies.native.Apex\",\"version\":\"1.0.0\"},\"deploy\":true"
100         if [[ $res == *"$str1"* ]]; then
101           echo "Policy successfully deployed"
102         else
103           echo "Policy deployment failed"
104           exit;
105         fi
106 }
107
108 echo "Create topic:"
109 curl -sk -X POST "http://$dmaap_url/topics/create" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{  \"topicName\": \"unauthenticated.SEC_FAULT_OUTPUT\",  \"topicDescription\": \"test topic\",  \"partitionCount\": 1,  \"replicationCnCount\": 1,  \"transactionEnabled\": \"false\"}"
110 echo "Get topics:"
111 curl -sk http://$dmaap_url/topics/listAll
112 echo -e "\n"
113
114 echo "Policy component healthcheck:"
115 pap_pod=`kubectl get pods -n onap | grep onap-policy-pap | awk  '{print $1}'`
116 api_pod=`kubectl get pods -n onap | grep onap-policy-api | awk  '{print $1}'`
117 checkPolicyStatus $pap_pod $api_pod
118 echo -e "\n"
119
120 echo "Create policy:"
121 res=`curl -sk -u 'healthcheck:zb!XztG34' -X POST "https://$api_url/policy/api/v1/policytypes/onap.policies.native.Apex/versions/1.0.0/policies" -H "Accept: application/json" -H "Content-Type: application/json" -d @./data/ToscaPolicy.json`
122 res2=`curl -sk -u 'healthcheck:zb!XztG34' -X GET "https://$api_url/policy/api/v1/policytypes/onap.policies.native.Apex/versions/1.0.0/policies/onap.policies.native.apex.LinkMonitor/versions/1.0.0"`
123 if [[ $res2 ]]; then
124     echo "Policy created successfully"
125 fi
126 echo -e "\n"
127
128 echo "Deploy the policy to apex-pdp via Policy PAP:"
129 curl -sk -u 'healthcheck:zb!XztG34' -X POST "https://$pap_url/policy/pap/v1/pdps/policies" -H "Accept: application/json" -H "Content-Type: application/json" -d @./data/DeployPolicyPAP.json
130 echo -e "\n"
131
132 echo "Verify policy deployed:"
133 checkPolicyDeployment
134 echo -e "\n"
135
136 echo "Check O-du/O-ru status"
137 res=$(curl -sk -H "Authorization: Basic YWRtaW46S3A4Yko0U1hzek0wV1hsaGFrM2VIbGNzZTJnQXc4NHZhb0dHbUp2VXkyVQ==" -X GET "http://$sdnc_url/rests/data/network-topology:network-topology/topology=topology-netconf/node=o-du-1122/yang-ext:mount/o-ran-sc-du-hello-world:network-function/du-to-ru-connection=o-ru-11221")
138 expected="{\"o-ran-sc-du-hello-world:du-to-ru-connection\":[{\"name\":\"o-ru-11221\",\"operational-state\":\"ENABLED\",\"administrative-state\":\"LOCKED\",\"status\":\"disconnected\"}]}"
139 checkStatus $res $expected "O-ru has status LOCKED"
140
141 echo -e "\n"
142 echo "Wait for a while for Apex engine to be ready before sending Dmaap event"
143 verifyApexPolicyStatus
144
145 echo -e "\n"
146 echo "Wait for a while and check O-du/O-ru status again"
147 sleep 5
148 res=$(curl -sk -H "Authorization: Basic YWRtaW46S3A4Yko0U1hzek0wV1hsaGFrM2VIbGNzZTJnQXc4NHZhb0dHbUp2VXkyVQ==" -X GET "http://$sdnc_url/rests/data/network-topology:network-topology/topology=topology-netconf/node=o-du-1122/yang-ext:mount/o-ran-sc-du-hello-world:network-function/du-to-ru-connection=o-ru-11221")
149 expected="{\"o-ran-sc-du-hello-world:du-to-ru-connection\":[{\"name\":\"o-ru-11221\",\"operational-state\":\"ENABLED\",\"administrative-state\":\"UNLOCKED\",\"status\":\"disconnected\"}]}"
150 checkStatus $res $expected "O-ru has status UNLOCKED"
151