def switch_unplug(self): switch_id = self.payload['switch_id'] logger.info('Switch %s deactivation request', switch_id) with graph.begin() as tx: flow_utils.precreate_switches(tx, switch_id) q = ('MATCH (target:switch {name: $dpid}) ' 'SET target.state="inactive"') tx.run(q, {'dpid': switch_id})
def activate_switch(self): switch_id = self.payload['switch_id'] logger.info('Switch %s activation request: timestamp=%s', switch_id, self.timestamp) with graph.begin() as tx: flow_utils.precreate_switches(tx, switch_id) q = 'MATCH (target:switch {name: $dpid}) SET target.state="active"' p = {'dpid': switch_id} db.log_query('SWITCH activate', q, p) tx.run(q, p)
def switch_unplug(self): switch_id = self.payload['switch_id'] logger.info('Switch %s deactivation request: timestamp=%s, ', switch_id, self.timestamp) with graph.begin() as tx: logger.info('Deactivating switch: %s', switch_id) flow_utils.precreate_switches(tx, switch_id) q = ('MATCH (target:switch {name: $dpid}) ' 'SET target.state="inactive"') tx.run(q, {'dpid': switch_id}) isl_utils.switch_unplug(tx, switch_id)
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 create_isl(self): """ Two parts to creating an ISL: (1) create the relationship itself (2) add any link properties, if they exist. NB: The query used for (2) is the same as in the TER TODO: either share the same query as library in python, or handle in java :return: success or failure (boolean) """ 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']) isl = model.InterSwitchLink.new_from_isl_data(self.payload) isl.ensure_path_complete() logger.info('ISL %s create request', isl) with graph.begin() as tx: flow_utils.precreate_switches( tx, isl.source.dpid, isl.dest.dpid) isl_utils.create_if_missing(tx, self.timestamp, isl) isl_utils.set_props(tx, isl, { 'latency': latency, 'speed': speed, 'max_bandwidth': available_bandwidth, 'actual': 'active'}) isl_utils.update_status(tx, isl, mtime=self.timestamp) isl_utils.resolve_conflicts(tx, isl) life_cycle = isl_utils.get_life_cycle_fields(tx, isl) self.update_payload_lifecycle(life_cycle) # # Now handle the second part .. pull properties from link_props if they exist # src_sw, src_pt, dst_sw, dst_pt = a_switch, a_port, b_switch, b_port # use same names as TER code query = 'MATCH (src:switch)-[i:isl]->(dst:switch) ' query += ' WHERE i.src_switch = "%s" ' \ ' AND i.src_port = %s ' \ ' AND i.dst_switch = "%s" ' \ ' AND i.dst_port = %s ' % (src_sw, src_pt, dst_sw, dst_pt) query += ' MATCH (lp:link_props) ' query += ' WHERE lp.src_switch = "%s" ' \ ' AND lp.src_port = %s ' \ ' AND lp.dst_switch = "%s" ' \ ' AND lp.dst_port = %s ' % (src_sw, src_pt, dst_sw, dst_pt) query += ' SET i += lp ' graph.run(query) # # Finally, update the available_bandwidth.. # flow_utils.update_isl_bandwidth(src_sw, src_pt, dst_sw, dst_pt) logger.info('ISL %s have been created/updated', isl) return True
def create_isl(self): """ Two parts to creating an ISL: (1) create the relationship itself (2) add any link properties, if they exist. NB: The query used for (2) is the same as in the TER TODO: either share the same query as library in python, or handle in java :return: success or failure (boolean) """ 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']) isl = model.InterSwitchLink.new_from_isl_data(self.payload) isl.ensure_path_complete() logger.info('ISL %s create request', isl) with graph.begin() as tx: flow_utils.precreate_switches(tx, isl.source.dpid, isl.dest.dpid) isl_utils.create_if_missing(tx, isl) # # Given that we know the the src and dst exist, the following query will either # create the relationship if it doesn't exist, or update it if it does # isl_create_or_update = ("MERGE " "(src:switch {{name:'{}'}}) " "ON CREATE SET src.state = 'inactive' " "MERGE " "(dst:switch {{name:'{}'}}) " "ON CREATE SET dst.state = 'inactive' " "MERGE " "(src)-[i:isl {{" "src_switch: '{}', src_port: {}, " "dst_switch: '{}', dst_port: {} " "}}]->(dst) " "SET " "i.latency = {}, " "i.speed = {}, " "i.max_bandwidth = {}, " "i.actual = 'active', " "i.status = 'inactive'").format( a_switch, b_switch, a_switch, a_port, b_switch, b_port, latency, speed, available_bandwidth) tx.run(isl_create_or_update) isl_utils.update_status(tx, isl) isl_utils.resolve_conflicts(tx, isl) # # Now handle the second part .. pull properties from link_props if they exist # src_sw, src_pt, dst_sw, dst_pt = a_switch, a_port, b_switch, b_port # use same names as TER code query = 'MATCH (src:switch)-[i:isl]->(dst:switch) ' query += ' WHERE i.src_switch = "%s" ' \ ' AND i.src_port = %s ' \ ' AND i.dst_switch = "%s" ' \ ' AND i.dst_port = %s ' % (src_sw, src_pt, dst_sw, dst_pt) query += ' MATCH (lp:link_props) ' query += ' WHERE lp.src_switch = "%s" ' \ ' AND lp.src_port = %s ' \ ' AND lp.dst_switch = "%s" ' \ ' AND lp.dst_port = %s ' % (src_sw, src_pt, dst_sw, dst_pt) query += ' SET i += lp ' graph.run(query) # # Finally, update the available_bandwidth.. # flow_utils.update_isl_bandwidth(src_sw, src_pt, dst_sw, dst_pt) logger.info('ISL %s have been created/updated', isl) return True