def init_env(options): signal.signal(signal.SIGINT, handle_exit_signal) signal.signal(signal.SIGTERM, handle_exit_signal) signal.signal(signal.SIGQUIT, handle_exit_signal) pipe.create_runtime_folder() check_single_instance() run_env.acquire_outside_env() lflow.init_build_flows_clause(extra['options']) system_id = cm.system_id() if system_id is None or system_id == "": logger.error('Openvswitch has no chassis id') clean_env(extra, -1) sys.exit(-1) extra['system_id'] = system_id +lgview.local_system_id(system_id) logic = pyDatalog.Logic(True) extra['logic'] = logic create_watch_master(options.host, options.path_prefix, system_id) cm.remove_all_tunnles() cm.build_br_integration() cm.clean_ovs_flows() cm.set_tunnel_tlv() cm.set_upcall_rate()
def monitor_ovsdb(entity_zoo, extra): pyDatalog.Logic(extra['logic']) cmd = [ 'ovsdb-client', 'monitor', 'Interface', 'ofport', 'name', 'external_ids', '--format=json' ] logger.info("start ovsdb-client instance") try: child = subprocess.Popen(cmd, stdout=subprocess.PIPE, preexec_fn=on_parent_exit('SIGTERM')) with extra['lock']: extra['ovsdb-client'] = child logger.info("monitoring the ovsdb") while child.poll() == None: json_str = child.stdout.readline().strip() output = json.loads(json_str) with entity_zoo.lock: for record in output['data']: update_ovsport(record, entity_zoo) except ValueError as err: if json_str != "": logger.warning("cannot parse %s to json object", json_str) else: logger.info('json_str is empty, maybe we should exit') subprocess.Popen.kill(child) return except Exception as err: logger.exception("exit ovsdb-client monitor, err:%s", str(err)) subprocess.Popen.kill(child) return
def listing(a, b, word2, idiom2, console, data, relations): """All idioms realted to a word""" pyDatalog.Logic(data) console.print("Idioms related to " + word2 + "(" + idiom2 + ") ?") if word2 == "" or idiom2 == "": return "Fill all spaces, please." query = createQuery("", "", word2, idiom2, "listing", relations) return executeQuery(query, console)
def originated(a, idiom1, word2, idiom2, console, data, relations): """All words in idiom1 originated from a word""" pyDatalog.Logic(data) console.print("Words in " + idiom1 + " originated from " + word2 + "(" + idiom2 + ") ?") if word2 == "" or idiom2 == "" or idiom1 == "": return "Fill all spaces, please." query = createQuery("", idiom1, word2, idiom2, "originated", relations) return executeQuery(query, console)
def related(a, idiom1, word2, idiom2, console, data, relations): """If a word is related to an idiom""" pyDatalog.Logic(data) console.print("Is " + idiom1 + " related to " + word2 + " (" + idiom2 + ") ?") if idiom1 == "" or word2 == "" or idiom2 == "": return "Fill all spaces, please." query = createQuery("", idiom1, word2, idiom2, "related", relations) return executeQuery(query, console)
def uncle(word1, idiom1, word2, idiom2, console, data, relations): """Checks if the first word (Son) is the second word's (Father) child""" pyDatalog.create_terms('console') pyDatalog.Logic(data) if word1=="" or word2=="": return "Fill all the spaces, please." query = createQuery(word1, idiom1, word2, idiom2, "uncle", relations) print("Query: ",query) return executeQuery(query, console)
def brother(word1, idiom1, word2, idiom2, console, data, relations): """Checks if two given words are brothers in the current data's context""" pyDatalog.create_terms('console') pyDatalog.Logic(data) if word1=="" or word2=="": return "Fill all the spaces, please." query = createQuery(word1, idiom1, word2, idiom2, "brother", relations) print("Query: ",query) return executeQuery(query, console)
def cousin_level(word1, idiom1, word2, idiom2, console, data, relations): """Checks if two given words are cousins and returns their cousin level""" pyDatalog.create_terms('console') pyDatalog.Logic(data) pyDatalog.create_terms('console') if word1=="" or word2=="": return "Fill all the spaces, please." query = createQuery(word1, idiom1, word2, idiom2, "cousin_level", relations) print("Query: ",query) return executeQuery(query, console)
def monitor_pkt_controller_tunnel(ez, extra): # make global entity_zoo can be accessed global entity_zoo entity_zoo = ez try: pyDatalog.Logic(extra['logic']) run_pkt_controller_instance() fd = pipe.create_pkt_controller_tunnel() while True: msg = os.read(fd, MAX_BUF_LEN) if msg == '': logger.info('receive no msg, maybe pkt_controller is down') return parse_pkt_controller_msg(msg) except Exception as err: logger.warning("hit unknow error, exit monitoring pkt_controller:%s", err)
def init_env(options): signal.signal(signal.SIGINT, handle_exit_signal) signal.signal(signal.SIGTERM, handle_exit_signal) signal.signal(signal.SIGQUIT, handle_exit_signal) pipe.create_runtime_folder() check_single_instance() system_id = cm.system_id() if system_id is None or system_id == "": logger.error('openvswitch has no chassis id') killme() extra['system_id'] = system_id logic = pyDatalog.Logic(True) extra['logic'] = logic br_int_mac = cm.build_br_integration() extra['options']['br-int_mac'] = br_int_mac if extra['options'].has_key('ENABLE_UNTUNNEL'): config = {'net.ipv4.conf.all.rp_filter': '0'} config['net.ipv4.ip_forward'] = '1' config['net.ipv4.conf.br-int.rp_filter'] = '0' config['net.ipv4.conf.br-int.forwarding'] = '1' if _correct_sysctl_config(config) is False: logger.error('failed to correct sysctl config:%s', config) killme() try: br = 'br-int' syscmd.network_ifup(br) logger.info('ifup the interface %s', br) except Exception as err: logger.error('failed to ifup %s interface, err:', br, err) killme() +lgview.local_system_id(system_id) lflow.init_build_flows_clause(extra['options']) try: cm.insert_ovs_ipfix() cm.set_tunnel_tlv() create_watch_master(options.host, options.path_prefix, system_id) except Exception as err: logger.error("hit error in init_env, err:%s", err) killme()
def get_logic(self): """Getter for the pyDatalog Logic""" if self.debug: print("Getting logic") return pyDatalog.Logic(True)
def clear(self): """Clears the current data""" pyDatalog.Logic() # Initializes Logic context for this thread self.included_files = "" self.data = ""
def idiom_list(a, b, idiom1, d, console, data, relations): """List all idioms that were contributed (show percentage)""" pyDatalog.Logic(data) query = createQuery(idiom1, "", "idiom_list", relations, console) return executeQuery(query, console, "idiom_list")
def contributed_most(a, b, idiom1, d, console, data, relations): """Idioms that contributed more to the other""" pyDatalog.Logic(data) query = createQuery(idiom1, "", "contributed_most", relations, console) return executeQuery(query, console, "contributed_most")
def common(a, b, idiom1, idiom2, console, data, relations): """Lists all common words between two idioms""" pyDatalog.Logic(data) query = createQuery(idiom1, idiom2, "common", relations, console) return executeQuery(query, console, "common")