Repo structure modification
[functest.git] / functest / opnfv_tests / 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 start_time = time.time()
40
41
42 def PreConfig():
43     logger.info("Testcase 1 : Prerequisites configuration for SFC")
44     logger.info("1.1 Creation of Auth-Token")
45     check(Sfc_obj.getToken, OK, "Creation of Token")
46     logger.info("1.2 Creation of Network")
47     check(Sfc_obj.createNetworks, CREATED, "Creation of network")
48     logger.info("1.3 Creation of Subnetwork")
49     check(Sfc_obj.createSubnets, CREATED, "Creation of Subnetwork")
50
51
52 def CreateNodes():
53     logger.info("Testcase 2 : Creation of 3 VNF Nodes and Attaching Ports")
54     logger.info("2.1 Creation of Ports")
55     check(Sfc_obj.createPorts, CREATED, "Creation of Port")
56     logger.info("2.2 Creation of VM-Compute-Node")
57     check(Sfc_obj.createVm, ACCEPTED, "Creation of VM")
58     logger.info("2.3 Check VM Status")
59     check(Sfc_obj.checkVmState, OK, "Vm statue check")
60     logger.info("2.4 Router Creation")
61     check(Sfc_obj.createRouter, CREATED, "Creation of Router")
62     logger.info("2.5 Attachement of Interface to VM")
63     check(Sfc_obj.attachInterface, OK, "Interface attached to VM")
64     logger.info("2.6 Attachement of FLoating Ip to VM")
65     check(Sfc_obj.addFloatingIp, ACCEPTED, "Floating Ip attached to VM")
66
67
68 def ConfigSfc():
69     logger.info(
70         "Testcase 3 : Configure SFC [Portair,PortGroup,Flow classifer]")
71     logger.info("3.1 Creation of Port Pair")
72     check(Sfc_obj.createPortPair, CREATED, "Creation of Port Pair")
73     logger.info("3.2 Getting the  Port Pair ID")
74     check(Sfc_obj.getPortPair, OK, "Getting Port Pair ID")
75     logger.info("3.3 Creation of Port Pair Group")
76     check(Sfc_obj.createPortGroup, CREATED, "Creation of Port Pair Group")
77     logger.info("3.4 Getting Port Pair Group ID ")
78     check(Sfc_obj.getPortGroup, OK, "Getting Port Pair Group ID")
79     logger.info("3.5 Creation of Flow Classifier")
80     check(Sfc_obj.createFlowClassifier, CREATED, "Creation of Flow Classifier")
81     logger.info(
82         "Testcase 4 : Configure Port Chain and verify flows are added")
83     logger.info("4.1 Creation of Port Chain")
84     check(Sfc_obj.createPortChain, CREATED,  "Creation of Port Chain")
85
86
87 def VerifySfcTraffic():
88     status = "PASS"
89     logger.info("Testcase 5 : Verify  traffic with VNF node.")
90     if (Sfc_obj.loginToVM() == "1"):
91         logger.info("SFC function Working")
92     else:
93         logger.error("SFC function not working")
94         status = "FAIL"
95
96     logger.info("Testcase 6 : Remove the Port Chain and Verify the traffic")
97     if (Sfc_obj.deletePortChain() == NO_CONTENT):
98         if (Sfc_obj.loginToVM() == "0"):
99             logger.info("SFC function is removed Successfully")
100         else:
101             logger.error("SFC function not Removed. Have some problem")
102             status = "FAIL"
103         if (Sfc_obj.deleteFlowClassifier() == NO_CONTENT):
104             if (Sfc_obj.deletePortGroup() == NO_CONTENT):
105                 if (Sfc_obj.deletePortPair() == NO_CONTENT):
106                     logger.info(
107                         "SFC configuration is deleted successfully")
108                 else:
109                     logger.error("Port pair is deleted successfully")
110                     status = "FAIL"
111             else:
112                 logger.error("Port Group is NOT deleted successfully")
113                 status = "FAIL"
114         else:
115             logger.error("Flow classifier is NOT deleted successfully")
116             status = "FAIL"
117     else:
118         logger.error("PortChain configuration is NOT deleted successfully")
119         status = "FAIL"
120     if (status == "FAIL"):
121         fail("Traffic for SFC is NOT verified successfully")
122
123
124 def CleanUp():
125     logger.info("Testcase 7 : Cleanup")
126     if (Sfc_obj.cleanup() == NO_CONTENT):
127         logger.info("CleanUp is successfull")
128     else:
129         logger.error("CleanUp is NOT successfull")
130
131
132 def check(method, criteria, msg):
133     if (method() == criteria):
134         logger.info(msg + 'is Successful')
135     else:
136         fail(msg + 'is not successful')
137
138
139 def fail(fail_info):
140     logger.error(fail_info)
141     CleanUp()
142     PushDB("FAIL", fail_info)
143     exit(-1)
144
145
146 def PushDB(status, info):
147     logger.info("Summary :")
148     try:
149         logger.debug("Push ONOS SFC results into DB")
150         stop_time = time.time()
151
152         # ONOS SFC success criteria = all tests OK
153         duration = round(stop_time - start_time, 1)
154         logger.info("Result is " + status)
155         functest_utils.push_results_to_db("functest",
156                                           "onos_sfc",
157                                           start_time,
158                                           stop_time,
159                                           status,
160                                           details={'duration': duration,
161                                                    'error': info})
162     except:
163         logger.error("Error pushing results into Database")
164
165
166 def main():
167     """Script to Test the SFC scenarios in ONOS."""
168     PreConfig()
169     CreateNodes()
170     ConfigSfc()
171     VerifySfcTraffic()
172     CleanUp()
173     PushDB("PASS", "")
174
175
176 if __name__ == '__main__':
177     main()