def resolve_conflicts(tx, isl): logger.info('Check ISL %s for conflicts', isl) involved = [fetch(tx, isl), fetch(tx, isl.reversed())] keep_dbid = {db.neo_id(x) for x in involved} involved.extend(fetch_by_endpoint(tx, isl.source)) involved.extend(fetch_by_endpoint(tx, isl.dest)) _lock_affected_switches(tx, involved) for link in involved: link_dbid = db.neo_id(link) if link_dbid in keep_dbid: continue link_isl = model.InterSwitchLink.new_from_db(link) if is_active_status(link['actual']): logger.error( 'Detected ISL %s conflict with %s. Please contact dev team', link_isl, isl) # set_active_field(tx, link_dbid, 'inactive') # update_status(tx, link_isl) else: logger.debug( "Skip conflict ISL %s deactivation due to its current status - %s", link_isl, link['actual'])
def switch_unplug(tx, dpid): logging.info("Deactivate all ISL to/from %s", dpid) for db_link in fetch_by_datapath(tx, dpid): source = model.NetworkEndpoint(db_link['src_switch'], db_link['src_port']) dest = model.NetworkEndpoint(db_link['dst_switch'], db_link['dst_port']) isl = model.InterSwitchLink(source, dest, db_link['actual']) logging.debug("Found ISL: %s", isl) set_active_field(tx, db.neo_id(db_link), 'inactive') update_status(tx, isl)
def set_props(tx, isl, props): target = fetch(tx, isl) origin, update = db.locate_changes(target, props) if update: q = textwrap.dedent(""" MATCH (:switch)-[target:isl]->(:switch) WHERE id(target)=$target_id """) + db.format_set_fields(db.escape_fields(update), field_prefix='target.') logger.debug('Push ISL properties: %r', update) tx.run(q, {'target_id': db.neo_id(target)}) return origin
def switch_unplug(tx, dpid, mtime=True): logging.info("Deactivate all ISL to/from %s", dpid) involved = list(fetch_by_datapath(tx, dpid)) _lock_affected_switches(tx, involved, dpid) for db_link in involved: source = model.IslPathNode(db_link['src_switch'], db_link['src_port']) dest = model.IslPathNode(db_link['dst_switch'], db_link['dst_port']) isl = model.InterSwitchLink(source, dest, db_link['actual']) logging.debug("Found ISL: %s", isl) set_active_field(tx, db.neo_id(db_link), 'inactive') update_status(tx, isl, mtime=mtime)
def set_props(tx, subject): db_subject = fetch(tx, subject) origin, update = db.locate_changes(db_subject, subject.props_db_view()) if update: q = textwrap.dedent(""" MATCH (target:link_props) WHERE id(target)=$target_id """) + db.format_set_fields( db.escape_fields(update), field_prefix='target.') p = {'target_id': db.neo_id(db_subject)} db.log_query('propagate link props to ISL', q, p) tx.run(q, p) return origin, update.keys()
def drop_db_flows(tx): q_lookup = 'MATCH (:switch)-[a:flow]->(:switch) RETURN a' q_delete = 'MATCH (:switch)-[a:flow]->(:switch) WHERE id(a)=$id DELETE a' batch = (x['a'] for x in tx.run(q_lookup)) for relation in batch: cookie = relation['cookie'] if cookie is None: continue try: cookie = int(cookie) if not (cookie & cookie_test_data_flag): continue except ValueError: continue drop_db_flow_segments(tx, relation['flowid']) tx.run(q_delete, {'id': db.neo_id(relation)})
def set_link_props(tx, isl, props): target = fetch_link_props(tx, isl) origin, update = _locate_changes(target, props) if update: q = textwrap.dedent(""" MATCH (target:link_props) WHERE id(target)=$target_id """) + db.format_set_fields(db.escape_fields(update), field_prefix='target.') p = {'target_id': db.neo_id(target)} db.log_query('link_props set props', q, p) tx.run(q, p) sync_with_link_props(tx, isl, *update.keys()) return origin
def disable_by_endpoint(tx, endpoint, is_moved=False, mtime=True): logging.debug('Locate all ISL starts on %s', endpoint) involved = list(fetch_by_endpoint(tx, endpoint)) _lock_affected_switches(tx, involved, endpoint.dpid) updated = [] for link in involved: isl = model.InterSwitchLink.new_from_db(link) logger.info('Deactivate ISL %s', isl) status = 'moved' if is_moved else 'inactive' set_active_field(tx, db.neo_id(link), status) update_status(tx, isl, mtime=mtime) updated.append(isl) return updated
def disable_by_endpoint(tx, endpoint, is_moved=False): logging.debug('Locate all ISL starts on %s', endpoint) involved = list(fetch_by_endpoint(tx, endpoint)) switches = set() for link in involved: switches.add(link['src_switch']) switches.add(link['dst_switch']) flow_utils.precreate_switches(tx, *switches) updated = [] for link in involved: isl = model.InterSwitchLink.new_from_db(link) logger.info('Deactivate ISL %s', isl) status = 'moved' if is_moved else 'inactive' set_active_field(tx, db.neo_id(link), status) update_status(tx, isl) updated.append(isl) return updated
def set_props(tx, isl, props): match = _make_match(isl) q = textwrap.dedent(""" MATCH (:switch {name: $src_switch}) - [target:isl { src_switch: $src_switch, src_port: $src_port, dst_switch: $dst_switch, dst_port: $dst_port }] -> (:switch {name: $dst_switch}) RETURN target""") logger.debug('ISL lookup query:\n%s', q) cursor = tx.run(q, match) try: target = db.fetch_one(cursor)['target'] except exc.DBEmptyResponse: raise exc.DBRecordNotFound(q, match) origin, update = _locate_changes(target, props) if update: q = textwrap.dedent(""" MATCH (:switch)-[target:isl]->(:switch) WHERE id(target)=$target_id """) + db.format_set_fields(db.escape_fields(update), field_prefix='target.') logger.debug('Push ISL properties: %r', update) tx.run(q, {'target_id': db.neo_id(target)}) return origin