aboutsummaryrefslogtreecommitdiffstats
path: root/src/workflow/opnfv_workflow.py
blob: 7d499eca97c27204c15b89a8aea0a51bbf3de15e (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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
##############################################################################
# Copyright (c) 2018 Parker Berberian, Sawyer Bergeron, and others.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################


from django.forms import formset_factory

from workflow.models import WorkflowStep, AbstractSelectOrCreate
from resource_inventory.models import ConfigBundle, OPNFV_SETTINGS
from workflow.forms import OPNFVSelectionForm, OPNFVNetworkRoleForm, OPNFVHostRoleForm, SWConfigSelectorForm, BasicMetaForm


class OPNFV_Resource_Select(AbstractSelectOrCreate):
    title = "Select Software Configuration"
    description = "Choose the software bundle you wish to use as a base for your OPNFV configuration"
    short_title = "software config"
    form = SWConfigSelectorForm

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.select_repo_key = self.repo.SELECTED_CONFIG_BUNDLE

    def get_form_queryset(self):
        user = self.repo_get(self.repo.SESSION_USER)
        qs = ConfigBundle.objects.filter(owner=user)
        return qs

    def put_confirm_info(self, bundle):
        confirm_dict = self.repo_get(self.repo.CONFIRMATION)
        confirm_dict['software bundle'] = bundle.name
        confirm_dict['hardware POD'] = bundle.bundle.name
        self.repo_put(self.repo.CONFIRMATION, confirm_dict)

    def get_page_context(self):
        return {
            'select_type': 'swconfig',
            'select_type_title': 'Software Config',
            'addable_type_num': 2
        }


class Pick_Installer(WorkflowStep):
    template = 'config_bundle/steps/pick_installer.html'
    title = 'Pick OPNFV Installer'
    description = 'Choose which OPNFV installer to use'
    short_title = "opnfv installer"
    modified_key = "installer_step"

    def update_confirmation(self):
        confirm = self.repo_get(self.repo.CONFIRMATION, {})
        models = self.repo_get(self.repo.OPNFV_MODELS, {})
        installer = models.get("installer_chosen")
        scenario = models.get("scenario_chosen")
        if not (installer and scenario):
            return
        confirm['installer'] = installer.name
        confirm['scenario'] = scenario.name
        self.repo_put(self.repo.CONFIRMATION, confirm)

    def get_context(self):
        context = super(Pick_Installer, self).get_context()

        models = self.repo_get(self.repo.OPNFV_MODELS, {})
        initial = {
            "installer": models.get("installer_chosen"),
            "scenario": models.get("scenario_chosen")
        }

        context["form"] = OPNFVSelectionForm(initial=initial)
        return context

    def post_render(self, request):
        form = OPNFVSelectionForm(request.POST)
        if form.is_valid():
            installer = form.cleaned_data['installer']
            scenario = form.cleaned_data['scenario']
            models = self.repo_get(self.repo.OPNFV_MODELS, {})
            models['installer_chosen'] = installer
            models['scenario_chosen'] = scenario
            self.repo_put(self.repo.OPNFV_MODELS, models)
            self.update_confirmation()
            self.set_valid("Step Completed")
        else:
            self.set_invalid("Please select an Installer and Scenario")

        return self.render(request)


class Assign_Network_Roles(WorkflowStep):
    template = 'config_bundle/steps/assign_network_roles.html'
    title = 'Pick Network Roles'
    description = 'Choose what role each network should get'
    short_title = "network roles"
    modified_key = "net_roles_step"

    """
    to do initial filling, repo should have a "network_roles" array with the following structure for each element:
    {
        "role": <NetworkRole object ref>,
        "network": <Network object ref>
    }
    """
    def create_netformset(self, roles, config_bundle, data=None):
        roles_initial = []
        set_roles = self.repo_get(self.repo.OPNFV_MODELS, {}).get("network_roles")
        if set_roles:
            roles_initial = set_roles
        else:
            for role in OPNFV_SETTINGS.NETWORK_ROLES:
                roles_initial.append({"role": role})

        Formset = formset_factory(OPNFVNetworkRoleForm, extra=0)
        kwargs = {
            "initial": roles_initial,
            "form_kwargs": {"config_bundle": config_bundle}
        }
        formset = None
        if data:
            formset = Formset(data, **kwargs)
        else:
            formset = Formset(**kwargs)
        return formset

    def get_context(self):
        context = super(Assign_Network_Roles, self).get_context()
        config_bundle = self.repo_get(self.repo.SELECTED_CONFIG_BUNDLE)
        if config_bundle is None:
            context["unavailable"] = True
            return context

        roles = OPNFV_SETTINGS.NETWORK_ROLES
        formset = self.create_netformset(roles, config_bundle)
        context['formset'] = formset

        return context

    def update_confirmation(self):
        confirm = self.repo_get(self.repo.CONFIRMATION, {})
        models = self.repo_get(self.repo.OPNFV_MODELS, {})
        roles = models.get("network_roles")
        if not roles:
            return
        confirm['network roles'] = {}
        for role in roles:
            confirm['network roles'][role['role']] = role['network'].name
        self.repo_put(self.repo.CONFIRMATION, confirm)

    def post_render(self, request):
        models = self.repo_get(self.repo.OPNFV_MODELS, {})
        config_bundle = self.repo_get(self.repo.SELECTED_CONFIG_BUNDLE)
        roles = OPNFV_SETTINGS.NETWORK_ROLES
        net_role_formset = self.create_netformset(roles, config_bundle, data=request.POST)
        if net_role_formset.is_valid():
            results = []
            for form in net_role_formset:
                results.append({
                    "role": form.cleaned_data['role'],
                    "network": form.cleaned_data['network']
                })
            models['network_roles'] = results
            self.set_valid("Completed")
            self.repo_put(self.repo.OPNFV_MODELS, models)
            self.update_confirmation()
        else:
            self.set_invalid("Please complete all fields")
        return self.render(request)


class Assign_Host_Roles(WorkflowStep):  # taken verbatim from Define_Software in sw workflow, merge the two?
    template = 'config_bundle/steps/assign_host_roles.html'
    title = 'Pick Host Roles'
    description = "Choose the role each machine will have in your OPNFV pod"
    short_title = "host roles"
    modified_key = "host_roles_step"

    def create_host_role_formset(self, hostlist=[], data=None):
        models = self.repo_get(self.repo.OPNFV_MODELS, {})
        host_roles = models.get("host_roles", [])
        if not host_roles:
            for host in hostlist:
                initial = {"host_name": host.resource.name}
                host_roles.append(initial)
        models['host_roles'] = host_roles
        self.repo_put(self.repo.OPNFV_MODELS, models)

        HostFormset = formset_factory(OPNFVHostRoleForm, extra=0)

        kwargs = {"initial": host_roles}
        formset = None
        if data:
            formset = HostFormset(data, **kwargs)
        else:
            formset = HostFormset(**kwargs)

        return formset

    def get_context(self):
        context = super(Assign_Host_Roles, self).get_context()
        config = self.repo_get(self.repo.SELECTED_CONFIG_BUNDLE)
        if config is None:
            context['error'] = "Please select a Configuration on the first step"

        formset = self.create_host_role_formset(hostlist=config.bundle.getHosts())
        context['formset'] = formset

        return context

    def get_host_role_mapping(self, host_roles, hostname):
        for obj in host_roles:
            if hostname == obj['host_name']:
                return obj
        return None

    def update_confirmation(self):
        confirm = self.repo_get(self.repo.CONFIRMATION, {})
        models = self.repo_get(self.repo.OPNFV_MODELS, {})
        roles = models.get("host_roles")
        if not roles:
            return
        confirm['host roles'] = {}
        for role in roles:
            confirm['host roles'][role['host_name']] = role['role'].name
        self.repo_put(self.repo.CONFIRMATION, confirm)

    def post_render(self, request):
        formset = self.create_host_role_formset(data=request.POST)

        models = self.repo_get(self.repo.OPNFV_MODELS, {})
        host_roles = models.get("host_roles", [])

        has_jumphost = False
        if formset.is_valid():
            for form in formset:
                hostname = form.cleaned_data['host_name']
                role = form.cleaned_data['role']
                mapping = self.get_host_role_mapping(host_roles, hostname)
                mapping['role'] = role
                if "jumphost" in role.name.lower():
                    has_jumphost = True

            models['host_roles'] = host_roles
            self.repo_put(self.repo.OPNFV_MODELS, models)
            self.update_confirmation()

            if not has_jumphost:
                self.set_invalid('Must have at least one "Jumphost" per POD')
            else:
                self.set_valid("Completed")
        else:
            self.set_invalid("Please complete all fields")

        return self.render(request)


class MetaInfo(WorkflowStep):
    template = 'config_bundle/steps/config_software.html'
    title = "Other Info"
    description = "Give your software config a name, description, and other stuff"
    short_title = "config info"

    def get_context(self):
        context = super(MetaInfo, self).get_context()

        initial = self.repo_get(self.repo.OPNFV_MODELS, {}).get("meta", {})
        context["form"] = BasicMetaForm(initial=initial)
        return context

    def update_confirmation(self):
        confirm = self.repo_get(self.repo.CONFIRMATION, {})
        models = self.repo_get(self.repo.OPNFV_MODELS, {})
        meta = models.get("meta")
        if not meta:
            return
        confirm['name'] = meta['name']
        confirm['description'] = meta['description']
        self.repo_put(self.repo.CONFIRMATION, confirm)

    def post_render(self, request):
        models = self.repo_get(self.repo.OPNFV_MODELS, {})
        info = models.get("meta", {})

        form = BasicMetaForm(request.POST)
        if form.is_valid():
            info['name'] = form.cleaned_data['name']
            info['description'] = form.cleaned_data['description']
            models['meta'] = info
            self.repo_put(self.repo.OPNFV_MODELS, models)
            self.update_confirmation()
            self.set_valid("Complete")
        else:
            self.set_invalid("Please correct the errors shown below")

        self.repo_put(self.repo.OPNFV_MODELS, models)
        return self.render(request)