timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import time |
| 4 | import pexpect |
| 5 | import struct, fcntl, os, sys, signal |
| 6 | import sys |
| 7 | import re |
| 8 | import json |
| 9 | sys.path.append("../") |
| 10 | from drivers.common.clidriver import CLI |
| 11 | |
| 12 | class QuaggaCliDriver(CLI): |
| 13 | |
| 14 | def __init__(self): |
| 15 | super(CLI, self).__init__() |
| 16 | |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 17 | # TODO: simplify this method |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 18 | def connect(self, **connectargs): |
| 19 | for key in connectargs: |
| 20 | vars(self)[key] = connectargs[key] |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 21 | |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 22 | self.name = self.options['name'] |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 23 | # self.handle = super(QuaggaCliDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 24 | self.handle = super(QuaggaCliDriver, self).connect(user_name=self.user_name, ip_address="1.1.1.1", port=self.port, pwd=self.pwd) |
| 25 | main.log.info("connect parameters:" + str(self.user_name) + ";" + str(self.ip_address) + ";" + str(self.port) + ";" + str(self.pwd)) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 26 | |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 27 | if self.handle: |
pingping-lin | bc23094 | 2014-12-03 18:36:27 -0800 | [diff] [blame] | 28 | #self.handle.expect("",timeout=10) |
| 29 | #self.handle.expect("\$",timeout=10) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 30 | self.handle.sendline("telnet localhost 2605") |
pingping-lin | bc23094 | 2014-12-03 18:36:27 -0800 | [diff] [blame] | 31 | #self.handle.expect("Password:", timeout=5) |
| 32 | self.handle.expect("Password:") |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 33 | self.handle.sendline("hello") |
pingping-lin | bc23094 | 2014-12-03 18:36:27 -0800 | [diff] [blame] | 34 | #self.handle.expect("bgpd", timeout=5) |
| 35 | self.handle.expect("bgpd") |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 36 | self.handle.sendline("enable") |
pingping-lin | bc23094 | 2014-12-03 18:36:27 -0800 | [diff] [blame] | 37 | #self.handle.expect("bgpd#", timeout=5) |
| 38 | self.handle.expect("bgpd#") |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 39 | return self.handle |
| 40 | else : |
| 41 | main.log.info("NO HANDLE") |
| 42 | return main.FALSE |
| 43 | |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 44 | def loginQuagga(self, ip_address): |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 45 | self.name = self.options['name'] |
| 46 | self.handle = super(QuaggaCliDriver, self).connect( |
| 47 | user_name=self.user_name, ip_address=ip_address, |
| 48 | port=self.port, pwd=self.pwd) |
| 49 | main.log.info("connect parameters:" + str(self.user_name) + ";" |
| 50 | + str(self.ip_address) + ";" + str(self.port) + ";" + str(self.pwd)) |
| 51 | |
| 52 | if self.handle: |
pingping-lin | bc23094 | 2014-12-03 18:36:27 -0800 | [diff] [blame] | 53 | #self.handle.expect("") |
| 54 | #self.handle.expect("\$") |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 55 | self.handle.sendline("telnet localhost 2605") |
pingping-lin | d3139ac | 2014-12-04 14:21:26 -0800 | [diff] [blame] | 56 | #self.handle.expect("Password:", timeout=5) |
| 57 | self.handle.expect("Password:") |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 58 | self.handle.sendline("hello") |
pingping-lin | d3139ac | 2014-12-04 14:21:26 -0800 | [diff] [blame] | 59 | #self.handle.expect("bgpd", timeout=5) |
| 60 | self.handle.expect("bgpd") |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 61 | self.handle.sendline("enable") |
pingping-lin | d3139ac | 2014-12-04 14:21:26 -0800 | [diff] [blame] | 62 | #self.handle.expect("bgpd#", timeout=5) |
| 63 | self.handle.expect("bgpd#") |
pingping-lin | 36fbe80 | 2014-11-25 16:01:14 -0800 | [diff] [blame] | 64 | main.log.info("I in quagga on host " + str(ip_address)) |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 65 | |
| 66 | return self.handle |
| 67 | else: |
| 68 | main.log.info("NO HANDLE") |
| 69 | return main.FALSE |
| 70 | |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 71 | def enter_config(self, asn): |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 72 | main.log.info("I am in enter_config method!") |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 73 | try: |
| 74 | self.handle.sendline("") |
| 75 | self.handle.expect("bgpd#") |
| 76 | except: |
| 77 | main.log.warn("Probably not currently in enable mode!") |
| 78 | self.disconnect() |
| 79 | return main.FALSE |
| 80 | self.handle.sendline("configure terminal") |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 81 | self.handle.expect("config", timeout=5) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 82 | routerAS = "router bgp " + str(asn) |
| 83 | try: |
| 84 | self.handle.sendline(routerAS) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 85 | self.handle.expect("config-router", timeout=5) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 86 | return main.TRUE |
| 87 | except: |
| 88 | return main.FALSE |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 89 | |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 90 | def generate_prefixes(self, net, numRoutes): |
| 91 | main.log.info("I am in generate_prefixes method!") |
| 92 | |
| 93 | # each IP prefix will be composed by "net" + "." + m + "." + n + "." + x |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 94 | # the length of each IP prefix is 24 |
| 95 | routes = [] |
| 96 | routes_gen = 0 |
| 97 | m = numRoutes / 256 |
| 98 | n = numRoutes % 256 |
| 99 | |
| 100 | for i in range(0, m): |
| 101 | for j in range(0, 256): |
| 102 | network = str(net) + "." + str(i) + "." + str(j) + ".0/24" |
| 103 | routes.append(network) |
| 104 | routes_gen = routes_gen + 1 |
| 105 | |
| 106 | for j in range(0, n): |
| 107 | network = str(net) + "." + str(m) + "." + str(j) + ".0/24" |
| 108 | routes.append(network) |
| 109 | routes_gen = routes_gen + 1 |
| 110 | |
| 111 | if routes_gen == numRoutes: |
| 112 | main.log.info("Successfully generated " + str(numRoutes) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 113 | + " prefixes!") |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 114 | return routes |
| 115 | return main.FALSE |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 116 | |
| 117 | # This method generates a multiple to single point intent(MultiPointToSinglePointIntent) for a given route |
| 118 | def generate_expected_singleRouteIntent(self, prefix, nextHop, nextHopMac, sdnip_data): |
| 119 | |
| 120 | ingress = [] |
| 121 | egress = "" |
| 122 | for peer in sdnip_data['bgpPeers']: |
| 123 | if peer['ipAddress'] == nextHop: |
| 124 | egress = "of:" + str(peer['attachmentDpid']).replace(":", "") + ":" + str(peer['attachmentPort']) |
| 125 | else: |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 126 | ingress.append("of:" + str(peer['attachmentDpid']).replace(":", "") + ":" + str(peer['attachmentPort'])) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 127 | |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 128 | selector = "ETH_TYPE{ethType=800},IPV4_DST{ip=" + prefix + "}" |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 129 | treatment = "[ETH_DST{mac=" + str(nextHopMac) + "}]" |
| 130 | |
| 131 | intent = egress + "/" + str(sorted(ingress)) + "/" + selector + "/" + treatment |
| 132 | return intent |
| 133 | |
| 134 | def generate_expected_onePeerRouteIntents(self, prefixes, nextHop, nextHopMac, sdnip_json_file_path): |
| 135 | intents = [] |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 136 | sdnip_json_file = open(sdnip_json_file_path).read() |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 137 | |
| 138 | sdnip_data = json.loads(sdnip_json_file) |
| 139 | |
| 140 | for prefix in prefixes: |
| 141 | intents.append(self.generate_expected_singleRouteIntent(prefix, nextHop, nextHopMac, sdnip_data)) |
| 142 | return sorted(intents) |
| 143 | |
| 144 | # TODO |
| 145 | # This method generates all expected route intents for all BGP peers |
| 146 | def generate_expected_routeIntents(self): |
| 147 | intents = [] |
| 148 | return intents |
| 149 | |
| 150 | # This method extracts all actual routes from ONOS CLI |
| 151 | def extract_actual_routes(self, get_routes_result): |
| 152 | routes_json_obj = json.loads(get_routes_result) |
| 153 | |
| 154 | allRoutes_actual = [] |
| 155 | for route in routes_json_obj: |
| 156 | if route['prefix'] == '172.16.10.0/24': |
| 157 | continue |
| 158 | allRoutes_actual.append(route['prefix'] + "/" + route['nextHop']) |
| 159 | |
| 160 | return sorted(allRoutes_actual) |
| 161 | |
| 162 | # This method extracts all actual route intents from ONOS CLI |
| 163 | def extract_actual_routeIntents(self, get_intents_result): |
| 164 | intents = [] |
| 165 | # TODO: delete the line below when change to Mininet demo script |
pingping-lin | 36fbe80 | 2014-11-25 16:01:14 -0800 | [diff] [blame] | 166 | # get_intents_result=open("../tests/SdnIpTest/intents.json").read() |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 167 | intents_json_obj = json.loads(get_intents_result) |
| 168 | |
| 169 | for intent in intents_json_obj: |
pingping-lin | 78fe446 | 2014-12-04 17:34:48 -0800 | [diff] [blame] | 170 | if intent['appId'] != "org.onosproject.sdnip" : |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 171 | continue |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 172 | if intent['type'] == "MultiPointToSinglePointIntent" and intent['state'] == 'INSTALLED': |
| 173 | egress = str(intent['egress']['device']) + ":" + str(intent['egress']['port']) |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 174 | ingress = [] |
| 175 | for attachmentPoint in intent['ingress']: |
| 176 | ingress.append(str(attachmentPoint['device']) + ":" + str(attachmentPoint['port'])) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 177 | |
| 178 | selector = intent['selector'].replace("[" , "").replace("]" , "").replace(" ", "") |
| 179 | if str(selector).startswith("IPV4"): |
| 180 | str1, str2 = str(selector).split(",") |
| 181 | selector = str2 + "," + str1 |
| 182 | |
| 183 | intent = egress + "/" + str(sorted(ingress)) + "/" + selector + "/" + intent['treatment'] |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 184 | intents.append(intent) |
| 185 | return sorted(intents) |
| 186 | |
| 187 | # This method extracts all actual BGP intents from ONOS CLI |
| 188 | def extract_actual_bgpIntents(self, get_intents_result): |
| 189 | intents = [] |
| 190 | # TODO: delete the line below when change to Mininet demo script |
pingping-lin | 36fbe80 | 2014-11-25 16:01:14 -0800 | [diff] [blame] | 191 | # get_intents_result=open("../tests/SdnIpTest/intents.json").read() |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 192 | intents_json_obj = json.loads(get_intents_result) |
| 193 | |
| 194 | for intent in intents_json_obj: |
pingping-lin | 78fe446 | 2014-12-04 17:34:48 -0800 | [diff] [blame] | 195 | if intent['appId'] != "org.onosproject.sdnip": |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 196 | continue |
| 197 | if intent['type'] == "PointToPointIntent" and "protocol=6" in str(intent['selector']): |
| 198 | ingress = str(intent['ingress']['device']) + ":" + str(intent['ingress']['port']) |
| 199 | egress = str(intent['egress']['device']) + ":" + str(intent['egress']['port']) |
| 200 | selector = str(intent['selector']).replace(" ", "").replace("[", "").replace("]", "").split(",") |
| 201 | intent = ingress + "/" + egress + "/" + str(sorted(selector)) |
| 202 | intents.append(intent) |
| 203 | |
| 204 | return sorted(intents) |
| 205 | |
| 206 | # This method generates a single point to single point intent(PointToPointIntent) for BGP path |
| 207 | def generate_expected_bgpIntents(self, sdnip_json_file_path): |
| 208 | from operator import eq |
| 209 | |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 210 | sdnip_json_file = open(sdnip_json_file_path).read() |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 211 | sdnip_data = json.loads(sdnip_json_file) |
| 212 | |
| 213 | intents = [] |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 214 | bgpPeerAttachmentPoint = "" |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 215 | bgpSpeakerAttachmentPoint = "of:" + str(sdnip_data['bgpSpeakers'][0]['attachmentDpid']).replace(":", "") + ":" + str(sdnip_data['bgpSpeakers'][0]['attachmentPort']) |
| 216 | for peer in sdnip_data['bgpPeers']: |
| 217 | bgpPeerAttachmentPoint = "of:" + str(peer['attachmentDpid']).replace(":", "") + ":" + str(peer['attachmentPort']) |
| 218 | # find out the BGP speaker IP address for this BGP peer |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 219 | bgpSpeakerIpAddress = "" |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 220 | for interfaceAddress in sdnip_data['bgpSpeakers'][0]['interfaceAddresses']: |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 221 | # if eq(interfaceAddress['interfaceDpid'],sdnip_data['bgpSpeakers'][0]['attachmentDpid']) and eq(interfaceAddress['interfacePort'], sdnip_data['bgpSpeakers'][0]['attachmentPort']): |
| 222 | if eq(interfaceAddress['interfaceDpid'], peer['attachmentDpid']) and eq(interfaceAddress['interfacePort'], peer['attachmentPort']): |
| 223 | bgpSpeakerIpAddress = interfaceAddress['ipAddress'] |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 224 | break |
| 225 | else: |
| 226 | continue |
| 227 | |
| 228 | # from bgpSpeakerAttachmentPoint to bgpPeerAttachmentPoint direction |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 229 | selector_str = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," + "IPV4_DST{ip=" + peer['ipAddress'] + "/32}," + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_DST{tcpPort=179}" |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 230 | selector = selector_str.replace(" ", "").replace("[", "").replace("]", "").split(",") |
| 231 | intent = bgpSpeakerAttachmentPoint + "/" + bgpPeerAttachmentPoint + "/" + str(sorted(selector)) |
| 232 | intents.append(intent) |
| 233 | |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 234 | selector_str = "IPV4_SRC{ip=" + bgpSpeakerIpAddress + "/32}," + "IPV4_DST{ip=" + peer['ipAddress'] + "/32}," + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_SRC{tcpPort=179}" |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 235 | selector = selector_str.replace(" ", "").replace("[", "").replace("]", "").split(",") |
| 236 | intent = bgpSpeakerAttachmentPoint + "/" + bgpPeerAttachmentPoint + "/" + str(sorted(selector)) |
| 237 | intents.append(intent) |
| 238 | |
| 239 | # from bgpPeerAttachmentPoint to bgpSpeakerAttachmentPoint direction |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 240 | selector_str = "IPV4_SRC{ip=" + peer['ipAddress'] + "/32}," + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_DST{tcpPort=179}" |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 241 | selector = selector_str.replace(" ", "").replace("[", "").replace("]", "").split(",") |
| 242 | intent = bgpPeerAttachmentPoint + "/" + bgpSpeakerAttachmentPoint + "/" + str(sorted(selector)) |
| 243 | intents.append(intent) |
| 244 | |
| 245 | selector_str = "IPV4_SRC{ip=" + peer['ipAddress'] + "/32}," + "IPV4_DST{ip=" + bgpSpeakerIpAddress + "/32}," + "IP_PROTO{protocol=6}, ETH_TYPE{ethType=800}, TCP_SRC{tcpPort=179}" |
| 246 | selector = selector_str.replace(" ", "").replace("[", "").replace("]", "").split(",") |
| 247 | intent = bgpPeerAttachmentPoint + "/" + bgpSpeakerAttachmentPoint + "/" + str(sorted(selector)) |
| 248 | intents.append(intent) |
| 249 | |
| 250 | return sorted(intents) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 251 | |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 252 | def add_routes(self, routes, routeRate): |
| 253 | main.log.info("I am in add_routes method!") |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 254 | |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 255 | routes_added = 0 |
| 256 | try: |
| 257 | self.handle.sendline("") |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 258 | # self.handle.expect("config-router") |
pingping-lin | 8b306ac | 2014-11-17 18:13:51 -0800 | [diff] [blame] | 259 | self.handle.expect("config-router", timeout=5) |
| 260 | except: |
| 261 | main.log.warn("Probably not in config-router mode!") |
| 262 | self.disconnect() |
| 263 | main.log.info("Start to add routes") |
| 264 | |
| 265 | for i in range(0, len(routes)): |
| 266 | routeCmd = "network " + routes[i] |
| 267 | try: |
| 268 | self.handle.sendline(routeCmd) |
| 269 | self.handle.expect("bgpd", timeout=5) |
| 270 | except: |
| 271 | main.log.warn("Failed to add route") |
| 272 | self.disconnect() |
| 273 | waitTimer = 1.00 / routeRate |
| 274 | time.sleep(waitTimer) |
| 275 | if routes_added == len(routes): |
| 276 | main.log.info("Finished adding routes") |
| 277 | return main.TRUE |
| 278 | return main.FALSE |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 279 | |
| 280 | def delete_routes(self, routes, routeRate): |
| 281 | main.log.info("I am in delete_routes method!") |
| 282 | |
| 283 | routes_added = 0 |
| 284 | try: |
| 285 | self.handle.sendline("") |
| 286 | # self.handle.expect("config-router") |
| 287 | self.handle.expect("config-router", timeout=5) |
| 288 | except: |
| 289 | main.log.warn("Probably not in config-router mode!") |
| 290 | self.disconnect() |
| 291 | main.log.info("Start to delete routes") |
| 292 | |
| 293 | for i in range(0, len(routes)): |
| 294 | routeCmd = "no network " + routes[i] |
| 295 | try: |
| 296 | self.handle.sendline(routeCmd) |
| 297 | self.handle.expect("bgpd", timeout=5) |
| 298 | except: |
| 299 | main.log.warn("Failed to add route") |
| 300 | self.disconnect() |
| 301 | waitTimer = 1.00 / routeRate |
| 302 | time.sleep(waitTimer) |
| 303 | if routes_added == len(routes): |
| 304 | main.log.info("Finished deleting routes") |
| 305 | return main.TRUE |
| 306 | return main.FALSE |
| 307 | |
| 308 | def ping_test(self, ip_address, ping_test_file, ping_test_result_file): |
pingping-lin | dd3b0e4 | 2014-12-02 11:46:54 -0800 | [diff] [blame] | 309 | main.log.info("Start the ping test on host:" + str(ip_address)) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 310 | |
| 311 | self.name = self.options['name'] |
| 312 | self.handle = super(QuaggaCliDriver, self).connect( |
| 313 | user_name=self.user_name, ip_address=ip_address, |
| 314 | port=self.port, pwd=self.pwd) |
| 315 | main.log.info("connect parameters:" + str(self.user_name) + ";" |
| 316 | + str(self.ip_address) + ";" + str(self.port) + ";" + str(self.pwd)) |
| 317 | |
| 318 | if self.handle: |
pingping-lin | d3139ac | 2014-12-04 14:21:26 -0800 | [diff] [blame] | 319 | #self.handle.expect("") |
| 320 | #self.handle.expect("\$") |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 321 | main.log.info("I in host " + str(ip_address)) |
pingping-lin | 01355a6 | 2014-12-02 20:58:14 -0800 | [diff] [blame] | 322 | main.log.info(ping_test_file + " > " + ping_test_result_file + " &") |
| 323 | self.handle.sendline(ping_test_file + " > " + ping_test_result_file + " &") |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 324 | self.handle.expect("\$", timeout=60) |
| 325 | handle = self.handle.before |
| 326 | |
| 327 | return handle |
| 328 | else: |
| 329 | main.log.info("NO HANDLE") |
| 330 | return main.FALSE |
| 331 | |
| 332 | |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 333 | # Please use the generate_routes plus add_routes instead of this one |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 334 | def add_route(self, net, numRoutes, routeRate): |
| 335 | try: |
| 336 | self.handle.sendline("") |
| 337 | self.handle.expect("config-router") |
| 338 | except: |
| 339 | main.log.warn("Probably not in config-router mode!") |
| 340 | self.disconnect() |
pingping-lin | 36fbe80 | 2014-11-25 16:01:14 -0800 | [diff] [blame] | 341 | main.log.info("Adding Routes") |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 342 | j = 0 |
| 343 | k = 0 |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 344 | while numRoutes > 255: |
| 345 | numRoutes = numRoutes - 255 |
| 346 | j = j + 1 |
| 347 | k = numRoutes % 254 |
| 348 | routes_added = 0 |
| 349 | if numRoutes > 255: |
| 350 | numRoutes = 255 |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 351 | for m in range(1, j + 1): |
| 352 | for n in range(1, numRoutes + 1): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 353 | network = str(net) + "." + str(m) + "." + str(n) + ".0/24" |
| 354 | routeCmd = "network " + network |
| 355 | try: |
| 356 | self.handle.sendline(routeCmd) |
| 357 | self.handle.expect("bgpd") |
| 358 | except: |
| 359 | main.log.warn("failed to add route") |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 360 | self.disconnect() |
| 361 | waitTimer = 1.00 / routeRate |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 362 | time.sleep(waitTimer) |
| 363 | routes_added = routes_added + 1 |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 364 | for d in range(j + 1, j + 2): |
| 365 | for e in range(1, k + 1): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 366 | network = str(net) + "." + str(d) + "." + str(e) + ".0/24" |
| 367 | routeCmd = "network " + network |
| 368 | try: |
| 369 | self.handle.sendline(routeCmd) |
| 370 | self.handle.expect("bgpd") |
| 371 | except: |
| 372 | main.log.warn("failed to add route") |
| 373 | self.disconnect |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 374 | waitTimer = 1.00 / routeRate |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 375 | time.sleep(waitTimer) |
| 376 | routes_added = routes_added + 1 |
| 377 | if routes_added == numRoutes: |
| 378 | return main.TRUE |
| 379 | return main.FALSE |
pingping-lin | 6f6332e | 2014-11-19 19:13:58 -0800 | [diff] [blame] | 380 | |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 381 | def del_route(self, net, numRoutes, routeRate): |
| 382 | try: |
| 383 | self.handle.sendline("") |
| 384 | self.handle.expect("config-router") |
| 385 | except: |
| 386 | main.log.warn("Probably not in config-router mode!") |
| 387 | self.disconnect() |
pingping-lin | 36fbe80 | 2014-11-25 16:01:14 -0800 | [diff] [blame] | 388 | main.log.info("Deleting Routes") |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 389 | j = 0 |
| 390 | k = 0 |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 391 | while numRoutes > 255: |
| 392 | numRoutes = numRoutes - 255 |
| 393 | j = j + 1 |
| 394 | k = numRoutes % 254 |
| 395 | routes_deleted = 0 |
| 396 | if numRoutes > 255: |
| 397 | numRoutes = 255 |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 398 | for m in range(1, j + 1): |
| 399 | for n in range(1, numRoutes + 1): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 400 | network = str(net) + "." + str(m) + "." + str(n) + ".0/24" |
| 401 | routeCmd = "no network " + network |
| 402 | try: |
| 403 | self.handle.sendline(routeCmd) |
| 404 | self.handle.expect("bgpd") |
| 405 | except: |
| 406 | main.log.warn("Failed to delete route") |
| 407 | self.disconnect() |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 408 | waitTimer = 1.00 / routeRate |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 409 | time.sleep(waitTimer) |
| 410 | routes_deleted = routes_deleted + 1 |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 411 | for d in range(j + 1, j + 2): |
| 412 | for e in range(1, k + 1): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 413 | network = str(net) + "." + str(d) + "." + str(e) + ".0/24" |
| 414 | routeCmd = "no network " + network |
| 415 | try: |
| 416 | self.handle.sendline(routeCmd) |
| 417 | self.handle.expect("bgpd") |
| 418 | except: |
| 419 | main.log.warn("Failed to delete route") |
| 420 | self.disconnect() |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 421 | waitTimer = 1.00 / routeRate |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 422 | time.sleep(waitTimer) |
| 423 | routes_deleted = routes_deleted + 1 |
| 424 | if routes_deleted == numRoutes: |
| 425 | return main.TRUE |
| 426 | return main.FALSE |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 427 | |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 428 | def check_routes(self, brand, ip, user, pw): |
| 429 | def pronto(ip, user, passwd): |
| 430 | print "Connecting to Pronto switch" |
| 431 | child = pexpect.spawn("telnet " + ip) |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 432 | i = child.expect(["login:", "CLI#", pexpect.TIMEOUT]) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 433 | if i == 0: |
| 434 | print "Username and password required. Passing login info." |
| 435 | child.sendline(user) |
| 436 | child.expect("Password:") |
| 437 | child.sendline(passwd) |
| 438 | child.expect("CLI#") |
| 439 | print "Logged in, getting flowtable." |
| 440 | child.sendline("flowtable brief") |
| 441 | for t in range (9): |
| 442 | t2 = 9 - t |
| 443 | print "\r" + str(t2) |
| 444 | sys.stdout.write("\033[F") |
| 445 | time.sleep(1) |
| 446 | print "Scanning flowtable" |
| 447 | child.expect("Flow table show") |
| 448 | count = 0 |
| 449 | while 1: |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 450 | i = child.expect(['17\d\.\d{1,3}\.\d{1,3}\.\d{1,3}', 'CLI#', pexpect.TIMEOUT]) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 451 | if i == 0: |
| 452 | count = count + 1 |
| 453 | elif i == 1: |
| 454 | print "Pronto flows: " + str(count) + "\nDone\n" |
| 455 | break |
| 456 | else: |
| 457 | break |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 458 | def cisco(ip, user, passwd): |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 459 | print "Establishing Cisco switch connection" |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 460 | child = pexpect.spawn("ssh " + user + "@" + ip) |
| 461 | i = child.expect(["Password:", "CLI#", pexpect.TIMEOUT]) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 462 | if i == 0: |
| 463 | print "Password required. Passing now." |
| 464 | child.sendline(passwd) |
| 465 | child.expect("#") |
| 466 | print "Logged in. Retrieving flow table then counting flows." |
| 467 | child.sendline("show openflow switch all flows all") |
| 468 | child.expect("Logical Openflow Switch") |
| 469 | print "Flow table retrieved. Counting flows" |
| 470 | count = 0 |
| 471 | while 1: |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 472 | i = child.expect(["nw_src=17", "#", pexpect.TIMEOUT]) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 473 | if i == 0: |
| 474 | count = count + 1 |
| 475 | elif i == 1: |
| 476 | print "Cisco flows: " + str(count) + "\nDone\n" |
| 477 | break |
| 478 | else: |
| 479 | break |
| 480 | if brand == "pronto" or brand == "PRONTO": |
pingping-lin | c6b86fa | 2014-12-01 16:18:10 -0800 | [diff] [blame] | 481 | pronto(ip, user, passwd) |
| 482 | # elif brand == "cisco" or brand == "CISCO": |
| 483 | # cisco(ip,user,passwd) |
timlindberg | ef8d55d | 2013-09-27 12:50:13 -0700 | [diff] [blame] | 484 | def disconnect(self): |
| 485 | ''' |
| 486 | Called when Test is complete to disconnect the Quagga handle. |
| 487 | ''' |
| 488 | response = '' |
| 489 | try: |
| 490 | self.handle.close() |
| 491 | except: |
| 492 | main.log.error("Connection failed to the host") |
| 493 | response = main.FALSE |
| 494 | return response |