def validate_config(self): """Validate DAQ configuration""" errors = False for old_key in _CONFIG_MIGRATIONS: if old_key in self.config: errors = True LOGGER.warning("Config '%s' is now '%s'", old_key, _CONFIG_MIGRATIONS[old_key]) if errors: LOGGER.error('Old style configs found. Goodby.') return False # Work around int/str ambiguity, force to base-10 string of_dpid = self.config.get('switch_setup', {}).get('of_dpid') if of_dpid: self.config.get('switch_setup', {})['of_dpid'] = str(int(str(of_dpid), 0)) # Validate structure of config by reading it into a pb message. utils.dict_proto(self.config, sys_config.DaqConfig) LOGGER.info('env setup %s %s %s %s', os.getcwd(), DAQ_RUN_DIR, DAQ_CONF_DIR, DAQ_LIB_DIR) return True
def get_port_rule_counts(self, switch, port, rule_samples): """Return the ACL count for a port""" acl_config, error_map = self._verify_port_acl_config(switch, port) if not acl_config: return dict_proto(error_map, RuleCounts) rule_counts = self._get_port_rule_counts(switch, port, acl_config, rule_samples) return dict_proto(rule_counts, RuleCounts)
def start_session(self, mac): """Send device result of a device to server""" devices_state = {'device_mac': mac} LOGGER.info('Connecting to stream for mac %s', mac) generator = self._stub.StartSession( dict_proto(devices_state, SessionParams)) return generator
def report_sink(self, report_dict): """Process a generated report""" # TODO: Make the DeviceReport proto complete so ignore_unknown_fields isn't required. report_proto = dict_proto(report_dict, DeviceReport, ignore_unknown_fields=True) LOGGER.info('Report sink with report %s', bool(report_proto))
def _port_event_handler(self, mac, callback): device = {"mac": mac} result_generator = self._stub.GetPortState(dict_proto(device, Device)) def cancel_request(): result_generator.cancel() self._active_requests.append(cancel_request) for port_event in result_generator: callback(port_event) self._active_requests.remove(cancel_request)
def send_device_result(self, mac, device_result): """Send device result of a device to server""" devices_state = { 'device_mac_behaviors': { mac: { 'port_behavior': device_result } } } self._stub.ReportDevicesState(dict_proto(devices_state, DevicesState), timeout=self._rpc_timeout_sec)
def connect(self, mac, callback): """Connect to remote and start streaming port events back""" device = {"mac": mac} result_generator = self._stub.GetPortState(dict_proto(device, Device)) def cancel_request(): result_generator.cancel() self._active_requests.append(cancel_request) threading.Thread(target=self._port_event_handler, args=(callback, result_generator, cancel_request)).start()
def __init__(self, config, mqtt_factory=MqttManager): self._config = config.get('cloud_config', {}) cloud_config = utils.dict_proto(self._config, sys_config.CloudConfig) if not cloud_config.project_id: LOGGER.info('No project_id defined, skipping mqtt client creation') self._mqtt = None return assert mqtt_factory, 'missing mqtt_factory import/definition' LOGGER.info('Creating mqtt connection to %s/%s/%s', cloud_config.project_id, cloud_config.registry_id, cloud_config.device_id) self._mqtt = mqtt_factory(cloud_config, on_message=self._on_message) self._mqtt.loop_start()
def _verify_rule_counts(self, rule_counts, expected_rule_counts): self.assertEqual(rule_counts, dict_proto(expected_rule_counts, RuleCounts))
def get_device_rule_counts(self): """Return the rule counts for all the learned devices""" with self._lock: return dict_proto(self._get_device_rule_counts(), DeviceRuleCounts)