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, STATUS,
31 ENTRY_SCHEMA) = ('type', 'description', 'default',
32 'constraints', 'required', 'status',
35 def __init__(self, name, schema_dict, custom_defs=None):
37 self.schema = Schema(name, schema_dict)
38 self.custom_defs = custom_defs or {}
40 self._validate_field()
41 self.validate_type(self.type)
45 return self.schema.type
49 return self.schema.required
52 def description(self):
53 return self.schema.description
57 return self.schema.default
60 def constraints(self):
61 return self.schema.constraints
65 return self.schema.status
67 def validate(self, value=None):
69 self._validate_value(value)
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,
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))
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
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[self.type]
97 DataEntity.validate_datatype(self.type, value, None, datatype)
100 class Output(object):
102 OUTPUTFIELD = (DESCRIPTION, VALUE) = ('description', 'value')
104 def __init__(self, name, attrs):
109 def description(self):
110 return self.attrs.get(self.DESCRIPTION)
114 return self.attrs.get(self.VALUE)
117 self._validate_field()
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,