def isl_exists(src_switch, src_port): if src_port: exists_query = ("MATCH (a:switch)-[r:isl {{" "src_switch: '{}', " "src_port: {}}}]->(b:switch) return r") return graph.run(exists_query.format(src_switch, src_port)).data() else: exists_query = ("MATCH (a:switch)-[r:isl {{" "src_switch: '{}'}}]->(b:switch) return r") return graph.run(exists_query.format(src_switch)).data()
def get_isls(): try: query = "MATCH (a:switch)-[r:isl]->(b:switch) RETURN r" result = graph.run(query).data() isls = [] for data in result: link = data['r'] isl = { 'id': str(link['src_switch'] + '_' + str(link['src_port'])), 'speed': int(link['speed']), 'latency_ns': int(link['latency']), 'available_bandwidth': int(link['available_bandwidth']), 'state': "DISCOVERED", 'path': [ {'switch_id': str(link['src_switch']), 'port_no': int(link['src_port']), 'seq_id': 0, 'segment_latency': int(link['latency'])}, {'switch_id': str(link['dst_switch']), 'port_no': int(link['dst_port']), 'seq_id': 1, 'segment_latency': 0}], 'message_type': 'isl' } isls.append(isl) logger.info('Got isls: %s', isls) except Exception as e: logger.exception('Can not get isls', e.message) raise return isls
def get_switches(): try: query = "MATCH (n:switch) RETURN n" result = graph.run(query).data() switches = [] for data in result: node = data['n'] switch = { 'switch_id': node['name'], 'state': switch_states[node['state']], 'address': node['address'], 'hostname': node['hostname'], 'description': node['description'], 'controller': node['controller'], 'message_type': 'switch', } switches.append(switch) logger.info('Got switches: %s', switches) except Exception as e: logger.exception('Can not get switches', e.message) raise return switches
def delete_isl(src_switch, src_port): flow_lock.acquire() try: logger.info('Removing ISL: src_switch=%s, src_port=%s', src_switch, src_port) if src_port: delete_query = ("MATCH (a:switch)-[r:isl {{" "src_switch: '{}', " "src_port: {}}}]->(b:switch) delete r") graph.run(delete_query.format(src_switch, src_port)).data() else: delete_query = ("MATCH (a:switch)-[r:isl {{" "src_switch: '{}'}}]->(b:switch) delete r") graph.run(delete_query.format(src_switch)).data() finally: flow_lock.release() return True
def create_isl(self): path = self.payload['path'] latency = int(self.payload['latency_ns']) a_switch = path[0]['switch_id'] a_port = int(path[0]['port_no']) b_switch = path[1]['switch_id'] b_port = int(path[1]['port_no']) speed = int(self.payload['speed']) available_bandwidth = int(self.payload['available_bandwidth']) a_switch_node = graph.find_one('switch', property_key='name', property_value='{}'.format(a_switch)) if not a_switch_node: logger.error('Isl source was not found: %s', a_switch_node) return False b_switch_node = graph.find_one('switch', property_key='name', property_value='{}'.format(b_switch)) if not b_switch_node: logger.error('Isl destination was not found: %s', b_switch_node) return False isl_lock.acquire() try: isl_exists_query = ("MATCH (a:switch)-[r:isl {{" "src_switch: '{}', " "src_port: {}, " "dst_switch: '{}', " "dst_port: {}}}]->(b:switch) return r") isl_exists = graph.run(isl_exists_query.format(a_switch, a_port, b_switch, b_port)).data() if not isl_exists: logger.info('Isl %s_%d creation request: timestamp=%s', a_switch, a_port, self.timestamp) isl_query = ("MATCH (u:switch {{name:'{}'}}), " "(r:switch {{name:'{}'}}) " "MERGE (u)-[:isl {{" "src_port: {}, " "dst_port: {}, " "src_switch: '{}', " "dst_switch: '{}', " "latency: {}, " "speed: {}, " "available_bandwidth: {}}}]->(r)") graph.run(isl_query.format(a_switch_node['name'], b_switch_node['name'], a_port, b_port, a_switch, b_switch, latency, speed, available_bandwidth)) logger.info('ISL between %s and %s created', a_switch_node['name'], b_switch_node['name']) else: logger.debug('Isl %s_%d update request: timestamp=%s', a_switch, a_port, self.timestamp) isl_update_query = ("MATCH (a:switch)-[r:isl {{" "src_switch: '{}', " "src_port: {}, " "dst_switch: '{}', " "dst_port: {}}}]->(b:switch) " "set r.latency = {} return r") graph.run(isl_update_query.format(a_switch, a_port, b_switch, b_port, latency)).data() logger.debug('ISL between %s and %s updated', a_switch_node['name'], b_switch_node['name']) except Exception as e: logger.exception('ISL between %s and %s creation error: %s', a_switch_node['name'], b_switch_node['name'], e.message) finally: isl_lock.release() return True