vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / modeling / service_common.py
1 # Licensed to the Apache Software Foundation (ASF) under one or more
2 # contributor license agreements.  See the NOTICE file distributed with
3 # this work for additional information regarding copyright ownership.
4 # The ASF licenses this file to You under the Apache License, Version 2.0
5 # (the "License"); you may not use this file except in compliance with
6 # the License.  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 """
17 ARIA modeling service common module
18 """
19
20 # pylint: disable=no-self-argument, no-member, abstract-method
21
22 from sqlalchemy import (
23     Column,
24     Text,
25     Boolean
26 )
27 from sqlalchemy.ext.declarative import declared_attr
28
29 from ..utils import (
30     collections,
31     formatting
32 )
33 from .mixins import InstanceModelMixin, TemplateModelMixin, ParameterMixin
34 from . import relationship
35
36
37 class OutputBase(ParameterMixin):
38     """
39     Output parameter or declaration for an output parameter.
40     """
41
42     __tablename__ = 'output'
43
44     # region many_to_one relationships
45
46     @declared_attr
47     def service_template(cls):
48         """
49         Containing service template (can be ``None``).
50
51         :type: :class:`ServiceTemplate`
52         """
53         return relationship.many_to_one(cls, 'service_template')
54
55     @declared_attr
56     def service(cls):
57         """
58         Containing service (can be ``None``).
59
60         :type: :class:`ServiceTemplate`
61         """
62         return relationship.many_to_one(cls, 'service')
63
64     # endregion
65
66     # region foreign keys
67
68     @declared_attr
69     def service_template_fk(cls):
70         return relationship.foreign_key('service_template', nullable=True)
71
72     @declared_attr
73     def service_fk(cls):
74         return relationship.foreign_key('service', nullable=True)
75
76     # endregion
77
78
79 class InputBase(ParameterMixin):
80     """
81     Input parameter or declaration for an input parameter.
82     """
83
84     __tablename__ = 'input'
85
86     required = Column(Boolean, doc="""
87     Is the input mandatory.
88
89     :type: :obj:`bool`
90     """)
91
92     @classmethod
93     def wrap(cls, name, value, description=None, required=True):  # pylint: disable=arguments-differ
94         input = super(InputBase, cls).wrap(name, value, description)
95         input.required = required
96         return input
97
98     # region many_to_one relationships
99
100     @declared_attr
101     def service_template(cls):
102         """
103         Containing service template (can be ``None``).
104
105         :type: :class:`ServiceTemplate`
106         """
107         return relationship.many_to_one(cls, 'service_template')
108
109     @declared_attr
110     def service(cls):
111         """
112         Containing service (can be ``None``).
113
114         :type: :class:`Service`
115         """
116         return relationship.many_to_one(cls, 'service')
117
118     @declared_attr
119     def interface(cls):
120         """
121         Containing interface (can be ``None``).
122
123         :type: :class:`Interface`
124         """
125         return relationship.many_to_one(cls, 'interface')
126
127     @declared_attr
128     def operation(cls):
129         """
130         Containing operation (can be ``None``).
131
132         :type: :class:`Operation`
133         """
134         return relationship.many_to_one(cls, 'operation')
135
136     @declared_attr
137     def interface_template(cls):
138         """
139         Containing interface template (can be ``None``).
140
141         :type: :class:`InterfaceTemplate`
142         """
143         return relationship.many_to_one(cls, 'interface_template')
144
145     @declared_attr
146     def operation_template(cls):
147         """
148         Containing operation template (can be ``None``).
149
150         :type: :class:`OperationTemplate`
151         """
152         return relationship.many_to_one(cls, 'operation_template')
153
154     @declared_attr
155     def execution(cls):
156         """
157         Containing execution (can be ``None``).
158
159         :type: :class:`Execution`
160         """
161         return relationship.many_to_one(cls, 'execution')
162
163     # endregion
164
165     # region foreign keys
166
167     @declared_attr
168     def service_template_fk(cls):
169         return relationship.foreign_key('service_template', nullable=True)
170
171     @declared_attr
172     def service_fk(cls):
173         return relationship.foreign_key('service', nullable=True)
174
175     @declared_attr
176     def interface_fk(cls):
177         return relationship.foreign_key('interface', nullable=True)
178
179     @declared_attr
180     def operation_fk(cls):
181         return relationship.foreign_key('operation', nullable=True)
182
183     @declared_attr
184     def interface_template_fk(cls):
185         return relationship.foreign_key('interface_template', nullable=True)
186
187     @declared_attr
188     def operation_template_fk(cls):
189         return relationship.foreign_key('operation_template', nullable=True)
190
191     @declared_attr
192     def execution_fk(cls):
193         return relationship.foreign_key('execution', nullable=True)
194
195     @declared_attr
196     def task_fk(cls):
197         return relationship.foreign_key('task', nullable=True)
198
199     # endregion
200
201
202 class ConfigurationBase(ParameterMixin):
203     """
204     Configuration parameter.
205     """
206
207     __tablename__ = 'configuration'
208
209     # region many_to_one relationships
210
211     @declared_attr
212     def operation_template(cls):
213         """
214         Containing operation template (can be ``None``).
215
216         :type: :class:`OperationTemplate`
217         """
218         return relationship.many_to_one(cls, 'operation_template')
219
220     @declared_attr
221     def operation(cls):
222         """
223         Containing operation (can be ``None``).
224
225         :type: :class:`Operation`
226         """
227         return relationship.many_to_one(cls, 'operation')
228
229     # endregion
230
231     # region foreign keys
232
233     @declared_attr
234     def operation_template_fk(cls):
235         return relationship.foreign_key('operation_template', nullable=True)
236
237     @declared_attr
238     def operation_fk(cls):
239         return relationship.foreign_key('operation', nullable=True)
240
241     # endregion
242
243
244 class PropertyBase(ParameterMixin):
245     """
246     Property parameter or declaration for a property parameter.
247     """
248
249     __tablename__ = 'property'
250
251     # region many_to_one relationships
252
253     @declared_attr
254     def node_template(cls):
255         """
256         Containing node template (can be ``None``).
257
258         :type: :class:`NodeTemplate`
259         """
260         return relationship.many_to_one(cls, 'node_template')
261
262     @declared_attr
263     def group_template(cls):
264         """
265         Containing group template (can be ``None``).
266
267         :type: :class:`GroupTemplate`
268         """
269         return relationship.many_to_one(cls, 'group_template')
270
271     @declared_attr
272     def policy_template(cls):
273         """
274         Containing policy template (can be ``None``).
275
276         :type: :class:`PolicyTemplate`
277         """
278         return relationship.many_to_one(cls, 'policy_template')
279
280     @declared_attr
281     def relationship_template(cls):
282         """
283         Containing relationship template (can be ``None``).
284
285         :type: :class:`RelationshipTemplate`
286         """
287         return relationship.many_to_one(cls, 'relationship_template')
288
289     @declared_attr
290     def capability_template(cls):
291         """
292         Containing capability template (can be ``None``).
293
294         :type: :class:`CapabilityTemplate`
295         """
296         return relationship.many_to_one(cls, 'capability_template')
297
298     @declared_attr
299     def artifact_template(cls):
300         """
301         Containing artifact template (can be ``None``).
302
303         :type: :class:`ArtifactTemplate`
304         """
305         return relationship.many_to_one(cls, 'artifact_template')
306
307     @declared_attr
308     def node(cls):
309         """
310         Containing node (can be ``None``).
311
312         :type: :class:`Node`
313         """
314         return relationship.many_to_one(cls, 'node')
315
316     @declared_attr
317     def group(cls):
318         """
319         Containing group (can be ``None``).
320
321         :type: :class:`Group`
322         """
323         return relationship.many_to_one(cls, 'group')
324
325     @declared_attr
326     def policy(cls):
327         """
328         Containing policy (can be ``None``).
329
330         :type: :class:`Policy`
331         """
332         return relationship.many_to_one(cls, 'policy')
333
334     @declared_attr
335     def relationship(cls):
336         """
337         Containing relationship (can be ``None``).
338
339         :type: :class:`Relationship`
340         """
341         return relationship.many_to_one(cls, 'relationship')
342
343     @declared_attr
344     def capability(cls):
345         """
346         Containing capability (can be ``None``).
347
348         :type: :class:`Capability`
349         """
350         return relationship.many_to_one(cls, 'capability')
351
352     @declared_attr
353     def artifact(cls):
354         """
355         Containing artifact (can be ``None``).
356
357         :type: :class:`Artifact`
358         """
359         return relationship.many_to_one(cls, 'artifact')
360
361     # endregion
362
363     # region foreign keys
364
365     @declared_attr
366     def node_template_fk(cls):
367         return relationship.foreign_key('node_template', nullable=True)
368
369     @declared_attr
370     def group_template_fk(cls):
371         return relationship.foreign_key('group_template', nullable=True)
372
373     @declared_attr
374     def policy_template_fk(cls):
375         return relationship.foreign_key('policy_template', nullable=True)
376
377     @declared_attr
378     def relationship_template_fk(cls):
379         return relationship.foreign_key('relationship_template', nullable=True)
380
381     @declared_attr
382     def capability_template_fk(cls):
383         return relationship.foreign_key('capability_template', nullable=True)
384
385     @declared_attr
386     def artifact_template_fk(cls):
387         return relationship.foreign_key('artifact_template', nullable=True)
388
389     @declared_attr
390     def node_fk(cls):
391         return relationship.foreign_key('node', nullable=True)
392
393     @declared_attr
394     def group_fk(cls):
395         return relationship.foreign_key('group', nullable=True)
396
397     @declared_attr
398     def policy_fk(cls):
399         return relationship.foreign_key('policy', nullable=True)
400
401     @declared_attr
402     def relationship_fk(cls):
403         return relationship.foreign_key('relationship', nullable=True)
404
405     @declared_attr
406     def capability_fk(cls):
407         return relationship.foreign_key('capability', nullable=True)
408
409     @declared_attr
410     def artifact_fk(cls):
411         return relationship.foreign_key('artifact', nullable=True)
412
413     # endregion
414
415
416 class AttributeBase(ParameterMixin):
417     """
418     Attribute parameter or declaration for an attribute parameter.
419     """
420
421     __tablename__ = 'attribute'
422
423     # region many_to_one relationships
424
425     @declared_attr
426     def node_template(cls):
427         """
428         Containing node template (can be ``None``).
429
430         :type: :class:`NodeTemplate`
431         """
432         return relationship.many_to_one(cls, 'node_template')
433
434     @declared_attr
435     def node(cls):
436         """
437         Containing node (can be ``None``).
438
439         :type: :class:`Node`
440         """
441         return relationship.many_to_one(cls, 'node')
442
443     # endregion
444
445     # region foreign keys
446
447     @declared_attr
448     def node_template_fk(cls):
449         """For Attribute many-to-one to NodeTemplate"""
450         return relationship.foreign_key('node_template', nullable=True)
451
452     @declared_attr
453     def node_fk(cls):
454         """For Attribute many-to-one to Node"""
455         return relationship.foreign_key('node', nullable=True)
456
457     # endregion
458
459
460 class TypeBase(InstanceModelMixin):
461     """
462     Type and its children. Can serve as the root for a type hierarchy.
463     """
464
465     __tablename__ = 'type'
466
467     __private_fields__ = ('parent_type_fk',)
468
469     variant = Column(Text, nullable=False)
470
471     description = Column(Text, doc="""
472     Human-readable description.
473
474     :type: :obj:`basestring`
475     """)
476
477     _role = Column(Text, name='role')
478
479     # region one_to_one relationships
480
481     @declared_attr
482     def parent(cls):
483         """
484         Parent type (will be ``None`` for the root of a type hierarchy).
485
486         :type: :class:`Type`
487         """
488         return relationship.one_to_one_self(cls, 'parent_type_fk')
489
490     # endregion
491
492     # region one_to_many relationships
493
494     @declared_attr
495     def children(cls):
496         """
497         Children.
498
499         :type: [:class:`Type`]
500         """
501         return relationship.one_to_many(cls, other_fk='parent_type_fk', self=True)
502
503     # endregion
504
505     # region foreign keys
506
507     @declared_attr
508     def parent_type_fk(cls):
509         """For Type one-to-many to Type"""
510         return relationship.foreign_key('type', nullable=True)
511
512     # endregion
513
514     @property
515     def role(self):
516         def get_role(the_type):
517             if the_type is None:
518                 return None
519             elif the_type._role is None:
520                 return get_role(the_type.parent)
521             return the_type._role
522
523         return get_role(self)
524
525     @role.setter
526     def role(self, value):
527         self._role = value
528
529     def is_descendant(self, base_name, name):
530         base = self.get_descendant(base_name)
531         if base is not None:
532             if base.get_descendant(name) is not None:
533                 return True
534         return False
535
536     def get_descendant(self, name):
537         if self.name == name:
538             return self
539         for child in self.children:
540             found = child.get_descendant(name)
541             if found is not None:
542                 return found
543         return None
544
545     def iter_descendants(self):
546         for child in self.children:
547             yield child
548             for descendant in child.iter_descendants():
549                 yield descendant
550
551     @property
552     def as_raw(self):
553         return collections.OrderedDict((
554             ('name', self.name),
555             ('description', self.description),
556             ('role', self.role)))
557
558     @property
559     def as_raw_all(self):
560         types = []
561         self._append_raw_children(types)
562         return types
563
564     def _append_raw_children(self, types):
565         for child in self.children:
566             raw_child = formatting.as_raw(child)
567             raw_child['parent'] = self.name
568             types.append(raw_child)
569             child._append_raw_children(types)
570
571     @property
572     def hierarchy(self):
573         """
574         Type hierarchy as a list beginning with this type and ending in the root.
575
576         :type: [:class:`Type`]
577         """
578         return [self] + (self.parent.hierarchy if self.parent else [])
579
580
581 class MetadataBase(TemplateModelMixin):
582     """
583     Custom values associated with the service.
584
585     This model is used by both service template and service instance elements.
586
587     :ivar name: name
588     :vartype name: basestring
589     :ivar value: value
590     :vartype value: basestring
591     """
592
593     __tablename__ = 'metadata'
594
595     value = Column(Text)
596
597     @property
598     def as_raw(self):
599         return collections.OrderedDict((
600             ('name', self.name),
601             ('value', self.value)))