summaryrefslogtreecommitdiffstats
path: root/src/workflow/workflow_factory.py
blob: 03c81262a3b13d54ab76397d99d8399d4cf59278 (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
##############################################################################
# Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, 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 workflow.booking_workflow import Booking_Resource_Select, SWConfig_Select, Booking_Meta, OPNFV_Select
from workflow.resource_bundle_workflow import Define_Hardware, Define_Nets, Resource_Meta_Info
from workflow.sw_bundle_workflow import Config_Software, Define_Software, SWConf_Resource_Select
from workflow.snapshot_workflow import Select_Host_Step, Image_Meta_Step
from workflow.opnfv_workflow import Pick_Installer, Assign_Network_Roles, Assign_Host_Roles, OPNFV_Resource_Select, MetaInfo
from workflow.models import Confirmation_Step

import uuid

import logging
logger = logging.getLogger(__name__)


class MetaStep(object):

    UNTOUCHED = 0
    INVALID = 100
    VALID = 200

    def set_invalid(self, message, code=100):
        self.valid = code
        self.message = message

    def set_valid(self, message, code=200):
        self.valid = code
        self.message = message

    def __init__(self, *args, **kwargs):
        self.short_title = "error"
        self.skip_step = 0
        self.valid = 0
        self.hidden = False
        self.message = ""
        self.id = uuid.uuid4()

    def to_json(self):
        return {
            'title': self.short_title,
            'skip': self.skip_step,
            'valid': self.valid,
            'message': self.message,
        }

    def __str__(self):
        return "metastep: " + str(self.short_title)

    def __hash__(self):
        return hash(self.id)

    def __eq__(self, other):
        return self.id.int == other.id.int

    def __ne__(self, other):
        return self.id.int != other.id.int


class Workflow(object):
    def __init__(self, steps, repository):
        self.repository = repository
        self.steps = steps
        self.active_index = 0


class WorkflowFactory():
    booking_steps = [
        Booking_Resource_Select,
        SWConfig_Select,
        Booking_Meta,
        OPNFV_Select,
    ]

    resource_steps = [
        Define_Hardware,
        Define_Nets,
        Resource_Meta_Info,
    ]

    config_steps = [
        SWConf_Resource_Select,
        Define_Software,
        Config_Software,
    ]

    snapshot_steps = [
        Select_Host_Step,
        Image_Meta_Step,
    ]

    opnfv_steps = [
        OPNFV_Resource_Select,
        Pick_Installer,
        Assign_Network_Roles,
        Assign_Host_Roles,
        MetaInfo
    ]

    def conjure(self, workflow_type=None, repo=None):
        workflow_types = [
            self.booking_steps,
            self.resource_steps,
            self.config_steps,
            self.snapshot_steps,
            self.opnfv_steps,
        ]

        steps = self.make_steps(workflow_types[workflow_type], repository=repo)
        return steps

    def create_workflow(self, workflow_type=None, repo=None):
        steps = self.conjure(workflow_type, repo)
        c_step = self.make_step(Confirmation_Step, repo)
        steps.append(c_step)
        return Workflow(steps, repo)

    def make_steps(self, step_types, repository):
        steps = []
        for step_type in step_types:
            steps.append(self.make_step(step_type, repository))

        return steps

    def make_step(self, step_type, repository):
        iden = step_type.description + step_type.title + step_type.template
        return step_type(iden, repository)