def push_props_to_isl(tx, subject, *fields): if not fields: return copy_fields = { name: 'source.' + name for name in fields} q = textwrap.dedent(""" MATCH (source:link_props) WHERE source.src_switch = $src_switch AND source.src_port = $src_port AND source.dst_switch = $dst_switch AND source.dst_port = $dst_port MATCH (:switch {name: source.src_switch}) - [target:isl { src_switch: source.src_switch, src_port: source.src_port, dst_switch: source.dst_switch, dst_port: source.dst_port }] -> (:switch {name: source.dst_switch}) """) + db.format_set_fields( db.escape_fields(copy_fields, raw_values=True), field_prefix='target.') p = _make_match(subject) db.log_query('link props to ISL', q, p) tx.run(q, p)
def set_link_props(tx, isl, props): match = _make_match(isl) q = textwrap.dedent(""" MATCH (target:link_props { src_switch: $src_switch, src_port: $src_port, dst_switch: $dst_switch, dst_port: $dst_port}) RETURN target""") logger.debug('link_props 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 (target:link_props) WHERE id(target)=$target_id """) + db.format_set_fields(db.escape_fields(update), field_prefix='target.') logger.debug('Push link_props properties: %r', update) tx.run(q, {'target_id': py2neo.remote(target)._id}) sync_with_link_props(tx, isl, *update.keys()) return origin
def sync_with_link_props(tx, isl, *fields): copy_fields = {name: 'source.' + name for name in fields} q = textwrap.dedent(""" MATCH (source:link_props) WHERE source.src_switch = $src_switch AND source.src_port = $src_port AND source.dst_switch = $dst_switch AND source.dst_port = $dst_port 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}) """) + db.format_set_fields( db.escape_fields(copy_fields, raw_values=True), field_prefix='target.') p = _make_match(isl) db.log_query('propagate link props to ISL', q, p) tx.run(q, p)
def update_config(): q = 'MERGE (target:config {name: "config"})\n' q += db.format_set_fields(db.escape_fields( {x: '$' + x for x in features_status_app_to_transport_map.values()}, raw_values=True), field_prefix='target.') p = { y: features_status[x] for x, y in features_status_app_to_transport_map.items()} with graph.begin() as tx: db.log_query('CONFIG update', q, p) tx.run(q, p)
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 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 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 create_switch(self): switch_id = self.payload['switch_id'] logger.info('Switch %s creation request: timestamp=%s', switch_id, self.timestamp) with graph.begin() as tx: flow_utils.precreate_switches(tx, switch_id) p = { 'address': self.payload['address'], 'hostname': self.payload['hostname'], 'description': self.payload['description'], 'controller': self.payload['controller'], 'state': 'active'} q = 'MATCH (target:switch {name: $dpid})\n' + db.format_set_fields( db.escape_fields( {x: '$' + x for x in p}, raw_values=True), field_prefix='target.') p['dpid'] = switch_id db.log_query('SWITCH create', q, p) tx.run(q, p)
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