vFW and vDNS support added to azure-plugin
[multicloud/azure.git] / azure / aria / aria-extension-cloudify / src / aria / aria / parser / consumption / consumer.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 from ...exceptions import AriaException
18 from ...utils.exceptions import print_exception
19 from ..validation import Issue
20
21
22 class Consumer(object):
23     """
24     Base class for ARIA consumers.
25
26     Consumers provide useful functionality by consuming presentations.
27     """
28
29     def __init__(self, context):
30         from ...orchestrator import topology
31
32         self.topology = topology.Topology()
33         self.context = context
34
35     def consume(self):
36         pass
37
38     def dump(self):
39         pass
40
41     def _handle_exception(self, e):
42         if hasattr(e, 'issue') and isinstance(e.issue, Issue):
43             self.context.validation.report(issue=e.issue)
44         else:
45             self.context.validation.report(exception=e)
46         if not isinstance(e, AriaException):
47             print_exception(e)
48
49
50 class ConsumerChain(Consumer):
51     """
52     ARIA consumer chain.
53
54     Calls consumers in order, handling exception by calling ``_handle_exception`` on them, and stops
55     the chain if there are any validation issues.
56     """
57
58     def __init__(self, context, consumer_classes=None, handle_exceptions=True):
59         super(ConsumerChain, self).__init__(context)
60         self.handle_exceptions = handle_exceptions
61         self.consumers = []
62         if consumer_classes:
63             for consumer_class in consumer_classes:
64                 self.append(consumer_class)
65
66     def append(self, *consumer_classes):
67         for consumer_class in consumer_classes:
68             self.consumers.append(consumer_class(self.context))
69
70     def consume(self):
71         for consumer in self.consumers:
72             try:
73                 consumer.consume()
74             except BaseException as e:
75                 if self.handle_exceptions:
76                     handle_exception(consumer, e)
77                 else:
78                     raise e
79
80             if consumer.topology.has_issues:
81                 self.context.validation.extend_issues(consumer.topology.issues)
82
83             if self.context.validation.has_issues:
84                 break
85
86
87 def handle_exception(consumer, e):
88     if isinstance(e, AriaException) and e.issue:
89         consumer.context.validation.report(issue=e.issue)
90     else:
91         consumer.context.validation.report(exception=e)
92     if not isinstance(e, AriaException):
93         print_exception(e)