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
8 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 from .consumer import Consumer, ConsumerChain
17 from ...utils.formatting import json_dumps, yaml_dumps
20 class DeriveServiceTemplate(Consumer):
22 Derives the service template from the presenter.
26 if self.context.presentation.presenter is None:
27 self.context.validation.report('DeriveServiceTemplate consumer: missing presenter')
30 if not hasattr(self.context.presentation.presenter, '_get_model'):
31 self.context.validation.report('DeriveServiceTemplate consumer: presenter does not'
32 ' support "_get_model"')
35 self.context.modeling.template = \
36 self.context.presentation.presenter._get_model(self.context)
39 class CoerceServiceTemplateValues(Consumer):
41 Coerces values in the service template.
45 self.topology.coerce(self.context.modeling.template, report_issues=True)
48 class ValidateServiceTemplate(Consumer):
50 Validates the service template.
54 self.topology.validate(self.context.modeling.template)
57 class ServiceTemplate(ConsumerChain):
59 Generates the service template from the presenter.
62 def __init__(self, context):
63 super(ServiceTemplate, self).__init__(context, (DeriveServiceTemplate,
64 CoerceServiceTemplateValues,
65 ValidateServiceTemplate))
68 if self.context.has_arg_switch('yaml'):
69 indent = self.context.get_arg_value_int('indent', 2)
70 raw = self.context.modeling.template_as_raw
71 self.context.write(yaml_dumps(raw, indent=indent))
72 elif self.context.has_arg_switch('json'):
73 indent = self.context.get_arg_value_int('indent', 2)
74 raw = self.context.modeling.template_as_raw
75 self.context.write(json_dumps(raw, indent=indent))
77 self.context.write(self.topology.dump(self.context.modeling.template))
80 class Types(Consumer):
82 Used to just dump the types.
86 if self.context.has_arg_switch('yaml'):
87 indent = self.context.get_arg_value_int('indent', 2)
88 raw = self.context.modeling.types_as_raw
89 self.context.write(yaml_dumps(raw, indent=indent))
90 elif self.context.has_arg_switch('json'):
91 indent = self.context.get_arg_value_int('indent', 2)
92 raw = self.context.modeling.types_as_raw
93 self.context.write(json_dumps(raw, indent=indent))
95 self.topology.dump_types(self.context, self.context.modeling.template)
98 class InstantiateServiceInstance(Consumer):
100 Instantiates the service template into a service instance.
104 if self.context.modeling.template is None:
105 self.context.validation.report('InstantiateServiceInstance consumer: missing service '
108 self.context.modeling.instance = self.topology.instantiate(
109 self.context.modeling.template,
110 inputs=dict(self.context.modeling.inputs)
114 class CoerceServiceInstanceValues(Consumer):
116 Coerces values in the service instance.
120 self.topology.coerce(self.context.modeling.instance, report_issues=True)
123 class ValidateServiceInstance(Consumer):
125 Validates the service instance.
129 self.topology.validate(self.context.modeling.instance)
132 class SatisfyRequirements(Consumer):
134 Satisfies node requirements in the service instance.
138 self.topology.satisfy_requirements(self.context.modeling.instance)
141 class ValidateCapabilities(Consumer):
143 Validates capabilities in the service instance.
147 self.topology.validate_capabilities(self.context.modeling.instance)
150 class FindHosts(Consumer):
152 Find hosts for all nodes in the service instance.
156 self.topology.assign_hosts(self.context.modeling.instance)
159 class ConfigureOperations(Consumer):
161 Configures all operations in the service instance.
165 self.topology.configure_operations(self.context.modeling.instance)
168 class ServiceInstance(ConsumerChain):
170 Generates the service instance by instantiating the service template.
173 def __init__(self, context):
174 super(ServiceInstance, self).__init__(context, (InstantiateServiceInstance,
175 CoerceServiceInstanceValues,
176 ValidateServiceInstance,
177 CoerceServiceInstanceValues,
179 CoerceServiceInstanceValues,
180 ValidateCapabilities,
183 CoerceServiceInstanceValues))
186 if self.context.has_arg_switch('graph'):
187 self.context.modeling.instance.dump_graph()
188 elif self.context.has_arg_switch('yaml'):
189 indent = self.context.get_arg_value_int('indent', 2)
190 raw = self.context.modeling.instance_as_raw
191 self.context.write(yaml_dumps(raw, indent=indent))
192 elif self.context.has_arg_switch('json'):
193 indent = self.context.get_arg_value_int('indent', 2)
194 raw = self.context.modeling.instance_as_raw
195 self.context.write(json_dumps(raw, indent=indent))
197 str_rep = self.topology.dump(self.context.modeling.instance)
198 self.context.write(str_rep)