28611982bca7dd407b08a8f224555b6d27a9aa45
[functest.git] / testcases / Controllers / ONOS / Sfc / Sfc.py
1 """Script to Test the SFC scenarios in ONOS."""
2 # !/usr/bin/python
3 #
4 # Copyright (c) CREATED5 All rights reserved
5 # This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # ###########################################################################
12 #                          OPNFV SFC Script
13 # **** Scripted by Antony Silvester  - antony.silvester@huawei.com ******
14 # ###########################################################################
15
16 # Testcase 1 : Prerequisites configuration for SFC
17 # Testcase 2 : Creation of 3 VNF Nodes and Attaching Ports
18 # Testcase 3 : Configure  SFC [Port pair,Port Group ,Flow classifer
19 # Testcase 4 : Configure Port Chain and verify the flows are added.
20 # Testcase 5 : Verify  traffic with VNF node.
21 # Testcase 6 : Remove the Port Chain and Verify the traffic.
22 # Testcase 7 : Cleanup
23 # ###########################################################################
24 #
25
26 import time
27 import functest.utils.functest_logger as ft_logger
28 import functest.utils.functest_utils as functest_utils
29 from Sfc_fun import Sfc_fun
30
31 logger = ft_logger.Logger("sfc").getLogger()
32 Sfc_obj = Sfc_fun()
33
34 OK = 200
35 CREATED = 201
36 ACCEPTED = 202
37 NO_CONTENT = 204
38
39
40 def PreConfig():
41     logger.info("Testcase 1 : Prerequisites configuration for SFC")
42     logger.info("1.1 Creation of Auth-Token")
43     if (Sfc_obj.getToken() == OK):
44         logger.info("Creation of Token is successfull")
45     else:
46         logger.error("  :  Creation of Token is NOT successfull")
47         fail()
48     logger.info("1.2 Creation of Network")
49     if (Sfc_obj.createNetworks() == CREATED):
50         logger.info("Creation of network is successfull")
51     else:
52         logger.error("  :  Creation of network is NOT successfull")
53         fail()
54     logger.info("1.3 Creation of Subnetwork")
55
56     if (Sfc_obj.createSubnets() == CREATED):
57         logger.info("Creation of Subnetwork is successfull")
58     else:
59         logger.error("  :  Creation of Subnetwork is NOT successfull")
60         fail()
61
62
63 def CreateNodes():
64     logger.info("Testcase 2 : Creation of 3 VNF Nodes and Attaching Ports")
65     logger.info("2.1 Creation of Ports")
66     if (Sfc_obj.createPorts() == CREATED):
67         logger.info("Creation of Port is successfull")
68     else:
69         logger.error("  :  Creation of Port is NOT successfull")
70         fail()
71     logger.info("2.2 Creation of VM-Compute-Node")
72     if (Sfc_obj.createVm() == ACCEPTED):
73         logger.info("Creation of VM is successfull")
74     else:
75         logger.error("  :  Creation of VM is NOT successfull")
76         fail()
77     logger.info("2.3 Check VM Status")
78     if (Sfc_obj.checkVmState() == OK):
79         logger.info("VM are in active state")
80     else:
81         logger.error("  :   VM is NOT Active")
82         fail()
83     logger.info("2.4 Router Creation")
84     if (Sfc_obj.createRouter() == CREATED):
85         logger.info("Creation of Router is Successful")
86     else:
87         logger.error("  :   Router Creation is NOT Successful")
88         fail()
89     logger.info("2.5 Attachement of Interface to VM")
90     if (Sfc_obj.attachInterface() == OK):
91         logger.info("Interface attached to VM")
92     else:
93         logger.error("  :   Interface NOT attached to VM")
94         fail()
95     logger.info("2.6 Attachement of FLoating Ip to VM")
96     if (Sfc_obj.addFloatingIp() == ACCEPTED):
97         logger.info("Floating Ip attached to VM SUccessful")
98     else:
99         logger.error("  :   Floating Ip NOT attached to VM ")
100         fail()
101
102
103 def ConfigSfc():
104     logger.info(
105         "Testcase 3 : Configure SFC [Portair,PortGroup,Flow classifer]")
106     logger.info("3.1 Creation of Port Pair")
107     if (Sfc_obj.createPortPair() == CREATED):
108         logger.info("Creation of Port pair is successful")
109     else:
110         logger.error("  :  Creation of Port pair is NOT successful")
111         fail()
112     logger.info("3.2 Getting the  Port Pair ID")
113     if (Sfc_obj.getPortPair() == OK):
114         logger.info("Port Pair ID is successfully got")
115     else:
116         logger.error("  :  UnSuccessfully got Port Pair ID")
117         fail()
118     logger.info("3.3 Creation of Port Pair Group")
119     if (Sfc_obj.createPortGroup() == CREATED):
120         logger.info("Creation of Port Pair Group is successful")
121     else:
122         logger.error("  :  Creation of Port Pair Group is NOT successful")
123         fail()
124     logger.info("3.4 Getting Port Pair Group ID ")
125     if (Sfc_obj.getPortGroup() == OK):
126         logger.info("Port Pair Group ID is successfully received")
127     else:
128         logger.error("  :  Port Pair Group ID is NOT successfully got")
129         fail()
130     logger.info("3.5 Creation of Flow Classifier")
131     if (Sfc_obj.createFlowClassifier() == CREATED):
132         logger.info("Creation of Flow Classifier is successful")
133     else:
134         logger.error("  :  Creation of Flow Classifier is NOT successful")
135         fail()
136     logger.info(
137         "Testcase 4 : Configure Port Chain and verify flows are added")
138     logger.info("4.1 Creation of Port Chain")
139     if (Sfc_obj.createPortChain() == CREATED):
140         logger.info("Creation of Port Chain is successful")
141     else:
142         logger.error("Creation of Port Chain is NOT successful")
143
144
145 def VerifySfcTraffic():
146     status = "PASS"
147     logger.info("Testcase 5 : Verify  traffic with VNF node.")
148     if (Sfc_obj.loginToVM() == "1"):
149         logger.info("SFC function Working")
150     else:
151         logger.error("  :  SFC function not working")
152         status = "FAIL"
153
154     logger.info("Testcase 6 : Remove the Port Chain and Verify the traffic")
155     if (Sfc_obj.deletePortChain() == NO_CONTENT):
156         if (Sfc_obj.loginToVM() == "0"):
157             logger.info("SFC function is removed Successfully")
158         else:
159             logger.error(":SFC function not Removed. Have some problem")
160             status = "FAIL"
161         if (Sfc_obj.deleteFlowClassifier() == NO_CONTENT):
162             if (Sfc_obj.deletePortGroup() == NO_CONTENT):
163                 if (Sfc_obj.deletePortPair() == NO_CONTENT):
164                     logger.info(
165                         "SFC configuration is deleted successfully")
166                 else:
167                     logger.error("  :  Port pair configuration is NOT\
168                                   deleted successfully")
169                     status = "FAIL"
170             else:
171                 logger.error("  :  Port Group configuration is NOT \
172                              deleted successfully")
173                 status = "FAIL"
174         else:
175                 logger.error("  :  Flow classifier configuration is NOT \
176                              deleted successfully")
177                 status = "FAIL"
178     else:
179         logger.error(":PortChain configuration is NOT deleted \
180                      successfully")
181         status = "FAIL"
182     if (status == "FAIL"):
183         fail()
184
185
186 def CleanUp():
187     logger.info("Testcase 7 : Cleanup")
188     if (Sfc_obj.cleanup() == NO_CONTENT):
189         logger.info("CleanUp is successfull")
190     else:
191         logger.error("  :  CleanUp is NOT successfull")
192
193
194 def fail():
195     CleanUp()
196     exit(-1)
197
198
199 def main():
200     """Script to Test the SFC scenarios in ONOS."""
201     start_time = time.time()
202     PreConfig()
203     CreateNodes()
204     ConfigSfc()
205     VerifySfcTraffic()
206     CleanUp()
207     status = "PASS"
208     logger.info("Summary :")
209     try:
210         logger.debug("Push ONOS SFC results into DB")
211         stop_time = time.time()
212
213         # ONOS SFC success criteria = all tests OK
214         duration = round(stop_time - start_time, 1)
215         logger.info("Result is " + status)
216         functest_utils.push_results_to_db("functest",
217                                           "onos_sfc",
218                                           start_time,
219                                           stop_time,
220                                           status,
221                                           details={'timestart': start_time,
222                                                    'duration': duration,
223                                                    'status': status})
224     except:
225         logger.error("Error pushing results into Database")
226
227 if __name__ == '__main__':
228     main()