summaryrefslogtreecommitdiffstats
path: root/azure/aria/aria-extension-cloudify/src/aria/aria/modeling/service_common.py
blob: d1f6b00c186819791319b8223623a749cb268095 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
ARIA modeling service common module
"""

# pylint: disable=no-self-argument, no-member, abstract-method

from sqlalchemy import (
    Column,
    Text,
    Boolean
)
from sqlalchemy.ext.declarative import declared_attr

from ..utils import (
    collections,
    formatting
)
from .mixins import InstanceModelMixin, TemplateModelMixin, ParameterMixin
from . import relationship


class OutputBase(ParameterMixin):
    """
    Output parameter or declaration for an output parameter.
    """

    __tablename__ = 'output'

    # region many_to_one relationships

    @declared_attr
    def service_template(cls):
        """
        Containing service template (can be ``None``).

        :type: :class:`ServiceTemplate`
        """
        return relationship.many_to_one(cls, 'service_template')

    @declared_attr
    def service(cls):
        """
        Containing service (can be ``None``).

        :type: :class:`ServiceTemplate`
        """
        return relationship.many_to_one(cls, 'service')

    # endregion

    # region foreign keys

    @declared_attr
    def service_template_fk(cls):
        return relationship.foreign_key('service_template', nullable=True)

    @declared_attr
    def service_fk(cls):
        return relationship.foreign_key('service', nullable=True)

    # endregion


class InputBase(ParameterMixin):
    """
    Input parameter or declaration for an input parameter.
    """

    __tablename__ = 'input'

    required = Column(Boolean, doc="""
    Is the input mandatory.

    :type: :obj:`bool`
    """)

    @classmethod
    def wrap(cls, name, value, description=None, required=True):  # pylint: disable=arguments-differ
        input = super(InputBase, cls).wrap(name, value, description)
        input.required = required
        return input

    # region many_to_one relationships

    @declared_attr
    def service_template(cls):
        """
        Containing service template (can be ``None``).

        :type: :class:`ServiceTemplate`
        """
        return relationship.many_to_one(cls, 'service_template')

    @declared_attr
    def service(cls):
        """
        Containing service (can be ``None``).

        :type: :class:`Service`
        """
        return relationship.many_to_one(cls, 'service')

    @declared_attr
    def interface(cls):
        """
        Containing interface (can be ``None``).

        :type: :class:`Interface`
        """
        return relationship.many_to_one(cls, 'interface')

    @declared_attr
    def operation(cls):
        """
        Containing operation (can be ``None``).

        :type: :class:`Operation`
        """
        return relationship.many_to_one(cls, 'operation')

    @declared_attr
    def interface_template(cls):
        """
        Containing interface template (can be ``None``).

        :type: :class:`InterfaceTemplate`
        """
        return relationship.many_to_one(cls, 'interface_template')

    @declared_attr
    def operation_template(cls):
        """
        Containing operation template (can be ``None``).

        :type: :class:`OperationTemplate`
        """
        return relationship.many_to_one(cls, 'operation_template')

    @declared_attr
    def execution(cls):
        """
        Containing execution (can be ``None``).

        :type: :class:`Execution`
        """
        return relationship.many_to_one(cls, 'execution')

    # endregion

    # region foreign keys

    @declared_attr
    def service_template_fk(cls):
        return relationship.foreign_key('service_template', nullable=True)

    @declared_attr
    def service_fk(cls):
        return relationship.foreign_key('service', nullable=True)

    @declared_attr
    def interface_fk(cls):
        return relationship.foreign_key('interface', nullable=True)

    @declared_attr
    def operation_fk(cls):
        return relationship.foreign_key('operation', nullable=True)

    @declared_attr
    def interface_template_fk(cls):
        return relationship.foreign_key('interface_template', nullable=True)

    @declared_attr
    def operation_template_fk(cls):
        return relationship.foreign_key('operation_template', nullable=True)

    @declared_attr
    def execution_fk(cls):
        return relationship.foreign_key('execution', nullable=True)

    @declared_attr
    def task_fk(cls):
        return relationship.foreign_key('task', nullable=True)

    # endregion


class ConfigurationBase(ParameterMixin):
    """
    Configuration parameter.
    """

    __tablename__ = 'configuration'

    # region many_to_one relationships

    @declared_attr
    def operation_template(cls):
        """
        Containing operation template (can be ``None``).

        :type: :class:`OperationTemplate`
        """
        return relationship.many_to_one(cls, 'operation_template')

    @declared_attr
    def operation(cls):
        """
        Containing operation (can be ``None``).

        :type: :class:`Operation`
        """
        return relationship.many_to_one(cls, 'operation')

    # endregion

    # region foreign keys

    @declared_attr
    def operation_template_fk(cls):
        return relationship.foreign_key('operation_template', nullable=True)

    @declared_attr
    def operation_fk(cls):
        return relationship.foreign_key('operation', nullable=True)

    # endregion


class PropertyBase(ParameterMixin):
    """
    Property parameter or declaration for a property parameter.
    """

    __tablename__ = 'property'

    # region many_to_one relationships

    @declared_attr
    def node_template(cls):
        """
        Containing node template (can be ``None``).

        :type: :class:`NodeTemplate`
        """
        return relationship.many_to_one(cls, 'node_template')

    @declared_attr
    def group_template(cls):
        """
        Containing group template (can be ``None``).

        :type: :class:`GroupTemplate`
        """
        return relationship.many_to_one(cls, 'group_template')

    @declared_attr
    def policy_template(cls):
        """
        Containing policy template (can be ``None``).

        :type: :class:`PolicyTemplate`
        """
        return relationship.many_to_one(cls, 'policy_template')

    @declared_attr
    def relationship_template(cls):
        """
        Containing relationship template (can be ``None``).

        :type: :class:`RelationshipTemplate`
        """
        return relationship.many_to_one(cls, 'relationship_template')

    @declared_attr
    def capability_template(cls):
        """
        Containing capability template (can be ``None``).

        :type: :class:`CapabilityTemplate`
        """
        return relationship.many_to_one(cls, 'capability_template')

    @declared_attr
    def artifact_template(cls):
        """
        Containing artifact template (can be ``None``).

        :type: :class:`ArtifactTemplate`
        """
        return relationship.many_to_one(cls, 'artifact_template')

    @declared_attr
    def node(cls):
        """
        Containing node (can be ``None``).

        :type: :class:`Node`
        """
        return relationship.many_to_one(cls, 'node')

    @declared_attr
    def group(cls):
        """
        Containing group (can be ``None``).

        :type: :class:`Group`
        """
        return relationship.many_to_one(cls, 'group')

    @declared_attr
    def policy(cls):
        """
        Containing policy (can be ``None``).

        :type: :class:`Policy`
        """
        return relationship.many_to_one(cls, 'policy')

    @declared_attr
    def relationship(cls):
        """
        Containing relationship (can be ``None``).

        :type: :class:`Relationship`
        """
        return relationship.many_to_one(cls, 'relationship')

    @declared_attr
    def capability(cls):
        """
        Containing capability (can be ``None``).

        :type: :class:`Capability`
        """
        return relationship.many_to_one(cls, 'capability')

    @declared_attr
    def artifact(cls):
        """
        Containing artifact (can be ``None``).

        :type: :class:`Artifact`
        """
        return relationship.many_to_one(cls, 'artifact')

    # endregion

    # region foreign keys

    @declared_attr
    def node_template_fk(cls):
        return relationship.foreign_key('node_template', nullable=True)

    @declared_attr
    def group_template_fk(cls):
        return relationship.foreign_key('group_template', nullable=True)

    @declared_attr
    def policy_template_fk(cls):
        return relationship.foreign_key('policy_template', nullable=True)

    @declared_attr
    def relationship_template_fk(cls):
        return relationship.foreign_key('relationship_template', nullable=True)

    @declared_attr
    def capability_template_fk(cls):
        return relationship.foreign_key('capability_template', nullable=True)

    @declared_attr
    def artifact_template_fk(cls):
        return relationship.foreign_key('artifact_template', nullable=True)

    @declared_attr
    def node_fk(cls):
        return relationship.foreign_key('node', nullable=True)

    @declared_attr
    def group_fk(cls):
        return relationship.foreign_key('group', nullable=True)

    @declared_attr
    def policy_fk(cls):
        return relationship.foreign_key('policy', nullable=True)

    @declared_attr
    def relationship_fk(cls):
        return relationship.foreign_key('relationship', nullable=True)

    @declared_attr
    def capability_fk(cls):
        return relationship.foreign_key('capability', nullable=True)

    @declared_attr
    def artifact_fk(cls):
        return relationship.foreign_key('artifact', nullable=True)

    # endregion


class AttributeBase(ParameterMixin):
    """
    Attribute parameter or declaration for an attribute parameter.
    """

    __tablename__ = 'attribute'

    # region many_to_one relationships

    @declared_attr
    def node_template(cls):
        """
        Containing node template (can be ``None``).

        :type: :class:`NodeTemplate`
        """
        return relationship.many_to_one(cls, 'node_template')

    @declared_attr
    def node(cls):
        """
        Containing node (can be ``None``).

        :type: :class:`Node`
        """
        return relationship.many_to_one(cls, 'node')

    # endregion

    # region foreign keys

    @declared_attr
    def node_template_fk(cls):
        """For Attribute many-to-one to NodeTemplate"""
        return relationship.foreign_key('node_template', nullable=True)

    @declared_attr
    def node_fk(cls):
        """For Attribute many-to-one to Node"""
        return relationship.foreign_key('node', nullable=True)

    # endregion


class TypeBase(InstanceModelMixin):
    """
    Type and its children. Can serve as the root for a type hierarchy.
    """

    __tablename__ = 'type'

    __private_fields__ = ('parent_type_fk',)

    variant = Column(Text, nullable=False)

    description = Column(Text, doc="""
    Human-readable description.

    :type: :obj:`basestring`
    """)

    _role = Column(Text, name='role')

    # region one_to_one relationships

    @declared_attr
    def parent(cls):
        """
        Parent type (will be ``None`` for the root of a type hierarchy).

        :type: :class:`Type`
        """
        return relationship.one_to_one_self(cls, 'parent_type_fk')

    # endregion

    # region one_to_many relationships

    @declared_attr
    def children(cls):
        """
        Children.

        :type: [:class:`Type`]
        """
        return relationship.one_to_many(cls, other_fk='parent_type_fk', self=True)

    # endregion

    # region foreign keys

    @declared_attr
    def parent_type_fk(cls):
        """For Type one-to-many to Type"""
        return relationship.foreign_key('type', nullable=True)

    # endregion

    @property
    def role(self):
        def get_role(the_type):
            if the_type is None:
                return None
            elif the_type._role is None:
                return get_role(the_type.parent)
            return the_type._role

        return get_role(self)

    @role.setter
    def role(self, value):
        self._role = value

    def is_descendant(self, base_name, name):
        base = self.get_descendant(base_name)
        if base is not None:
            if base.get_descendant(name) is not None:
                return True
        return False

    def get_descendant(self, name):
        if self.name == name:
            return self
        for child in self.children:
            found = child.get_descendant(name)
            if found is not None:
                return found
        return None

    def iter_descendants(self):
        for child in self.children:
            yield child
            for descendant in child.iter_descendants():
                yield descendant

    @property
    def as_raw(self):
        return collections.OrderedDict((
            ('name', self.name),
            ('description', self.description),
            ('role', self.role)))

    @property
    def as_raw_all(self):
        types = []
        self._append_raw_children(types)
        return types

    def _append_raw_children(self, types):
        for child in self.children:
            raw_child = formatting.as_raw(child)
            raw_child['parent'] = self.name
            types.append(raw_child)
            child._append_raw_children(types)

    @property
    def hierarchy(self):
        """
        Type hierarchy as a list beginning with this type and ending in the root.

        :type: [:class:`Type`]
        """
        return [self] + (self.parent.hierarchy if self.parent else [])


class MetadataBase(TemplateModelMixin):
    """
    Custom values associated with the service.

    This model is used by both service template and service instance elements.

    :ivar name: name
    :vartype name: basestring
    :ivar value: value
    :vartype value: basestring
    """

    __tablename__ = 'metadata'

    value = Column(Text)

    @property
    def as_raw(self):
        return collections.OrderedDict((
            ('name', self.name),
            ('value', self.value)))