Adds L3 agent test case to test_l3_agent_scheduler.py

This submission is to verify "add L3 Agent" and "remove L3 Agent"
functionality and also added required methods in both XML/JSON
interface

Change-Id: I7829b7aefdea3c138ffb8070ba1497343f4a11fe
diff --git a/tempest/api/network/admin/test_l3_agent_scheduler.py b/tempest/api/network/admin/test_l3_agent_scheduler.py
index 7c02787..eb397ba 100644
--- a/tempest/api/network/admin/test_l3_agent_scheduler.py
+++ b/tempest/api/network/admin/test_l3_agent_scheduler.py
@@ -26,6 +26,7 @@
 
         List routers that the given L3 agent is hosting.
         List L3 agents hosting the given router.
+        Add and Remove Router to L3 agent
 
     v2.0 of the Neutron API is assumed. It is also assumed that the following
     options are defined in the [network] section of etc/tempest.conf:
@@ -37,28 +38,49 @@
         if not test.is_extension_enabled('l3_agent_scheduler', 'network'):
             msg = "L3 Agent Scheduler Extension not enabled."
             raise cls.skipException(msg)
+        # Trying to get agent details for L3 Agent
+        resp, body = cls.admin_client.list_agents()
+        agents = body['agents']
+        for agent in agents:
+            if agent['agent_type'] == 'L3 agent':
+                cls.agent = agent
+                break
+        else:
+            msg = "L3 Agent not found"
+            raise cls.skipException(msg)
 
     @test.attr(type='smoke')
     def test_list_routers_on_l3_agent(self):
-        resp, body = self.admin_client.list_agents()
-        agents = body['agents']
-        for a in agents:
-            if a['agent_type'] == 'L3 agent':
-                agent = a
         resp, body = self.admin_client.list_routers_on_l3_agent(
-            agent['id'])
+            self.agent['id'])
         self.assertEqual('200', resp['status'])
 
     @test.attr(type='smoke')
-    def test_list_l3_agents_hosting_router(self):
-        name = data_utils.rand_name('router-')
+    def test_add_list_remove_router_on_l3_agent(self):
+        l3_agent_ids = list()
+        name = data_utils.rand_name('router1-')
         resp, router = self.client.create_router(name)
+        self.addCleanup(self.client.delete_router, router['router']['id'])
+        resp, body = self.admin_client.add_router_to_l3_agent(
+            self.agent['id'], router['router']['id'])
         self.assertEqual('201', resp['status'])
         resp, body = self.admin_client.list_l3_agents_hosting_router(
             router['router']['id'])
         self.assertEqual('200', resp['status'])
-        resp, _ = self.client.delete_router(router['router']['id'])
-        self.assertEqual(204, resp.status)
+        for agent in body['agents']:
+            l3_agent_ids.append(agent['id'])
+            self.assertIn('agent_type', agent)
+            self.assertEqual('L3 agent', agent['agent_type'])
+        self.assertIn(self.agent['id'], l3_agent_ids)
+        del l3_agent_ids[:]
+        resp, body = self.admin_client.remove_router_from_l3_agent(
+            self.agent['id'], router['router']['id'])
+        self.assertEqual('204', resp['status'])
+        resp, body = self.admin_client.list_l3_agents_hosting_router(
+            router['router']['id'])
+        for agent in body['agents']:
+            l3_agent_ids.append(agent['id'])
+        self.assertNotIn(self.agent['id'], l3_agent_ids)
 
 
 class L3AgentSchedulerTestXML(L3AgentSchedulerTestJSON):
diff --git a/tempest/services/network/json/network_client.py b/tempest/services/network/json/network_client.py
index 81dbfbc..5089e69 100644
--- a/tempest/services/network/json/network_client.py
+++ b/tempest/services/network/json/network_client.py
@@ -310,6 +310,20 @@
         body = json.loads(body)
         return resp, body
 
+    def add_router_to_l3_agent(self, agent_id, router_id):
+        uri = '%s/agents/%s/l3-routers' % (self.uri_prefix, agent_id)
+        post_body = {"router_id": router_id}
+        body = json.dumps(post_body)
+        resp, body = self.post(uri, body)
+        body = json.loads(body)
+        return resp, body
+
+    def remove_router_from_l3_agent(self, agent_id, router_id):
+        uri = '%s/agents/%s/l3-routers/%s' % (
+            self.uri_prefix, agent_id, router_id)
+        resp, body = self.delete(uri)
+        return resp, body
+
     def list_dhcp_agent_hosting_network(self, network_id):
         uri = '%s/networks/%s/dhcp-agents' % (self.uri_prefix, network_id)
         resp, body = self.get(uri)
diff --git a/tempest/services/network/xml/network_client.py b/tempest/services/network/xml/network_client.py
index 25e6edb..8f6b355 100644
--- a/tempest/services/network/xml/network_client.py
+++ b/tempest/services/network/xml/network_client.py
@@ -268,15 +268,30 @@
     def list_routers_on_l3_agent(self, agent_id):
         uri = '%s/agents/%s/l3-routers' % (self.uri_prefix, agent_id)
         resp, body = self.get(uri)
-        body = _root_tag_fetcher_and_xml_to_json_parse(body)
+        routers = common.parse_array(etree.fromstring(body))
+        body = {'routers': routers}
         return resp, body
 
     def list_l3_agents_hosting_router(self, router_id):
         uri = '%s/routers/%s/l3-agents' % (self.uri_prefix, router_id)
         resp, body = self.get(uri)
+        agents = common.parse_array(etree.fromstring(body))
+        body = {'agents': agents}
+        return resp, body
+
+    def add_router_to_l3_agent(self, agent_id, router_id):
+        uri = '%s/agents/%s/l3-routers' % (self.uri_prefix, agent_id)
+        router = (common.Element("router_id", router_id))
+        resp, body = self.post(uri, str(common.Document(router)))
         body = _root_tag_fetcher_and_xml_to_json_parse(body)
         return resp, body
 
+    def remove_router_from_l3_agent(self, agent_id, router_id):
+        uri = '%s/agents/%s/l3-routers/%s' % (
+            self.uri_prefix, agent_id, router_id)
+        resp, body = self.delete(uri)
+        return resp, body
+
     def list_dhcp_agent_hosting_network(self, network_id):
         uri = '%s/networks/%s/dhcp-agents' % (self.uri_prefix, network_id)
         resp, body = self.get(uri)