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
5 # http://www.apache.org/licenses/LICENSE-2.0
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
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 _
25 log = logging.getLogger('tosca')
30 INPUTFIELD = (TYPE, DESCRIPTION, DEFAULT, CONSTRAINTS, REQUIRED,
31 STATUS) = ('type', 'description', 'default',
32 'constraints', 'required', 'status')
34 def __init__(self, name, schema_dict):
36 self.schema = Schema(name, schema_dict)
38 self._validate_field()
39 self.validate_type(self.type)
43 return self.schema.type
47 return self.schema.required
50 def description(self):
51 return self.schema.description
55 return self.schema.default
58 def constraints(self):
59 return self.schema.constraints
61 def validate(self, value=None):
63 self._validate_value(value)
65 def _validate_field(self):
66 for name in self.schema.schema:
67 if name not in self.INPUTFIELD:
68 ExceptionCollector.appendException(
69 UnknownFieldError(what='Input "%s"' % self.name,
72 def validate_type(self, input_type):
73 if input_type not in Schema.PROPERTY_TYPES:
74 ExceptionCollector.appendException(
75 ValueError(_('Invalid type "%s".') % type))
77 # TODO(anyone) Need to test for any built-in datatype not just network
78 # that is, tosca.datatypes.* and not assume tosca.datatypes.network.*
79 # TODO(anyone) Add support for tosca.datatypes.Credential
80 def _validate_value(self, value):
81 tosca = EntityType.TOSCA_DEF
83 if self.type in tosca:
84 datatype = tosca[self.type]
85 elif EntityType.DATATYPE_NETWORK_PREFIX + self.type in tosca:
86 datatype = tosca[EntityType.DATATYPE_NETWORK_PREFIX + self.type]
88 DataEntity.validate_datatype(self.type, value, None, datatype)
93 OUTPUTFIELD = (DESCRIPTION, VALUE) = ('description', 'value')
95 def __init__(self, name, attrs):
100 def description(self):
101 return self.attrs.get(self.DESCRIPTION)
105 return self.attrs.get(self.VALUE)
108 self._validate_field()
110 def _validate_field(self):
111 if not isinstance(self.attrs, dict):
112 ExceptionCollector.appendException(
113 MissingRequiredFieldError(what='Output "%s"' % self.name,
114 required=self.VALUE))
115 if self.value is None:
116 ExceptionCollector.appendException(
117 MissingRequiredFieldError(what='Output "%s"' % self.name,
118 required=self.VALUE))
119 for name in self.attrs:
120 if name not in self.OUTPUTFIELD:
121 ExceptionCollector.appendException(
122 UnknownFieldError(what='Output "%s"' % self.name,