aboutsummaryrefslogtreecommitdiffstats
path: root/sfc/lib/topology_shuffler.py
blob: 24c8f875a917470c96ee3ec27434c51e8fa8f85b (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
import datetime
import random
import logging
import sfc.lib.utils as sfc_utils

logger = logging.getLogger(__name__)

# The possible topologies we are testing
TOPOLOGIES = [
    {
        'id': 'CLIENT_SERVER_VNF_SAME_HOST',
        'description': '''
        All endpoints and VNFs are on a single host.
        This is the baseline test.
        '''
    },
    {
        'id': 'CLIENT_VNF_SAME_HOST',
        'description': '''
        Client instance and vnfs are on the same
        compute host. Server instance is on a different host
        '''
    },
    {
        'id': 'SERVER_VNF_SAME_HOST',
        'description': '''
        Server instance and vnfs are on the same
        compute host. Client instance is on a different host
        '''
    },
    {
        'id': 'CLIENT_SERVER_DIFFERENT_HOST_SPLIT_VNF',
        'description': '''
        Client and server are on different hosts.
        The VNFs are split between hosts Round Robin.
        '''
    },
    {
        '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_SAME_HOST',
        'description': '''
        Client instance and server instance are on the same
        compute host. All VNFs are on a different host.
        '''
    }
]

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

WORKING_TOPOLOGIES = ['CLIENT_SERVER_VNF_SAME_HOST',
                      'CLIENT_VNF_SAME_HOST',
                      'SERVER_VNF_SAME_HOST']


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
    '''
    # We only add the topologies which are working
    cutoff = len(WORKING_TOPOLOGIES) - 1
    seed = datetime.datetime.today().weekday()
    if seed > cutoff:
        seed = random.randrange(cutoff)
    return seed


def topology(vnf_names, av_zones=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=None to get the default topology created by nova-scheduler
    '''

    if av_zones is None:
        av_zones = sfc_utils.get_av_zones()

    if len(av_zones) < 2 or seed is None:
        # fall back to nova availability zone
        topology_assignment = {
            'id': DEFAULT_TOPO['id'],
            'description': DEFAULT_TOPO['description'],
            'client': 'nova',
            'server': 'nova'
        }
        for vnf in vnf_names:
            topology_assignment[vnf] = 'nova'
        return topology_assignment

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