Merge "Remove TRex installer from ansible directory"
[yardstick.git] / yardstick / benchmark / scenarios / lib / create_subnet.py
1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import logging
11
12 from yardstick.benchmark.scenarios import base
13 from yardstick.common import openstack_utils
14 from yardstick.common import exceptions
15
16
17 LOG = logging.getLogger(__name__)
18
19
20 class CreateSubnet(base.Scenario):
21     """Create an OpenStack flavor"""
22
23     __scenario_type__ = "CreateSubnet"
24
25     def __init__(self, scenario_cfg, context_cfg):
26         self.scenario_cfg = scenario_cfg
27         self.context_cfg = context_cfg
28         self.options = self.scenario_cfg['options']
29
30         self.network_name_or_id = self.options['network_name_or_id']
31         self.cidr = self.options.get('cidr')
32         self.ip_version = self.options.get('ip_version', 4)
33         self.enable_dhcp = self.options.get('enable_dhcp', False)
34         self.subnet_name = self.options.get('subnet_name')
35         self.tenant_id = self.options.get('tenant_id')
36         self.allocation_pools = self.options.get('allocation_pools')
37         self.gateway_ip = self.options.get('gateway_ip')
38         self.disable_gateway_ip = self.options.get('disable_gateway_ip', False)
39         self.dns_nameservers = self.options.get('dns_nameservers')
40         self.host_routes = self.options.get('host_routes')
41         self.ipv6_ra_mode = self.options.get('ipv6_ra_mode')
42         self.ipv6_address_mode = self.options.get('ipv6_address_mode')
43         self.use_default_subnetpool = self.options.get(
44             'use_default_subnetpool', False)
45
46         self.shade_client = openstack_utils.get_shade_client()
47
48         self.setup_done = False
49
50     def setup(self):
51         """scenario setup"""
52
53         self.setup_done = True
54
55     def run(self, result):
56         """execute the test"""
57
58         if not self.setup_done:
59             self.setup()
60
61         subnet_id = openstack_utils.create_neutron_subnet(
62             self.shade_client, self.network_name_or_id, cidr=self.cidr,
63             ip_version=self.ip_version, enable_dhcp=self.enable_dhcp,
64             subnet_name=self.subnet_name, tenant_id=self.tenant_id,
65             allocation_pools=self.allocation_pools, gateway_ip=self.gateway_ip,
66             disable_gateway_ip=self.disable_gateway_ip,
67             dns_nameservers=self.dns_nameservers, host_routes=self.host_routes,
68             ipv6_ra_mode=self.ipv6_ra_mode,
69             ipv6_address_mode=self.ipv6_address_mode,
70             use_default_subnetpool=self.use_default_subnetpool)
71         if not subnet_id:
72             result.update({"subnet_create": 0})
73             LOG.error("Create subnet failed!")
74             raise exceptions.ScenarioCreateSubnetError
75
76         result.update({"subnet_create": 1})
77         LOG.info("Create subnet successful!")
78         keys = self.scenario_cfg.get('output', '').split()
79         values = [subnet_id]
80         return self._push_to_outputs(keys, values)