def physical_topology_get(): logger.info('GET {0} topology.'.format(const.TYPE_NW_PHYSICAL)) try: #open DB connection. tpldb_setup() #create <topology_list> xml_topology_list = Element(const.XML_TAG_TOPOL_LIST) nw_list = Network.query.filter(Network.type == const.TYPE_NW_PHYSICAL).all() for nw in nw_list: #add <topology type='slice' last_update_time='xxx' name='xxx' owner='xxx'> xml_topology = SubElement(xml_topology_list, const.XML_TAG_TOPOL, {const.XML_ATTR_TYPE:nw.type, const.XML_ATTR_LAST_UPD_TIME:str(nw.last_update_time), const.XML_ATTR_NAME:nw.network_name }) #create node xml xml_topology = create_node_xml(xml_topology, nw.network_name) #create link xml xml_topology = create_link_xml(xml_topology, nw.network_name) except Exception: logger.exception('GET physical topology error.') return HTTPResponse("GET physical topology error.", status=500) finally: #close DB connection. tpldb_close() return util.prettify(xml_topology_list)
def physical_topology_get(): logger.info('GET {0} topology.'.format(const.TYPE_NW_PHYSICAL)) try: #open DB connection. tpldb_setup() #create <topology_list> xml_topology_list = Element(const.XML_TAG_TOPOL_LIST) nw_list = Network.query.filter( Network.type == const.TYPE_NW_PHYSICAL).all() for nw in nw_list: #add <topology type='slice' last_update_time='xxx' name='xxx' owner='xxx'> xml_topology = SubElement( xml_topology_list, const.XML_TAG_TOPOL, { const.XML_ATTR_TYPE: nw.type, const.XML_ATTR_LAST_UPD_TIME: str(nw.last_update_time), const.XML_ATTR_NAME: nw.network_name }) #create node xml xml_topology = create_node_xml(xml_topology, nw.network_name) #create link xml xml_topology = create_link_xml(xml_topology, nw.network_name) except Exception: logger.exception('GET physical topology error.') return HTTPResponse("GET physical topology error.", status=500) finally: #close DB connection. tpldb_close() return util.prettify(xml_topology_list)
def topology_list(): logger.info('GET topology list.') try: #open DB connection. tpldb_setup() #create <topology_list> xml_topology_list = Element(const.XML_TAG_TOPOL_LIST) nw_list = Network.query.all() for nw in nw_list: #add <topology type='slice' last_update_time='xxx' name='xxx' owner='xxx'> xml_topology = SubElement(xml_topology_list, const.XML_TAG_TOPOL, {const.XML_ATTR_TYPE:nw.type, const.XML_ATTR_LAST_UPD_TIME:str(nw.last_update_time), const.XML_ATTR_NAME:nw.network_name }) if nw.type == const.TYPE_NW_SLICE: xml_topology.set(const.XML_ATTR_OWN,nw.user) except Exception: logger.exception('GET topology list error.') return HTTPResponse("GET topology list error.", status=500) finally: #close DB connection. tpldb_close() return util.prettify(xml_topology_list)
def topology_list(): logger.info('GET topology list.') try: #open DB connection. tpldb_setup() #create <topology_list> xml_topology_list = Element(const.XML_TAG_TOPOL_LIST) nw_list = Network.query.all() for nw in nw_list: #add <topology type='slice' last_update_time='xxx' name='xxx' owner='xxx'> xml_topology = SubElement( xml_topology_list, const.XML_TAG_TOPOL, { const.XML_ATTR_TYPE: nw.type, const.XML_ATTR_LAST_UPD_TIME: str(nw.last_update_time), const.XML_ATTR_NAME: nw.network_name }) if nw.type == const.TYPE_NW_SLICE: xml_topology.set(const.XML_ATTR_OWN, nw.user) except Exception: logger.exception('GET topology list error.') return HTTPResponse("GET topology list error.", status=500) finally: #close DB connection. tpldb_close() return util.prettify(xml_topology_list)
def create_monitoring_data_xml(logger,all_md_list): try: # create <monitoring-data> xml_md = Element(const.XML_TAG_MON_DATA) # add <topology_list> xml_topol_list = SubElement(xml_md, const.XML_TAG_TOPOL_LIST) ### md_dict={type:switch,network_name:xxx,node_name:xxx, ### port:xxx,val_list:list(val_dict[param_name:value])} for md_dict in all_md_list: xml_topology = xml_topol_list.find(".//{0}[@{1}='{2}']" .format(const.XML_TAG_TOPOL,const.XML_ATTR_NAME,md_dict['network_name'])) if xml_topology is None: # add <topology type='pyshical' name='xxx'> xml_topology = SubElement(xml_topol_list, const.XML_TAG_TOPOL, {const.XML_ATTR_TYPE:const.TYPE_NW_PHYSICAL, const.XML_ATTR_NAME:md_dict['network_name'] }) xml_node = xml_topology.find(".//{0}[@{1}='{2}']" .format(const.XML_TAG_NODE,const.XML_ATTR_ID,md_dict['node_name'])) if xml_node is None: # add <node id='xxx' type='xxx'> xml_node = SubElement(xml_topology, const.XML_TAG_NODE, {const.XML_ATTR_ID:md_dict['node_name'], const.XML_ATTR_TYPE:md_dict['type'] }) xml_port = xml_node.find(".//{0}[@{1}='{2}']" .format(const.XML_TAG_PORT,const.XML_ATTR_NUM,md_dict['port'])) if xml_port is None: # add <port num='xxx'> xml_port = SubElement(xml_node, const.XML_TAG_PORT, {const.XML_ATTR_NUM:md_dict['port']}) for val_dict in md_dict['val_list']: timestamp = val_dict['timestamp'] for key, value in val_dict.iteritems(): if key == 'timestamp': continue xml_param = xml_port.find(".//{0}[@{1}='{2}']" .format(const.XML_TAG_PARAM,const.XML_ATTR_TYPE,key)) if xml_param is None: # add <parameter type='xxx'> xml_param = SubElement(xml_port, const.XML_TAG_PARAM, {const.XML_ATTR_TYPE:key}) # add <value timestamp='xxx'>xxx</value> SubElement(xml_param, const.XML_TAG_VAL, {const.XML_ATTR_TIME_STAMP:timestamp}).text = value except Exception: logger.exception('create_monitoring_data_xml') return '' return util.prettify(xml_md)
def create_monitoring_data_xml(logger, all_md_list): try: # create <monitoring-data> xml_md = Element(const.XML_TAG_MON_DATA) # add <topology_list> xml_topol_list = SubElement(xml_md, const.XML_TAG_TOPOL_LIST) ### md_dict={nw_name:xxx,nw_type,node_name:xxx,node_type:server|vm, ### val_list:list(val_dict[param_name:value])} for md_dict in all_md_list: xml_topology = xml_topol_list.find(".//{0}[@{1}='{2}']".format( const.XML_TAG_TOPOL, const.XML_ATTR_NAME, md_dict['network_name'])) if xml_topology is None: # add <topology type='pyshical | slice' name='xxx'> xml_topology = SubElement( xml_topol_list, const.XML_TAG_TOPOL, { const.XML_ATTR_TYPE: md_dict['network_type'], const.XML_ATTR_NAME: md_dict['network_name'] }) xml_node = xml_topology.find(".//{0}[@{1}='{2}']".format( const.XML_TAG_NODE, const.XML_ATTR_ID, md_dict['node_name'])) if xml_node is None: # add <node id='xxx' type='xxx'> xml_node = SubElement( xml_topology, const.XML_TAG_NODE, { const.XML_ATTR_ID: md_dict['node_name'], const.XML_ATTR_TYPE: md_dict['node_type'] }) for val_dict in md_dict['val_list']: timestamp = val_dict['timestamp'] for key, value in val_dict.iteritems(): if key == 'timestamp': continue xml_param = xml_node.find(".//{0}[@{1}='{2}']".format( const.XML_TAG_PARAM, const.XML_ATTR_TYPE, key)) if xml_param is None: # add <parameter type='xxx'> xml_param = SubElement(xml_node, const.XML_TAG_PARAM, {const.XML_ATTR_TYPE: key}) # add <value timestamp='xxx'>xxx</value> SubElement(xml_param, const.XML_TAG_VAL, { const.XML_ATTR_TIME_STAMP: timestamp }).text = value except Exception: logger.exception('create_monitoring_data_xml') return '' return util.prettify(xml_md)
def slice_topology_get(slice_id="default_slice_id"): logger.info('GET {0} topology.(slice_id={1})'.format( const.TYPE_NW_SLICE, slice_id)) try: # lock -start- with util.semaphore_tpl: #open DB connection. tpldb_setup() #create <topology_list> xml_topology_list = Element(const.XML_TAG_TOPOL_LIST) #add <network type='slice' last_update_time='xxx'> nw = Network.query.filter(Network.type == const.TYPE_NW_SLICE)\ .filter(Network.network_name == slice_id).first() if not nw: return util.prettify(xml_topology_list) #add <topology type='slice' last_update_time='xxx' name='xxx' owner='xxx'> xml_topology = SubElement( xml_topology_list, const.XML_TAG_TOPOL, { const.XML_ATTR_TYPE: nw.type, const.XML_ATTR_LAST_UPD_TIME: str(nw.last_update_time), const.XML_ATTR_NAME: nw.network_name }) #create node xml create_node_xml(xml_topology, slice_id) #create link xml create_link_xml(xml_topology, slice_id) # lock -end- except Exception: logger.exception('GET slice topology error.') return HTTPResponse("GET slice topology error.", status=500) finally: #close DB connection. tpldb_close() return util.prettify(xml_topology_list)
def create_monitoring_data_xml(logger,all_md_list): try: # create <monitoring-data> xml_md = Element(const.XML_TAG_MON_DATA) # add <topology_list> xml_topol_list = SubElement(xml_md, const.XML_TAG_TOPOL_LIST) ### md_dict={nw_name:xxx,nw_type:slice,link_name:xxx,link_type:tn, ### val_list:list(val_dict[param_name:value])} for md_dict in all_md_list: xml_topology = xml_topol_list.find(".//{0}[@{1}='{2}']" .format(const.XML_TAG_TOPOL,const.XML_ATTR_NAME,md_dict['network_name'])) if xml_topology is None: # add <topology type='slice' name='xxx'> xml_topology = SubElement(xml_topol_list, const.XML_TAG_TOPOL, {const.XML_ATTR_TYPE:md_dict['network_type'], const.XML_ATTR_NAME:md_dict['network_name'] }) xml_link = xml_topology.find(".//{0}[@{1}='{2}']" .format(const.XML_TAG_LINK,const.XML_ATTR_ID,md_dict['link_name'])) if xml_link is None: # add <link id='xxx' type='xxx'> xml_link = SubElement(xml_topology, const.XML_TAG_LINK, {const.XML_ATTR_ID:md_dict['link_name'], const.XML_ATTR_TYPE:md_dict['link_type'] }) for val_dict in md_dict['val_list']: timestamp = val_dict['timestamp'] for key, value in val_dict.iteritems(): if key == 'timestamp': continue xml_param = xml_link.find(".//{0}[@{1}='{2}']" .format(const.XML_TAG_PARAM,const.XML_ATTR_TYPE,key)) if xml_param is None: # add <parameter type='xxx'> xml_param = SubElement(xml_link, const.XML_TAG_PARAM, {const.XML_ATTR_TYPE:key}) # add <value timestamp='xxx'>xxx</value> SubElement(xml_param, const.XML_TAG_VAL, {const.XML_ATTR_TIME_STAMP:timestamp}).text = value except Exception: logger.exception('create_monitoring_data_xml') return '' return util.prettify(xml_md)
def slice_topology_get( slice_id="default_slice_id" ): logger.info('GET {0} topology.(slice_id={1})'.format(const.TYPE_NW_SLICE,slice_id)) try: # lock -start- with util.semaphore_tpl: #open DB connection. tpldb_setup() #create <topology_list> xml_topology_list = Element(const.XML_TAG_TOPOL_LIST) #add <network type='slice' last_update_time='xxx'> nw = Network.query.filter(Network.type == const.TYPE_NW_SLICE)\ .filter(Network.network_name == slice_id).first() if not nw: return util.prettify(xml_topology_list) #add <topology type='slice' last_update_time='xxx' name='xxx' owner='xxx'> xml_topology = SubElement(xml_topology_list, const.XML_TAG_TOPOL, {const.XML_ATTR_TYPE:nw.type, const.XML_ATTR_LAST_UPD_TIME:str(nw.last_update_time), const.XML_ATTR_NAME:nw.network_name }) #create node xml create_node_xml(xml_topology, slice_id) #create link xml create_link_xml(xml_topology, slice_id) # lock -end- except Exception: logger.exception('GET slice topology error.') return HTTPResponse("GET slice topology error.", status=500) finally: #close DB connection. tpldb_close() return util.prettify(xml_topology_list)
def monitoring_cp_get(topology_type=""): logger.info('GET monitoring-data({0}). -start-'.format(const.TYPE_MON_CP)) # database handle. db_handle = DBHandle() try: # create <monitoring-data> xml_mon_data = Element(const.XML_TAG_MON_DATA) # topology_type==[physical | slice] logger.debug('topology type=' + topology_type) # param_type_list==[status | cpu_load | etc...] param_type_list = request.query.getall(const.HTTP_GET_OPT_TYPE) if param_type_list: logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_TYPE +')=' + str(param_type_list)) # get HTTP GET option. param_topol = request.query.get(const.HTTP_GET_OPT_TOPOL) if param_topol: logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_TOPOL +')=' + param_topol) param_node = request.query.get(const.HTTP_GET_OPT_NODE) if param_node: logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_NODE +')=' + param_node) param_stime = request.query.get(const.HTTP_GET_OPT_STIME) if param_stime: logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_STIME +')=' + param_stime) param_etime = request.query.get(const.HTTP_GET_OPT_ETIME) if param_etime: logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_ETIME +')=' + param_etime) param_limit = request.query.get(const.HTTP_GET_OPT_LMT) if param_limit: logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_LMT +')=' + param_limit) else: # set the default value if not specified. param_limit = str(const.DEFAULT_LIMIT) logger.debug('HTTP GET option ('+ const.HTTP_GET_OPT_LMT +')=default value(' + param_limit +')') # required check.(topology_type) if not topology_type == const.TYPE_NW_PHYSICAL and \ not topology_type == const.TYPE_NW_SLICE: logger.warn('topology type is invalid. ('+ topology_type +')') return HTTPResponse("GET monitoring-data({0}) error.(topology type({1}) is invalid.)" .format(const.TYPE_MON_CP,topology_type), status=400) # required check.(HTTP GET option) if not param_type_list: logger.warn('Required HTTP GET option({0}) have not been set.'.format(const.HTTP_GET_OPT_TYPE)) return HTTPResponse("GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)" .format(const.TYPE_MON_CP,const.HTTP_GET_OPT_TYPE), status=400) if not param_stime: logger.warn('Required HTTP GET option({0}) have not been set.'.format(const.HTTP_GET_OPT_STIME)) return HTTPResponse("GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)" .format(const.TYPE_MON_CP,const.HTTP_GET_OPT_STIME), status=400) if not param_etime: logger.warn('Required HTTP GET option({0}) have not been set.'.format(const.HTTP_GET_OPT_ETIME)) return HTTPResponse("GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)" .format(const.TYPE_MON_CP,const.HTTP_GET_OPT_ETIME), status=400) # lock -start- with util.semaphore_md: # open topology database connection. tpldb_setup() # required check.(HTTP GET option:topology) nw_list = [] if not param_topol: # If the topology type is 'slice', topology(sliceID) is required if topology_type == const.TYPE_NW_SLICE: logger.warn('Required HTTP GET option({0}) have not been set.'.format(const.HTTP_GET_OPT_TOPOL)) return HTTPResponse("GET monitoring-data({0}) error.(Required HTTP GET option({1}) not specified.)" .format(const.TYPE_MON_CP,const.HTTP_GET_OPT_TOPOL), status=400) # If the topology type is 'physical',target all network. elif topology_type == const.TYPE_NW_PHYSICAL: nw_list = get_all_network(const.TYPE_NW_PHYSICAL) # Without network specified, if the node is specified, the value of the node are ignored. param_node = None else: tmp_nw = Network.query.filter(Network.network_name == param_topol).first() if tmp_nw: nw_list.append(tmp_nw) # search target table name. tbl_name_list = search_target_table(logger,int(param_stime),int(param_etime),config.mon_data_cp_db) # add <topology_list> xml_topol_list = SubElement(xml_mon_data, const.XML_TAG_TOPOL_LIST) # connect to the database. db_con = db_handle.connect(config.mon_data_cp_db,config.db_addr ,config.db_port,config.db_user,config.db_pass) for nw in nw_list: # add <topology type=topology_type name='xxx'> xml_topol = SubElement(xml_topol_list, const.XML_TAG_TOPOL, {const.XML_ATTR_TYPE:nw.type,const.XML_ATTR_NAME:nw.network_name}) node_list = [] if not param_node: # not specified. cover all of the node. if topology_type == const.TYPE_NW_SLICE: node_list = get_all_vm(nw) # If the topology type is 'physical',target all network. elif topology_type == const.TYPE_NW_PHYSICAL: node_list = get_all_srv(nw) else: if topology_type == const.TYPE_NW_SLICE: tmp_node = Node.query.filter(Node.node_name == param_node)\ .filter(Node.network_name == nw.network_name)\ .filter(Node.type == const.TYPE_NODE_VM).first() # If the topology type is 'physical',target all network. elif topology_type == const.TYPE_NW_PHYSICAL: tmp_node = Node.query.filter(Node.node_name == param_node)\ .filter(Node.network_name == nw.network_name)\ .filter(Node.type == const.TYPE_NODE_SRV).first() if tmp_node: node_list.append(tmp_node) for node in node_list: # add <node id='xxx' type='switch'> xml_node = SubElement(xml_topol, const.XML_TAG_NODE, {const.XML_ATTR_ID:node.node_name, const.XML_ATTR_TYPE:node.type }) for param_type in param_type_list: # add <parameter type=param_type> xml_param = SubElement(xml_node, const.XML_TAG_PARAM, {const.XML_ATTR_TYPE:param_type}) # no data in the search time-range. if not tbl_name_list: continue # search database.(metaID) sql = "(SELECT metaID FROM metaData " \ + "WHERE network_name='{0}' AND node_name='{1}' AND type='{2}')"\ .format(nw.network_name,node.node_name,param_type) logger.debug(sql) db_con.execute(sql) res_metaid = db_con.fetchone() if res_metaid: metaid = res_metaid['metaID'] else: continue # search database.(data) sql_base = "(SELECT timestamp,value FROM {0} WHERE metaID={1}"\ + " AND timestamp >= {0}".format(param_stime)\ + " AND timestamp <= {0})".format(param_etime) sql_list = [] for tbl_name in tbl_name_list: sql_list.append(sql_base.format(tbl_name,metaid)) sql = " UNION ".join(sql_list) sql += " ORDER BY timestamp DESC LIMIT " + param_limit logger.debug(sql) db_con.execute(sql) res_mon_data = db_con.fetchall() for mon_data in res_mon_data: # add <value timestamp=mon_data["timestamp"]> xml_value = SubElement(xml_param, const.XML_TAG_VAL, {const.XML_ATTR_TIME_STAMP:str(mon_data["timestamp"])}) # add <value>mon_data["value"]</value> tmp_value = util.isinteger(float(mon_data["value"])) xml_value.text = str(tmp_value) # lock -end- except Exception: logger.exception('GET monitoring-data({0}) error.'.format(const.TYPE_MON_CP)) return HTTPResponse("GET monitoring-data({0}) error." .format(const.TYPE_MON_CP), status=500) finally: # close monitoring database connection. db_handle.close() # close topology database connection. tpldb_close() logger.info('GET monitoring-data({0}). -end-'.format(const.TYPE_MON_CP)) return util.prettify(xml_mon_data)