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