| 1 |
# Copyright 2016 - Nokia Networks. |
| 2 |
# Copyright 2017 - Red Hat, Inc. |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
# you may not use this file except in compliance with the License. |
| 6 |
# You may obtain a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
# See the License for the specific language governing permissions and |
| 14 |
# limitations under the License. |
| 15 |
|
| 16 |
import abc |
| 17 |
|
| 18 |
from oslo_utils import importutils |
| 19 |
|
| 20 |
from mistral_lib import serialization |
| 21 |
from mistral_lib.utils import inspect_utils as i_utils |
| 22 |
|
| 23 |
|
| 24 |
class Action(serialization.MistralSerializable): |
| 25 |
"""Action. |
| 26 |
|
| 27 |
Action is a means in Mistral to perform some useful work associated with |
| 28 |
a workflow during its execution. Every workflow task is configured with |
| 29 |
an action and when the task runs it eventually delegates to the action. |
| 30 |
When it happens task parameters get evaluated (calculating expressions, |
| 31 |
if any) and are treated as action parameters. So in a regular general |
| 32 |
purpose languages terminology action is a method declaration and task is |
| 33 |
a method call. |
| 34 |
|
| 35 |
Base action class initializer doesn't have arguments. However, concrete |
| 36 |
action classes may have any number of parameters defining action behavior. |
| 37 |
These parameters must correspond to parameters declared in action |
| 38 |
specification (e.g. using DSL or others). |
| 39 |
""" |
| 40 |
|
| 41 |
def __init__(self): |
| 42 |
# NOTE(d0ugal): We need to define an empty __init__ otherwise |
| 43 |
# inspect.getargspec will fail in Python 2 for actions that subclass |
| 44 |
# but don't define their own __init__. |
| 45 |
pass |
| 46 |
|
| 47 |
@abc.abstractmethod |
| 48 |
def run(self, context): |
| 49 |
"""Run action logic. |
| 50 |
|
| 51 |
:param context: contains contextual information of the action. |
| 52 |
The context includes an execution context (like execution identifier |
| 53 |
and workflow name) and a security context with the authorization |
| 54 |
details. |
| 55 |
:return: Result of the action. Note that for asynchronous actions |
| 56 |
it should always be None, however, if even it's not None it will be |
| 57 |
ignored by a caller. |
| 58 |
|
| 59 |
Result can be of two types: |
| 60 |
1) Any serializable value meaningful from a user perspective (such |
| 61 |
as string, number or dict). |
| 62 |
2) Instance of {mistral_lib.types.Result} which has field "data" |
| 63 |
for success result and field "error" for keeping so called "error |
| 64 |
result" like HTTP error code and similar. Using the second type |
| 65 |
allows to communicate a result even in case of error and hence to have |
| 66 |
conditions in "on-error" clause of direct workflows. Depending on |
| 67 |
particular action semantics one or another option may be preferable. |
| 68 |
In case if action failed and there's no need to communicate any error |
| 69 |
result this method should throw a ActionException. |
| 70 |
""" |
| 71 |
pass |
| 72 |
|
| 73 |
def is_sync(self): |
| 74 |
"""Returns True if the action is synchronous, otherwise False. |
| 75 |
|
| 76 |
:return: True if the action is synchronous and method run() returns |
| 77 |
final action result. Otherwise returns False which means that |
| 78 |
a result of method run() should be ignored and a real action |
| 79 |
result is supposed to be delivered in an asynchronous manner |
| 80 |
using public API. By default, if a concrete implementation |
| 81 |
doesn't override this method then the action is synchronous. |
| 82 |
""" |
| 83 |
return True |
| 84 |
|
| 85 |
@classmethod |
| 86 |
def get_serialization_key(cls): |
| 87 |
# By default we use the same serializer key for every action |
| 88 |
# assuming that most action class can use the same serializer. |
| 89 |
return "%s.%s" % (Action.__module__, Action.__name__) |
| 90 |
|
| 91 |
|
| 92 |
class ActionSerializer(serialization.DictBasedSerializer): |
| 93 |
def serialize_to_dict(self, entity): |
| 94 |
cls = type(entity) |
| 95 |
|
| 96 |
return { |
| 97 |
'cls': '%s.%s' % (cls.__module__, cls.__name__), |
| 98 |
'cls_attrs': i_utils.get_public_fields(cls), |
| 99 |
'data': vars(entity), |
| 100 |
} |
| 101 |
|
| 102 |
def deserialize_from_dict(self, entity_dict): |
| 103 |
cls_str = entity_dict['cls'] |
| 104 |
|
| 105 |
# Rebuild action class and restore attributes. |
| 106 |
cls = importutils.import_class(cls_str) |
| 107 |
|
| 108 |
cls_attrs = entity_dict['cls_attrs'] |
| 109 |
|
| 110 |
if cls_attrs: |
| 111 |
# If we have serialized class attributes it means that we need |
| 112 |
# to create a dynamic class. |
| 113 |
cls = type(cls.__name__, (cls,), cls_attrs) |
| 114 |
|
| 115 |
# NOTE(rakhmerov): We use this hacky was of instantiating |
| 116 |
# the action here because we can't use normal __init__(), |
| 117 |
# we don't know the parameters. And even if we find out |
| 118 |
# what they are the real internal state of the object is |
| 119 |
# what was stored as vars() method that just returns all |
| 120 |
# fields. So we have to bypass __init__() and set attributes |
| 121 |
# one by one. Of course, this is a serious limitation since |
| 122 |
# action creators will need to keep in mind to avoid having |
| 123 |
# some complex initialisation logic in __init__() that |
| 124 |
# does something not reflecting in an instance state. |
| 125 |
# However, this all applies to the case when the action |
| 126 |
# has to be sent to a remote executor. |
| 127 |
action = cls.__new__(cls) |
| 128 |
|
| 129 |
for k, v in entity_dict['data'].items(): |
| 130 |
setattr(action, k, v) |
| 131 |
|
| 132 |
return action |
| 133 |
|
| 134 |
|
| 135 |
# NOTE: Every action implementation can register its own serializer |
| 136 |
# if needed, but this serializer should work for vast of majority of |
| 137 |
# actions. |
| 138 |
serialization.register_serializer(Action, ActionSerializer()) |
| 139 |
|
| 140 |
|
| 141 |
class ActionDescriptor(abc.ABC): |
| 142 |
"""Provides required information about a certain type of actions. |
| 143 |
|
| 144 |
Action descriptor is not an action itself. It rather carries all |
| 145 |
important meta information about a particular action before the |
| 146 |
action is instantiated. In some sense it is similar to a class but |
| 147 |
the difference is that one type of action descriptor may support |
| 148 |
many different actions. This abstraction is responsible for |
| 149 |
validation of input parameters and instantiation of an action. |
| 150 |
|
| 151 |
""" |
| 152 |
|
| 153 |
@property |
| 154 |
@abc.abstractmethod |
| 155 |
def name(self): |
| 156 |
"""The name of the action.""" |
| 157 |
pass |
| 158 |
|
| 159 |
@property |
| 160 |
@abc.abstractmethod |
| 161 |
def description(self): |
| 162 |
"""The description of the action.""" |
| 163 |
pass |
| 164 |
|
| 165 |
@property |
| 166 |
@abc.abstractmethod |
| 167 |
def params_spec(self): |
| 168 |
"""Comma-separated string with input parameter names. |
| 169 |
|
| 170 |
Each parameter name can be either just a name or a string |
| 171 |
"param=val" where "param" is the name of the parameter |
| 172 |
and "val" its default value. The values are only indications |
| 173 |
for the user and not used in the action instantiation process. |
| 174 |
The string is split along the commas and then the parts along the |
| 175 |
equal signs. Escaping is not possible. |
| 176 |
""" |
| 177 |
pass |
| 178 |
|
| 179 |
@property |
| 180 |
@abc.abstractmethod |
| 181 |
def namespace(self): |
| 182 |
"""The namespace of the action. |
| 183 |
|
| 184 |
NOTE: Not all actions have to support namespaces. |
| 185 |
""" |
| 186 |
pass |
| 187 |
|
| 188 |
@property |
| 189 |
@abc.abstractmethod |
| 190 |
def project_id(self): |
| 191 |
"""The ID of the project (tenant) this action belongs to. |
| 192 |
|
| 193 |
If it's not specified then the action can be used within |
| 194 |
all projects (tenants). |
| 195 |
|
| 196 |
NOTE: Not all actions have to support projects(tenants). |
| 197 |
""" |
| 198 |
pass |
| 199 |
|
| 200 |
@property |
| 201 |
@abc.abstractmethod |
| 202 |
def scope(self): |
| 203 |
"""The scope of the action within a project (tenant). |
| 204 |
|
| 205 |
It makes sense only if the "project_id" property is not None. |
| 206 |
It should be assigned with the "public" value if the action |
| 207 |
is available in all projects and "private" if it's accessible |
| 208 |
only by users of the specified project. |
| 209 |
|
| 210 |
NOTE: Not all actions have to support projects(tenants). |
| 211 |
""" |
| 212 |
pass |
| 213 |
|
| 214 |
@property |
| 215 |
@abc.abstractmethod |
| 216 |
def action_class_name(self): |
| 217 |
"""String representation of the Python class of the action. |
| 218 |
|
| 219 |
Can be None in case if the action is dynamically generated with |
| 220 |
some kind of wrapper. |
| 221 |
""" |
| 222 |
pass |
| 223 |
|
| 224 |
@property |
| 225 |
@abc.abstractmethod |
| 226 |
def action_class_attributes(self): |
| 227 |
"""The attributes of the action Python class, if relevant. |
| 228 |
|
| 229 |
If the action has a static Python class associated with it |
| 230 |
and this method returns not an empty dictionary then the |
| 231 |
action can be instantiated from a new dynamic class |
| 232 |
based on the property "action_class_string" and this property |
| 233 |
that essentially carries public class field values. |
| 234 |
""" |
| 235 |
pass |
| 236 |
|
| 237 |
@abc.abstractmethod |
| 238 |
def instantiate(self, input_dict, wf_ctx): |
| 239 |
"""Instantiate the required action with the given parameters. |
| 240 |
|
| 241 |
:param input_dict: Action parameters as a dictionary where keys |
| 242 |
are parameter names and values are parameter values. |
| 243 |
:param wf_ctx: Workflow context relevant for the point when |
| 244 |
action is about to start. |
| 245 |
:return: An instance of mistral_lib.actions.Action. |
| 246 |
""" |
| 247 |
pass |
| 248 |
|
| 249 |
@abc.abstractmethod |
| 250 |
def check_parameters(self, params): |
| 251 |
"""Validate action parameters. |
| 252 |
|
| 253 |
The method does a preliminary check of the given actual action |
| 254 |
parameters and raises an exception if they don't match the |
| 255 |
action signature. However, a successful invocation of this |
| 256 |
method does not guarantee a further successful run of the |
| 257 |
instantiated action. |
| 258 |
|
| 259 |
:param params: Action parameters as a dictionary where keys |
| 260 |
are parameter names and values are parameter values. |
| 261 |
:return: None or raises an exception if the given parameters |
| 262 |
are not valid. |
| 263 |
""" |
| 264 |
pass |
| 265 |
|
| 266 |
@abc.abstractmethod |
| 267 |
def post_process_result(self, result): |
| 268 |
"""Converts the given action result. |
| 269 |
|
| 270 |
A certain action implementation may need to do an additional |
| 271 |
conversion of the action result by its descriptor. This approach |
| 272 |
allows to implement wrapper actions running asynchronously |
| 273 |
because in such cases, the initial action result depends on a 3rd |
| 274 |
party that's responsible for delivering it to Mistral. But when |
| 275 |
it comes to Mistral we still have a chance to apply needed |
| 276 |
transformations defined by this method. |
| 277 |
|
| 278 |
:param result: Action result. |
| 279 |
An instance of mistral_lib.types.Result. |
| 280 |
:return: Converted action result. |
| 281 |
""" |
| 282 |
pass |
| 283 |
|
| 284 |
|
| 285 |
class ActionProvider(abc.ABC): |
| 286 |
"""Serves as a source of actions for the system. |
| 287 |
|
| 288 |
A concrete implementation of this interface can use its own |
| 289 |
way of delivering actions to the system. It can store actions |
| 290 |
in a database, get them over HTTP, AMQP or any other transport. |
| 291 |
Or it can simply provide a static collection of actions and keep |
| 292 |
them in memory throughout the cycle of the application. |
| 293 |
""" |
| 294 |
|
| 295 |
def __init__(self, name): |
| 296 |
self._name = name |
| 297 |
|
| 298 |
@property |
| 299 |
def name(self): |
| 300 |
"""The name of the action provider. |
| 301 |
|
| 302 |
Different action providers can use it differently. |
| 303 |
Some may completely ignore it, others may use it, for example, |
| 304 |
for searching actions in a certain way. |
| 305 |
""" |
| 306 |
return self._name |
| 307 |
|
| 308 |
@abc.abstractmethod |
| 309 |
def find(self, action_name, namespace=None): |
| 310 |
"""Finds action descriptor by name. |
| 311 |
|
| 312 |
:param action_name: Action name. |
| 313 |
:param namespace: Action namespace. None is used for the default |
| 314 |
namespace. |
| 315 |
:return: An instance of ActionDescriptor or None, if not found. |
| 316 |
""" |
| 317 |
pass |
| 318 |
|
| 319 |
@abc.abstractmethod |
| 320 |
def find_all(self, namespace=None, limit=None, sort_fields=None, |
| 321 |
sort_dirs=None, filters=None): |
| 322 |
"""Finds all action descriptors for this provider. |
| 323 |
|
| 324 |
:param namespace: Optional. Action namespace. |
| 325 |
:param limit: Positive integer or None. Maximum number of action |
| 326 |
descriptors to return in a single result. |
| 327 |
:param sort_fields: Optional. A list of action descriptor fields |
| 328 |
that define sorting of the result set. |
| 329 |
:param sort_dirs: Optional. A list of sorting orders ("asc" or "desc") |
| 330 |
in addition to the "sort_fields" argument. |
| 331 |
:param filters: Optional. A dictionary describing AND-joined filters. |
| 332 |
:return: List of ActionDescriptor instances. |
| 333 |
""" |
| 334 |
pass |