def test_orchestrating(self):
        res_view = 'RES'
        chain_view = 'CHAIN'
        self.nm = NetworkGraphManager(auto_id=False,
                                      views=[chain_view, res_view],
                                      chain_view=chain_view,
                                      res_view=res_view)

        #build a basic chain network
        (chain_id, start_id, end_id) = self.nm.add_new_chain('start1', 'end1')
        vnf_node_id = self.nm.add_node(
            'vnf1',
            chain_view,
            node_type=NetworkGraphManager.NODE_TYPE_VNF,
            req={
                'cpu': 4,
                'mem': 4
            })
        vnf_node_id2 = self.nm.add_node(
            'vnf2',
            chain_view,
            node_type=NetworkGraphManager.NODE_TYPE_VNF,
            req={
                'cpu': 7,
                'mem': 4
            })

        self.nm.add_link(start_id, vnf_node_id, chain_view)
        self.nm.add_link(vnf_node_id, vnf_node_id2, chain_view)
        self.nm.add_link(vnf_node_id2, end_id, chain_view)

        #build a basic phy network
        phy1_id = self.nm.add_node(
            'phy1',
            res_view,
            node_type=NetworkGraphManager.NODE_TYPE_HOST,
            res={
                'cpu': 2,
                'mem': 3
            })

        phy2_id = self.nm.add_node(
            'phy2',
            res_view,
            node_type=NetworkGraphManager.NODE_TYPE_HOST,
            res={
                'cpu': 4,
                'mem': 3
            })

        self.nm.add_link(phy1_id, phy2_id, res_view)

        chain_g = self.nm.get_graph(chain_view)
        res_g = self.nm.get_graph(res_view)
        #start end end nodes are not presented in the physical view
        self.assertRaises(RuntimeError, Mapping.map, chain_g, res_g,
                          DefaultSorter)

        self.nm.add_node('start1',
                         res_view,
                         node_type=NetworkGraphManager.NODE_TYPE_SAP)
        self.nm.add_node('end1',
                         res_view,
                         node_type=NetworkGraphManager.NODE_TYPE_SAP)
        #start end end nodes are not connected in the physical view
        self.assertRaises(RuntimeError, Mapping.map, chain_g, res_g,
                          DefaultSorter)

        self.nm.add_link('start1', 'end1', res_view)
        pair_list = Mapping.map(chain_g, res_g, DefaultSorter)

        #Not enough resource available in res view, so we get an empty pair list
        self.assertFalse(pair_list)

        self.nm.modify_node(phy2_id, res_view, res={'cpu': 11, 'mem': 8})
        pair_list = Mapping.map(chain_g, res_g, DefaultSorter)
        self.assertIn((vnf_node_id, phy2_id), pair_list)
        self.assertIn((vnf_node_id2, phy2_id), pair_list)

        self.nm.modify_node(phy2_id, res_view, res={'cpu': 8, 'mem': 4})
        self.nm.modify_node(phy1_id, res_view, res={'cpu': 7, 'mem': 4})
        pair_list = Mapping.map(chain_g, res_g, DefaultSorter)
        self.assertIn((vnf_node_id, phy2_id), pair_list)
        self.assertIn((vnf_node_id2, phy1_id), pair_list)
    def test_orchestrating(self):
        res_view = 'RES'
        chain_view = 'CHAIN'
        self.nm = NetworkGraphManager(auto_id = False,
                                 views = [chain_view, res_view],
                                 chain_view = chain_view,
                                 res_view = res_view)

        #build a basic chain network
        (chain_id, start_id, end_id) = self.nm.add_new_chain('start1', 'end1')
        vnf_node_id = self.nm.add_node('vnf1', chain_view,
                                       node_type = NetworkGraphManager.NODE_TYPE_VNF,
                                       req = {'cpu': 4, 'mem': 4})
        vnf_node_id2 = self.nm.add_node('vnf2', chain_view,
                                        node_type = NetworkGraphManager.NODE_TYPE_VNF,
                                        req = {'cpu': 7, 'mem': 4})

        self.nm.add_link(start_id, vnf_node_id, chain_view)
        self.nm.add_link(vnf_node_id, vnf_node_id2, chain_view)
        self.nm.add_link(vnf_node_id2, end_id, chain_view)

        #build a basic phy network
        phy1_id = self.nm.add_node('phy1', res_view,
                           node_type = NetworkGraphManager.NODE_TYPE_HOST,
                           res = {'cpu': 2, 'mem': 3})

        phy2_id = self.nm.add_node('phy2', res_view,
                           node_type = NetworkGraphManager.NODE_TYPE_HOST,
                           res = {'cpu': 4, 'mem': 3})

        self.nm.add_link(phy1_id, phy2_id, res_view)

        chain_g = self.nm.get_graph(chain_view)
        res_g = self.nm.get_graph(res_view)
        #start end end nodes are not presented in the physical view
        self.assertRaises(RuntimeError, Mapping.map,
                          chain_g, res_g, DefaultSorter)

        self.nm.add_node('start1', res_view,
                         node_type = NetworkGraphManager.NODE_TYPE_SAP)
        self.nm.add_node('end1', res_view,
                         node_type = NetworkGraphManager.NODE_TYPE_SAP)
        #start end end nodes are not connected in the physical view
        self.assertRaises(RuntimeError, Mapping.map,
                          chain_g, res_g, DefaultSorter)

        self.nm.add_link('start1', 'end1', res_view)
        pair_list = Mapping.map(chain_g, res_g, DefaultSorter)

        #Not enough resource available in res view, so we get an empty pair list
        self.assertFalse(pair_list)

        self.nm.modify_node(phy2_id, res_view, res = {'cpu': 11, 'mem': 8})
        pair_list = Mapping.map(chain_g, res_g, DefaultSorter)
        self.assertIn((vnf_node_id, phy2_id), pair_list)
        self.assertIn((vnf_node_id2, phy2_id), pair_list)

        self.nm.modify_node(phy2_id, res_view, res = {'cpu': 8, 'mem': 4})
        self.nm.modify_node(phy1_id, res_view, res = {'cpu': 7, 'mem': 4})
        pair_list = Mapping.map(chain_g, res_g, DefaultSorter)
        self.assertIn((vnf_node_id, phy2_id), pair_list)
        self.assertIn((vnf_node_id2, phy1_id), pair_list)
    def test_orchestrator_helpers(self):
        res_view = self.def_views[1]

        self.nm.auto_id = False
        phy_s = self.nm.add_node('phy_s',
                                 res_view,
                                 node_type=NetworkGraphManager.NODE_TYPE_SAP)
        phy_t = self.nm.add_node('phy_t',
                                 res_view,
                                 node_type=NetworkGraphManager.NODE_TYPE_SAP)

        res_g = self.nm.get_graph(res_view)

        stnodes = Mapping.get_stnodes(res_g)
        #there is no path between nodes
        self.assertEqual(stnodes, [])

        self.nm.add_link(phy_s, phy_t, res_view)
        stnodes = Mapping.get_stnodes(res_g)
        self.assertEqual(stnodes, [(phy_s, phy_t)])

        self.nm.auto_id = True
        (view, s_node, t_node, nodes) = self._add_one_vnf_chain()

        g = self.nm.get_graph(view)

        stnodes = Mapping.get_stnodes(g)

        #check if SAPs are in stnodes list
        self.assertIn((t_node, s_node), stnodes)

        #only one endpoint pair in this basic chain view
        self.assertEqual(1, len(stnodes))

        #add another node, and check if the two new SAP is added
        (chain_id, s_node2, t_node2) = self.nm.add_new_chain()
        self.nm.add_link(s_node2, t_node2, view)

        stnodes = Mapping.get_stnodes(g)

        #check if SAPs are in stnodes list
        self.assertIn((s_node2, t_node2), stnodes)

        #now the view has two chain and two st pair
        self.assertEqual(2, len(stnodes))

        #add a vnf node with dummy type to the view
        dummy_id = self.nm.add_node(None, view, node_type='dummy')
        self.nm.add_link(s_node2, dummy_id, view)

        #add a normal vnf node to the view
        true_vnf = self.nm.add_node(
            None,
            view,
            node_type=NetworkGraphManager.NODE_TYPE_VNF,
            req={
                'cpu': 4,
                'mem': 3
            })
        self.nm.add_link(dummy_id, true_vnf, view)
        self.nm.add_link(true_vnf, t_node2, view)

        #add an unreachable vnf node to the view
        unreach_vnf = self.nm.add_node(
            None, view, node_type=NetworkGraphManager.NODE_TYPE_VNF)

        #add a broken chain
        (chain_id3, s_node3, t_node3) = self.nm.add_new_chain()
        broken_vnf = self.nm.add_node(
            None, view, node_type=NetworkGraphManager.NODE_TYPE_VNF)
        self.nm.add_link(s_node3, broken_vnf, view)

        stnodes = Mapping.get_stnodes(g)
        vnf_list = Mapping.get_accessible_vnf_list(stnodes, g)

        #dummy not in the vnf list, because its type
        self.assertNotIn(dummy_id, vnf_list)
        #broken not in the vnf list, because its chain is not valid
        self.assertNotIn(broken_vnf, vnf_list)
        #unreach not in the vnf list, because it is unreachable
        self.assertNotIn(unreach_vnf, vnf_list)

        self.assertIn(true_vnf, vnf_list)

        #Test, if we add a vnf to a phy node, resources are globally updated
        phy_view = self.def_views[1]
        phy_g = self.nm.get_graph(phy_view)
        phy_node1 = self.nm.add_node(None, phy_view, res={'cpu': 6, 'mem': 3})
        Mapping.add_vnf_to_host(true_vnf, phy_node1, g, phy_g)

        phy_node = self.nm.graphs[phy_view].node[phy_node1]
        self.assertEqual(2, phy_node['res']['cpu'])
        self.assertEqual(0, phy_node['res']['mem'])
    def test_orchestrator_helpers(self):
        res_view = self.def_views[1]

        self.nm.auto_id = False
        phy_s = self.nm.add_node('phy_s', res_view,
                                 node_type = NetworkGraphManager.NODE_TYPE_SAP)
        phy_t = self.nm.add_node('phy_t', res_view,
                                 node_type = NetworkGraphManager.NODE_TYPE_SAP)

        res_g = self.nm.get_graph(res_view)

        stnodes = Mapping.get_stnodes(res_g)
        #there is no path between nodes
        self.assertEqual(stnodes, [])

        self.nm.add_link(phy_s, phy_t, res_view)
        stnodes = Mapping.get_stnodes(res_g)
        self.assertEqual(stnodes, [(phy_s, phy_t)])


        self.nm.auto_id = True
        (view, s_node, t_node, nodes) = self._add_one_vnf_chain()

        g = self.nm.get_graph(view)

        stnodes = Mapping.get_stnodes(g)

        #check if SAPs are in stnodes list
        self.assertIn((t_node, s_node), stnodes)

        #only one endpoint pair in this basic chain view
        self.assertEqual(1, len(stnodes))

        #add another node, and check if the two new SAP is added
        (chain_id, s_node2, t_node2) = self.nm.add_new_chain()
        self.nm.add_link(s_node2, t_node2, view)

        stnodes = Mapping.get_stnodes(g)

        #check if SAPs are in stnodes list
        self.assertIn((s_node2, t_node2), stnodes)

        #now the view has two chain and two st pair
        self.assertEqual(2, len(stnodes))

        #add a vnf node with dummy type to the view
        dummy_id = self.nm.add_node(None, view, node_type = 'dummy')
        self.nm.add_link(s_node2, dummy_id, view)

        #add a normal vnf node to the view
        true_vnf = self.nm.add_node(None, view,
                                    node_type = NetworkGraphManager.NODE_TYPE_VNF,
                                    req = {'cpu': 4, 'mem': 3})
        self.nm.add_link(dummy_id, true_vnf, view)
        self.nm.add_link(true_vnf, t_node2, view)

        #add an unreachable vnf node to the view
        unreach_vnf = self.nm.add_node(None, view,
                                       node_type = NetworkGraphManager.NODE_TYPE_VNF)

        #add a broken chain
        (chain_id3, s_node3, t_node3) = self.nm.add_new_chain()
        broken_vnf = self.nm.add_node(None, view,
                                      node_type = NetworkGraphManager.NODE_TYPE_VNF)
        self.nm.add_link(s_node3, broken_vnf, view)


        stnodes = Mapping.get_stnodes(g)
        vnf_list = Mapping.get_accessible_vnf_list(stnodes, g)

        #dummy not in the vnf list, because its type
        self.assertNotIn(dummy_id, vnf_list)
        #broken not in the vnf list, because its chain is not valid
        self.assertNotIn(broken_vnf, vnf_list)
        #unreach not in the vnf list, because it is unreachable
        self.assertNotIn(unreach_vnf, vnf_list)

        self.assertIn(true_vnf, vnf_list)

        #Test, if we add a vnf to a phy node, resources are globally updated
        phy_view = self.def_views[1]
        phy_g = self.nm.get_graph(phy_view)
        phy_node1 = self.nm.add_node(None, phy_view, res = {'cpu': 6, 'mem': 3})
        Mapping.add_vnf_to_host(true_vnf, phy_node1, g, phy_g)

        phy_node = self.nm.graphs[phy_view].node[phy_node1]
        self.assertEqual(2, phy_node['res']['cpu'])
        self.assertEqual(0, phy_node['res']['mem'])