# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import copy
import testtools

import openstack
import openstack.cloud
from openstack.tests.unit import base


class TestNetwork(base.TestCase):

    mock_new_network_rep = {
        'provider:physical_network': None,
        'ipv6_address_scope': None,
        'revision_number': 3,
        'port_security_enabled': True,
        'provider:network_type': 'local',
        'id': '881d1bb7-a663-44c0-8f9f-ee2765b74486',
        'router:external': False,
        'availability_zone_hints': [],
        'availability_zones': [],
        'provider:segmentation_id': None,
        'ipv4_address_scope': None,
        'shared': False,
        'project_id': '861808a93da0484ea1767967c4df8a23',
        'status': 'ACTIVE',
        'subnets': [],
        'description': '',
        'tags': [],
        'updated_at': '2017-04-22T19:22:53Z',
        'is_default': False,
        'qos_policy_id': None,
        'name': 'netname',
        'admin_state_up': True,
        'tenant_id': '861808a93da0484ea1767967c4df8a23',
        'created_at': '2017-04-22T19:22:53Z',
        'mtu': 0,
        'dns_domain': 'sample.openstack.org.'
    }

    network_availability_zone_extension = {
        "alias": "network_availability_zone",
        "updated": "2015-01-01T10:00:00-00:00",
        "description": "Availability zone support for router.",
        "links": [],
        "name": "Network Availability Zone"
    }

    enabled_neutron_extensions = [network_availability_zone_extension]

    def test_list_networks(self):
        net1 = {'id': '1', 'name': 'net1'}
        net2 = {'id': '2', 'name': 'net2'}
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'network', 'public', append=['v2.0', 'networks.json']),
                 json={'networks': [net1, net2]})
        ])
        nets = self.cloud.list_networks()
        self.assertEqual([net1, net2], nets)
        self.assert_calls()

    def test_list_networks_filtered(self):
        self.register_uris([
            dict(method='GET',
                 uri=self.get_mock_url(
                     'network', 'public', append=['v2.0', 'networks.json'],
                     qs_elements=["name=test"]),
                 json={'networks': []})
        ])
        self.cloud.list_networks(filters={'name': 'test'})
        self.assert_calls()

    def test_create_network(self):
        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url(
                     'network', 'public', append=['v2.0', 'networks.json']),
                 json={'network': self.mock_new_network_rep},
                 validate=dict(
                     json={'network': {
                         'admin_state_up': True,
                         'name': 'netname'}}))
        ])
        network = self.cloud.create_network("netname")
        self.assertEqual(self.mock_new_network_rep, network)
        self.assert_calls()

    def test_create_network_specific_tenant(self):
        project_id = "project_id_value"
        mock_new_network_rep = copy.copy(self.mock_new_network_rep)
        mock_new_network_rep['project_id'] = project_id
        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url(
                     'network', 'public', append=['v2.0', 'networks.json']),
                 json={'network': mock_new_network_rep},
                 validate=dict(
                     json={'network': {
                         'admin_state_up': True,
                         'name': 'netname',
                         'tenant_id': project_id}}))
        ])
        network = self.cloud.create_network("netname", project_id=project_id)
        self.assertEqual(mock_new_network_rep, network)
        self.assert_calls()

    def test_create_network_external(self):
        mock_new_network_rep = copy.copy(self.mock_new_network_rep)
        mock_new_network_rep['router:external'] = True
        self.register_uris([
            dict(method='POST',
                 uri=self.get_mock_url(
                     'network', 'public', append=['v2.0', 'networks.json']),
                 json={'network': mock_new_network_rep},
                 validate=dict(
                     json={'network': {
                         'admin_state_up': True,
                         'name': 'netname',
                         'router:external': True}}))
        ])