def post (self, rpc=None): if rpc == 'ping': LOG.debug('ping received') self.write('OK') elif rpc == 'get-config': LOG.debug('get-config received') with open(CONFIG_FILE) as f: nffg = NFFG.parse(f.read()) nffg.duplicate_static_links() self.write(nffg.dump()) return elif rpc == 'edit-config': # parse body if it contains a Nf-fg: nffg = None if self.request.body: nffg = NFFG.parse(self.request.body) if nffg is None: self.send_error(400) return LOG.debug('edit-config received: %s', nffg.dump()) return else: self.set_status(404) self.write_error(404)
def get_balanced_tree(r=2, h=3, seed=0, max_cpu=4, max_mem=1600, max_storage=3, max_link_bw=5, min_link_delay=2, abc_nf_types_len=10, max_link_delay=4): """ Gets a balanced tree which has SAPs in the root and the leaves, directed from the root to the leaves. :param r: branching factor of the tree :param h: height of the tree :return: NFFG """ nf_types = list(string.ascii_uppercase)[:abc_nf_types_len] nffg = NFFG(id="req-tree-branching-" + str(r) + "-height-" + str(h)) nffg.mode = NFFG.MODE_ADD rnd = random.Random() rnd.seed(seed) gen = NameGenerator() sap_obj = nffg.add_sap(id=gen.get_name("sap")) prev_level_nf_ports = [sap_obj.add_port(id=gen.get_name("port"))] for level in xrange(0, h): curr_level_nf_ports = [] for prev_level_port in prev_level_nf_ports: for j in xrange(0, r): nf = nffg.add_nf(id=gen.get_name("nf"), func_type=rnd.choice(nf_types), cpu=rnd.random() * max_cpu, mem=rnd.random() * max_mem, storage=rnd.random() * max_storage) nffg.add_sglink(prev_level_port, nf.add_port(gen.get_name("port")), id=gen.get_name("sghop")) curr_level_nf_ports.append(nf.add_port(gen.get_name("port"))) prev_level_nf_ports = curr_level_nf_ports for port in prev_level_nf_ports: sap = nffg.add_sap(id=gen.get_name("sap")) nffg.add_sglink(port, sap.add_port(id=gen.get_name("port")), id=gen.get_name("delay_sghop"), delay=rnd.uniform(min_link_delay, max_link_delay), bandwidth=rnd.random() * max_link_bw) return nffg
def _calculate_diffs(old_path, new_path): """ Calculate and print the difference of the two :class:`NFFG` given by it"s path. :param old_path: file path of the original :class:`NFFG` :param new_path: file path of the modified :class:`NFFG` :return: None """ print "Calculate the difference NFFGs..." old = NFFG.parse_from_file(old_path) NFFGToolBox.recreate_all_sghops(nffg=old) new = NFFG.parse_from_file(new_path) NFFGToolBox.recreate_all_sghops(nffg=new) add_nffg, del_nffg = NFFGToolBox.generate_difference_of_nffgs( old=old, new=new, ignore_infras=True) print "\nADD NFFG:" print add_nffg.dump() print "\nDEL NFFG:" print del_nffg.dump()
def networkx_request_generator (func_name, seed=0, max_cpu=4, max_mem=1600, max_storage=3, max_link_bw=7, abc_nf_types_len=10, max_link_delay=2, sap_cnt=10, **kwargs): rnd = random.Random() rnd.seed(seed) nx_graph = get_networkx_func(func_name, seed=seed, **kwargs) nf_types = list(string.ascii_uppercase)[:abc_nf_types_len] nffg = NFFG(id="req-" + func_name + "-seed" + str(seed)) nffg.mode = NFFG.MODE_ADD for nf_id in nx_graph.nodes_iter(): nf = nffg.add_nf(id=nf_id, func_type=rnd.choice(nf_types), cpu=rnd.random() * max_cpu, mem=rnd.random() * max_mem, storage=rnd.random() * max_storage) for i, j in nx_graph.edges_iter(): """TODO: How to direct the randomly generated graph's edges.""" pass return nffg
def timerHandler(self): new_nffg = None if self.__viewer.url is not None: n = self.__http_client.getNFFG(self.__viewer.url) if n is not None: if str(n).startswith("<?xml"): new_nffg = self.__converter.parse_from_Virtualizer(n) else: new_nffg = NFFG.parse(n) if new_nffg is not None: new_nffg.merge_duplicated_links() self.__process_NFFG(new_nffg) self.__viewer.canvas.dataG = self.__network self.__viewer.refresh() self.__timer = threading.Timer(1, self.timerHandler) self.__timer.start()
def networkx_resource_generator (func_name, seed=0, max_cpu=40, max_mem=16000, max_storage=30, max_link_bw=70, abc_nf_types_len=10, supported_nf_cnt=6, max_link_delay=2, sap_cnt=10, **kwargs): """ Uses a NetworkX graph to create a request NFFG. :param func_name: string of NetworkX lib's random graph generator function name :param seed: :param max_cpu: :param max_mem: :param max_storage: :param max_link_bw: :param abc_nf_types_len: :param supported_nf_cnt: :param max_link_delay: :param sap_cnt: :param kwargs: :return: """ rnd = random.Random() rnd.seed(seed) nx_graph = get_networkx_func(func_name, seed=seed, **kwargs) nf_types = list(string.ascii_uppercase)[:abc_nf_types_len] nffg = NFFG(id="net-" + func_name + "-seed" + str(seed)) gen = NameGenerator() for infra_id in nx_graph.nodes_iter(): infra = nffg.add_infra(id=infra_id, bandwidth=rnd.random() * max_link_bw * 1000, cpu=rnd.random() * max_cpu, mem=rnd.random() * max_mem, storage=rnd.random() * max_storage) infra.add_supported_type(rnd.sample(nf_types, supported_nf_cnt)) for i, j in nx_graph.edges_iter(): infra1 = nffg.network.node[i] infra2 = nffg.network.node[j] nffg.add_undirected_link(port1=infra1.add_port(id=gen.get_name("port")), port2=infra2.add_port(id=gen.get_name("port")), p1p2id=gen.get_name("link"), p2p1id=gen.get_name("link"), dynamic=False, delay=rnd.random() * max_link_delay, bandwidth=rnd.random() * max_link_bw) infra_ids = [i.id for i in nffg.infras] for s in xrange(0, sap_cnt): sap_obj = nffg.add_sap(id=gen.get_name("sap")) sap_port = sap_obj.add_port(id=gen.get_name("port")) infra_id = rnd.choice(infra_ids) infra = nffg.network.node[infra_id] nffg.add_undirected_link(port1=sap_port, port2=infra.add_port(id=gen.get_name("port")), p1p2id=gen.get_name("link"), p2p1id=gen.get_name("link"), dynamic=False, delay=rnd.random() * max_link_delay, bandwidth=rnd.uniform(max_link_bw / 2.0, max_link_bw)) return nffg
os.path.abspath(os.path.join(os.path.dirname(__file__), p))) from nffg import NFFG from conversion import NFFGConverter if __name__ == '__main__': logging.basicConfig(level=logging.DEBUG) graph = None if len(sys.argv) > 1: nffg_path = os.path.abspath(os.path.join(os.getcwd(), sys.argv[1])) with open(nffg_path, 'r') as f: raw = f.read() if raw.startswith("<?xml"): converter = NFFGConverter(logger=logging.getLogger(__name__)) graph = converter.parse_from_Virtualizer(raw) else: graph = NFFG.parse(raw) else: # creating basic network graph G = nx.complete_graph(4) iterator = G.nodes_iter() for i in iterator: # creating a dict for storing node parameters # TODO create data structure according to NFFG model G.node[i]['type'] = 'sap' G.node[i]['color'] = 'blue' G.node[i]['pattern'] = 'outline' # creating thread # t_view = vt.ViewerThread(viewer_type="get", graph=G) t_view = vt.ViewerThread(viewer_type="get", graph=graph) t_view.start() t_view.join()
def get_8loop_request(abc_nf_types_len=10, seed=0, eightloops=1): """ Generates simple request NFFGs in all combinations of sap1-->vnf1-->...--> vnfn-->sap1. Creates the requests for augmented-dfn-gwin.nffg :param abc_nf_types_len: list of VNF **Types** which should be instantiated :type abc_nf_types_len: list :param seed: seed for random generator :type seed: int :param eightloops: the number of eight loops :type eightloops: int :return: an 8loop NFFG :rtype: :any:`NFFG` """ saps = [] for i in xrange(0, 20): saps.append("sap" + str(i)) rnd = random.Random() rnd.seed(seed) gen = NameGenerator() nffg = NFFG(id="8loops-req") nffg.mode = NFFG.MODE_ADD nf_types = list(string.ascii_uppercase)[:abc_nf_types_len] i = 1 for j in xrange(0, eightloops): sap = rnd.choice(saps) if sap not in nffg: sapo = nffg.add_sap(id=sap, name=sap + "_name") else: sapo = nffg.network.node[sap] if len(sapo.ports) > 0: for sapp in sapo.ports: break else: sapp = sapo.add_port(id=gen.get_name("port")) vnfs1 = rnd.sample(nf_types, rnd.randint(1, len(nf_types))) vnfs2 = rnd.sample(nf_types, rnd.randint(1, len(nf_types))) nfmiddle = nffg.add_nf(id="nf0" + str(j), name="nf_middle" + str(j), func_type=rnd.choice(vnfs1), cpu=1, mem=1, storage=1) try: vnfs1.remove(nfmiddle.functional_type) except ValueError: pass try: vnfs2.remove(nfmiddle.functional_type) except ValueError: pass once = True for vnf_list in (vnfs1, vnfs2): nf0 = nfmiddle for vnf in vnf_list: nf1 = nffg.add_nf(id="-".join(("nf", str(j), str(i))), name="nf" + str(i) + "_" + vnf, func_type=vnf, cpu=1, mem=1, storage=1) nffg.add_sglink(src_port=nf0.add_port(id=gen.get_name("port")), dst_port=nf1.add_port(id=gen.get_name("port")), flowclass="HTTP", id=i) nf0 = nf1 i += 1 if once: nffg.add_sglink( src_port=nf0.add_port(id=gen.get_name("port")), dst_port=nfmiddle.add_port(id=gen.get_name("port")), flowclass="HTTP", id=i) once = False i += 1 nffg.add_sglink(src_port=nf1.add_port(id=gen.get_name("port")), dst_port=sapp, flowclass="HTTP", id=i) nffg.add_sglink(src_port=sapp, dst_port=nfmiddle.add_port(id=gen.get_name("port")), flowclass="HTTP", id=i + 1) i += 2 return nffg