summaryrefslogtreecommitdiffstats
path: root/sfc/lib/topology_shuffler.py
blob: ccabe427ebfcc5c36c0a9cc867fcd2989e815c2a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import datetime
import random
import functest.utils.openstack_utils as os_utils
import functest.utils.functest_logger as ft_logger


logger = ft_logger.Logger(__name__).getLogger()

# The possible topologies we are testing
TOPOLOGIES = [
    {
        'id': 'CLIENT_VNF_SAME_HOST',
        'description': '''
        Client instance and vnfs are are on the same
        compute host. Server instance is on a different host
        '''
    },
    {
        'id': 'CLIENT_SERVER_SAME_HOST',
        'description': '''
        Client instance and server instance are on the same
        compute host. All VNFs are on a different host.
        '''
    },
    {
        'id': 'SERVER_VNF_SAME_HOST',
        'description': '''
        Server instance and vnfs are are on the same
        compute host. Server instance is on a different host
        '''
    },
    {
        'id': 'CLIENT_SERVER_SAME_HOST_SPLIT_VNF',
        'description': '''
        Client and server are on the same host.
        The VNFs are split between hosts Round Robin.
        '''
    },
    {
        'id': 'CLIENT_SERVER_DIFFERENT_HOST_SPLIT_VNF',
        'description': '''
        Client and server are on different hosts.
        The VNFs are split between hosts Round Robin.
        '''
    }
]

DEFAULT_TOPO = {
    'id': 'DEFAULT',
    'description': '''
    The default topology created by nova scheduler
    '''
}


def _get_seed():
    '''
    Get a seed based on the day of the week to choose which topology to test

    NOTE: There's sure a smarter way to do this
          Probably with the Jenkins job id
    '''
    cutoff = len(TOPOLOGIES - 1)
    seed = datetime.datetime.today().weekday()
    if seed > cutoff:
        seed = random.randrange(cutoff)
    return seed


def _split_host_aggregates():
    '''
    Gets all the compute hosts and creates an aggregate for each.
    Aggregates are named based on the convention compute1, compute2, ...
    '''
    nova_client = os_utils.get_nova_client()
    hosts = os_utils.get_hypervisors(nova_client)
    aggregates = []
    for idx, host in enumerate(hosts):
        az_name = 'compute{0}'.format(idx)
        # aggregate name is the same as availability zone name
        os_utils.create_aggregate_with_host(
            nova_client, az_name, az_name, host)
        aggregates.append(az_name)
    # aggregates and av zones abstractions are tightly coupled in nova
    return aggregates


def clean_host_aggregates(aggregates):
    '''
    Clean all the created host aggregates
    '''
    nova_client = os_utils.get_nova_client()
    for aggregate in aggregates:
        if not os_utils.delete_aggregate(nova_client, aggregate):
            logger.error('Could not delete aggregate {0}'
                         .format(aggregate))


def topology(vnf_names, host_aggregates=None, seed=None):
    '''
    Get the topology for client, server and vnfs.
    The topology is returned as a dict in the form
    {
    'client': <availability_zone>,
    'server': <availability_zone>,
    'vnf1':   <availability_zone>,
    'vnf2':   <availability_zone>
    ...
    }
    Use seed=-1 to get the default topology created by nova-scheduler
    '''

    if host_aggregates is None:
        host_aggregates = _split_host_aggregates()
    if len(host_aggregates) < 2 or seed is None:
        return None

    topology = TOPOLOGIES[seed]
    topology_assigment = {}
    if topology['id'] == 'CLIENT_VNF_SAME_HOST':
        topology_assigment['client'] = host_aggregates[0]
        topology_assigment['server'] = host_aggregates[1]
        for vnf in vnf_names:
            topology_assigment[vnf] = host_aggregates[0]
    elif topology['id'] == 'CLIENT_SERVER_SAME_HOST':
        topology_assigment['client'] = host_aggregates[0]
        topology_assigment['server'] = host_aggregates[0]
        for vnf in vnf_names:
            topology_assigment[vnf] = host_aggregates[1]
    elif topology['id'] == 'SERVER_VNF_SAME_HOST':
        topology_assigment['client'] = host_aggregates[1]
        topology_assigment['server'] = host_aggregates[0]
        for vnf in vnf_names:
            topology_assigment[vnf] = host_aggregates[0]
    elif topology['id'] == 'CLIENT_SERVER_SAME_HOST_SPLIT_VNF':
        topology_assigment['client'] = host_aggregates[0]
        topology_assigment['server'] = host_aggregates[0]
        idx = 0
        for vnf in vnf_names:
            topology_assigment[vnf] = host_aggregates[idx % 2]
            idx += 1
    elif topology['id'] == 'CLIENT_SERVER_DIFFERENT_HOST_SPLIT_VNF':
        topology_assigment['client'] = host_aggregates[0]
        topology_assigment['server'] = host_aggregates[1]
        idx = 0
        for vnf in vnf_names:
            topology_assigment[vnf] = host_aggregates[idx % 2]
            idx += 1
    logger.info("Creating enpoint and VNF topology on the compute hosts")
    logger.info(topology['description'])
    return topology_assigment