Merge "Update heat-translator requirement"
[parser.git] / tosca2heat / tosca-parser / toscaparser / parameters.py
1 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
2 #    not use this file except in compliance with the License. You may obtain
3 #    a copy of the License at
4 #
5 #         http://www.apache.org/licenses/LICENSE-2.0
6 #
7 #    Unless required by applicable law or agreed to in writing, software
8 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 #    License for the specific language governing permissions and limitations
11 #    under the License.
12
13
14 import logging
15
16 from toscaparser.common.exception import ExceptionCollector
17 from toscaparser.common.exception import MissingRequiredFieldError
18 from toscaparser.common.exception import UnknownFieldError
19 from toscaparser.dataentity import DataEntity
20 from toscaparser.elements.constraints import Schema
21 from toscaparser.elements.entity_type import EntityType
22 from toscaparser.utils.gettextutils import _
23
24
25 log = logging.getLogger('tosca')
26
27
28 class Input(object):
29
30     INPUTFIELD = (TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS, REQUIRED, STATUS,
31                   ENTRY_SCHEMA) = ('type', 'description', 'default',
32                                    'constraints', 'required', 'status',
33                                    'entry_schema')
34
35     def __init__(self, name, schema_dict, custom_defs=None):
36         self.name = name
37         self.schema = Schema(name, schema_dict)
38         self.custom_defs = custom_defs or {}
39
40         self._validate_field()
41         self.validate_type(self.type)
42
43     @property
44     def type(self):
45         return self.schema.type
46
47     @property
48     def required(self):
49         return self.schema.required
50
51     @property
52     def description(self):
53         return self.schema.description
54
55     @property
56     def default(self):
57         return self.schema.default
58
59     @property
60     def constraints(self):
61         return self.schema.constraints
62
63     @property
64     def status(self):
65         return self.schema.status
66
67     def validate(self, value=None):
68         if value is not None:
69             self._validate_value(value)
70
71     def _validate_field(self):
72         for name in self.schema.schema:
73             if name not in self.INPUTFIELD:
74                 ExceptionCollector.appendException(
75                     UnknownFieldError(what='Input "%s"' % self.name,
76                                       field=name))
77
78     def validate_type(self, input_type):
79         if input_type not in Schema.PROPERTY_TYPES and \
80             input_type not in self.custom_defs:
81             ExceptionCollector.appendException(
82                 ValueError(_('Invalid type "%s".') % type))
83
84     # TODO(anyone) Need to test for any built-in datatype not just network
85     # that is, tosca.datatypes.* and not assume tosca.datatypes.network.*
86     # TODO(anyone) Add support for tosca.datatypes.Credential
87     def _validate_value(self, value):
88         tosca = EntityType.TOSCA_DEF
89         datatype = None
90         if self.type in tosca:
91             datatype = tosca[self.type]
92         elif EntityType.DATATYPE_NETWORK_PREFIX + self.type in tosca:
93             datatype = tosca[EntityType.DATATYPE_NETWORK_PREFIX + self.type]
94         elif self.type in self.custom_defs:
95             datatype = self.custom_defs
96
97         DataEntity.validate_datatype(self.type, value, None, datatype)
98
99
100 class Output(object):
101
102     OUTPUTFIELD = (DESCRIPTION, VALUE) = ('description', 'value')
103
104     def __init__(self, name, attrs):
105         self.name = name
106         self.attrs = attrs
107
108     @property
109     def description(self):
110         return self.attrs.get(self.DESCRIPTION)
111
112     @property
113     def value(self):
114         return self.attrs.get(self.VALUE)
115
116     def validate(self):
117         self._validate_field()
118
119     def _validate_field(self):
120         if not isinstance(self.attrs, dict):
121             ExceptionCollector.appendException(
122                 MissingRequiredFieldError(what='Output "%s"' % self.name,
123                                           required=self.VALUE))
124         if self.value is None:
125             ExceptionCollector.appendException(
126                 MissingRequiredFieldError(what='Output "%s"' % self.name,
127                                           required=self.VALUE))
128         for name in self.attrs:
129             if name not in self.OUTPUTFIELD:
130                 ExceptionCollector.appendException(
131                     UnknownFieldError(what='Output "%s"' % self.name,
132                                       field=name))