blob: 0015e5af954f26a7e5fe59b6140d69e03d0d8f1b [file] [log] [blame]
Gilles Depatie1be639b2018-12-06 10:51:08 -05001#
2# Copyright 2018 the original author or authors.
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
16
17"""
18vOLT-HA Discovery Test Case module
19"""
20
21import time
22import os
23import commands
24import testCaseUtils
25import logging
26
27class Discovery(object):
28
29 """
30 This class implements voltha discovery test
31 """
32
33 def __init__(self):
34 self.dirs = {}
35 self.dirs ['log'] = None
36 self.dirs ['root'] = None
37 self.dirs ['voltha'] = None
38
39 self.__oltType = None
40 self.__onuType = None
41 self.__fields = []
42 self.__oltDeviceId = None
43 self.__onuDeviceIds = []
44 self.__peers = None
45
46 def dSetLogDirs(self, logDir):
47 testCaseUtils.configDirs(self, logDir)
48
49 def dConfigure(self, oltType, onuType):
50 self.__oltType = oltType
51 self.__onuType = onuType
52
53 def oltDiscovery(self):
54 logging.info('Olt Discovery')
55 statusLines = testCaseUtils.get_fields_from_grep_command(self, self.__oltType, 'voltha_devices_after_enable.log')
56 assert statusLines, 'No Olt listed under devices'
57 self.__fields = testCaseUtils.parseFields(statusLines)
58 self.__oltDeviceId = self.__fields[1].strip()
59 testCaseUtils.send_command_to_voltha_cli(testCaseUtils.getDir(self, 'log'),
60 'voltha_olt_device.log', 'device ' + self.__oltDeviceId, 'voltha_olt_ports.log', 'ports', 'voltha_olt_flows.log', 'flows')
61 testCaseUtils.printLogFile (self, 'voltha_olt_ports.log')
62 testCaseUtils.printLogFile (self, 'voltha_olt_flows.log')
63
64 def onuDiscovery(self):
65 logging.info('Onu Discovery')
66 statusLines = testCaseUtils.get_fields_from_grep_command(self, self.__onuType, 'voltha_devices_after_enable.log')
67 assert statusLines, 'No Onu listed under devices'
68 lines = statusLines.splitlines()
69 for line in lines:
70 self.__fields = testCaseUtils.parseFields(line)
71 onuDeviceId = self.__fields[1].strip()
72 self.__onuDeviceIds.append(onuDeviceId)
73 testCaseUtils.send_command_to_voltha_cli(testCaseUtils.getDir(self, 'log'),
74 'voltha_onu_device_' + str(self.__onuDeviceIds.index(onuDeviceId)) + '.log', 'device ' + onuDeviceId,
75 'voltha_onu_ports_' + str(self.__onuDeviceIds.index(onuDeviceId)) + '.log', 'ports',
76 'voltha_onu_flows_' + str(self.__onuDeviceIds.index(onuDeviceId)) + '.log', 'flows')
77 testCaseUtils.printLogFile (self, 'voltha_onu_ports_' + str(self.__onuDeviceIds.index(onuDeviceId)) + '.log')
78 testCaseUtils.printLogFile (self, 'voltha_onu_flows_' + str(self.__onuDeviceIds.index(onuDeviceId)) + '.log')
79
80 def olt_ports_should_be_enabled_and_active(self):
81 statusLines = testCaseUtils.get_fields_from_grep_command(self, self.__oltDeviceId, 'voltha_olt_ports.log')
82 assert statusLines, 'No Olt device listed under ports'
83 lines = statusLines.splitlines()
84 for line in lines:
85 self.__fields = testCaseUtils.parseFields(line)
86 assert self.check_states(self.__oltDeviceId) == True, 'States of %s does match expected ' % self.__oltDeviceId
87 portType = self.__fields[3].strip()
88 assert (portType == 'ETHERNET_NNI' or portType == 'PON_OLT'),\
89 'Port type for %s does not match expected ETHERNET_NNI or PON_OLT' % self.__oltDeviceId
90 if portType == 'PON_OLT':
91 self.__peers = self.__fields[7].strip()
92 peerFields = self.__peers.split(',')
93 peerDevices = peerFields[1::2]
94 for peerDevice in peerDevices:
95 deviceFields = peerDevice.split(':')
96 deviceId = deviceFields[1].replace("'","").replace('u','').rstrip("}]").strip()
97 assert deviceId in self.__onuDeviceIds, 'ONU Device %s not found as Peer' % deviceId
98
99 def onu_ports_should_be_enabled_and_active(self):
100 for onuDeviceId in self.__onuDeviceIds:
101 statusLines = testCaseUtils.get_fields_from_grep_command(self, onuDeviceId, 'voltha_onu_ports_' + \
102 str(self.__onuDeviceIds.index(onuDeviceId)) + '.log')
103 assert statusLines, 'No Onu device listed under ports'
104 lines = statusLines.splitlines()
105 for line in lines:
106 self.__fields = testCaseUtils.parseFields(line)
107 assert self.check_states(onuDeviceId) == True, 'States of %s does match expected ' % onuDeviceId
108 portType = self.__fields[3].strip()
109 assert (portType == 'ETHERNET_UNI' or portType == 'PON_ONU'),\
110 'Port type for %s does not match expected ETHERNET_UNI or PON_ONU' % onuDeviceId
111 if portType == 'PON_ONU':
112 self.__peers = self.__fields[7].strip()
113 peerFields = self.__peers.split(',')
114 peerDevice = peerFields[1]
115 deviceFields = peerDevice.split(':')
116 deviceId = deviceFields[1].replace("'","").replace('u','').rstrip("}]").strip()
117 assert deviceId == self.__oltDeviceId, 'OLT Device %s not found as Peer' % deviceId
118
119
120 def check_states(self, deviceId):
121 result = True
122 adminState = self.__fields[4].strip()
123 assert adminState == 'ENABLED', 'Admin State of %s not ENABLED' % deviceId
124 operStatus = self.__fields[5].strip()
125 assert operStatus == 'ACTIVE', 'Oper Status of %s not ACTIVE' % deviceId
126 return result
127
128 def olt_should_have_at_least_one_flow(self):
129 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Flows', 'voltha_olt_flows.log')
130 assert statusLines, 'No Olt flows under device %s' % self.__oltDeviceId
131 before, flows, numFlows = statusLines.partition('Flows')
132 plainNumber = numFlows.strip().strip('():')
133 if plainNumber.isdigit():
134 assert int(plainNumber) > 0, 'Zero number of flows for Olt %s' % self.__oltDeviceId
135
136 def onu_should_have_at_least_one_flow(self):
137 for onuDeviceId in self.__onuDeviceIds:
138 statusLines = testCaseUtils.get_fields_from_grep_command(self, 'Flows', 'voltha_onu_flows_' + \
139 str(self.__onuDeviceIds.index(onuDeviceId)) + '.log')
140 assert statusLines, 'No Onu flows under device %s' % onuDeviceId
141 before, flows, numFlows = statusLines.partition('Flows')
142 plainNumber = numFlows.strip().strip('():')
143 if plainNumber.isdigit():
144 assert int(plainNumber) > 0, 'Zero number of flows for Onu %s' % onuDeviceId
145
146
147def runTest(oltType, onuType, logDir):
148 discovery = Discovery()
149 discovery.dSetLogDirs(logDir)
150 discovery.dConfigure(oltType, onuType)
151 discovery.oltDiscovery()
152 discovery.onuDiscovery()
153 discovery.olt_ports_should_be_enabled_and_active()
154 discovery.onu_ports_should_be_enabled_and_active()
155 discovery.olt_should_have_at_least_one_flow()
156 discovery.onu_should_have_at_least_one_flow()