e68851038f7897add65b54af3b80711a55481ed6
[laas.git] / src / workflow / workflow_factory.py
1 ##############################################################################
2 # Copyright (c) 2018 Sawyer Bergeron, Parker Berberian, Sean Smith, 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
11 from workflow.booking_workflow import Booking_Resource_Select, Booking_Meta, OPNFV_Select
12 from workflow.resource_bundle_workflow import Define_Hardware, Define_Nets, Resource_Meta_Info, Define_Software
13 from workflow.snapshot_workflow import Select_Host_Step, Image_Meta_Step
14 from workflow.opnfv_workflow import Pick_Installer, Assign_Network_Roles, Assign_Host_Roles, OPNFV_Resource_Select, MetaInfo
15 from workflow.models import Confirmation_Step
16
17 import uuid
18
19 import logging
20 logger = logging.getLogger(__name__)
21
22
23 class MetaStep(object):
24
25     UNTOUCHED = 0
26     INVALID = 100
27     VALID = 200
28
29     def set_invalid(self, message, code=100):
30         self.valid = code
31         self.message = message
32
33     def set_valid(self, message, code=200):
34         self.valid = code
35         self.message = message
36
37     def __init__(self, *args, **kwargs):
38         self.short_title = "error"
39         self.skip_step = 0
40         self.valid = 0
41         self.hidden = False
42         self.message = ""
43         self.id = uuid.uuid4()
44
45     def to_json(self):
46         return {
47             'title': self.short_title,
48             'skip': self.skip_step,
49             'valid': self.valid,
50             'message': self.message,
51         }
52
53     def __str__(self):
54         return "metastep: " + str(self.short_title)
55
56     def __hash__(self):
57         return hash(self.id)
58
59     def __eq__(self, other):
60         return self.id.int == other.id.int
61
62     def __ne__(self, other):
63         return self.id.int != other.id.int
64
65
66 class Workflow(object):
67     def __init__(self, steps, repository):
68         self.repository = repository
69         self.steps = steps
70         self.active_index = 0
71
72
73 class WorkflowFactory():
74     booking_steps = [
75         Booking_Resource_Select,
76         Booking_Meta,
77         OPNFV_Select,
78     ]
79
80     resource_steps = [
81         Define_Hardware,
82         Define_Software,
83         Define_Nets,
84         Resource_Meta_Info,
85     ]
86
87     snapshot_steps = [
88         Select_Host_Step,
89         Image_Meta_Step,
90     ]
91
92     opnfv_steps = [
93         OPNFV_Resource_Select,
94         Pick_Installer,
95         Assign_Network_Roles,
96         Assign_Host_Roles,
97         MetaInfo
98     ]
99
100     def conjure(self, workflow_type=None, repo=None):
101         workflow_types = [
102             self.booking_steps,
103             self.resource_steps,
104             self.snapshot_steps,
105             self.opnfv_steps,
106         ]
107
108         steps = self.make_steps(workflow_types[workflow_type], repository=repo)
109         return steps
110
111     def create_workflow(self, workflow_type=None, repo=None):
112         steps = self.conjure(workflow_type, repo)
113         c_step = self.make_step(Confirmation_Step, repo)
114         steps.append(c_step)
115         return Workflow(steps, repo)
116
117     def make_steps(self, step_types, repository):
118         steps = []
119         for step_type in step_types:
120             steps.append(self.make_step(step_type, repository))
121
122         return steps
123
124     def make_step(self, step_type, repository):
125         iden = step_type.description + step_type.title + step_type.template
126         return step_type(iden, repository)