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