Add relationship of node template associated with tosca template in
[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,
31                   STATUS) = ('type', 'description', 'default',
32                              'constraints', 'required', 'status')
33
34     def __init__(self, name, schema_dict):
35         self.name = name
36         self.schema = Schema(name, schema_dict)
37
38     @property
39     def type(self):
40         return self.schema.type
41
42     @property
43     def description(self):
44         return self.schema.description
45
46     @property
47     def default(self):
48         return self.schema.default
49
50     @property
51     def constraints(self):
52         return self.schema.constraints
53
54     def validate(self, value=None):
55         self._validate_field()
56         self.validate_type(self.type)
57         if value is not None:
58             self._validate_value(value)
59
60     def _validate_field(self):
61         for name in self.schema.schema:
62             if name not in self.INPUTFIELD:
63                 ExceptionCollector.appendException(
64                     UnknownFieldError(what='Input "%s"' % self.name,
65                                       field=name))
66
67     def validate_type(self, input_type):
68         if input_type not in Schema.PROPERTY_TYPES:
69             ExceptionCollector.appendException(
70                 ValueError(_('Invalid type "%s".') % type))
71
72     # TODO(anyone) Need to test for any built-in datatype not just network
73     # that is, tosca.datatypes.* and not assume tosca.datatypes.network.*
74     # TODO(anyone) Add support for tosca.datatypes.Credential
75     def _validate_value(self, value):
76         tosca = EntityType.TOSCA_DEF
77         datatype = None
78         if self.type in tosca:
79             datatype = tosca[self.type]
80         elif EntityType.DATATYPE_NETWORK_PREFIX + self.type in tosca:
81             datatype = tosca[EntityType.DATATYPE_NETWORK_PREFIX + self.type]
82
83         DataEntity.validate_datatype(self.type, value, None, datatype)
84
85
86 class Output(object):
87
88     OUTPUTFIELD = (DESCRIPTION, VALUE) = ('description', 'value')
89
90     def __init__(self, name, attrs):
91         self.name = name
92         self.attrs = attrs
93
94     @property
95     def description(self):
96         return self.attrs.get(self.DESCRIPTION)
97
98     @property
99     def value(self):
100         return self.attrs.get(self.VALUE)
101
102     def validate(self):
103         self._validate_field()
104
105     def _validate_field(self):
106         if not isinstance(self.attrs, dict):
107             ExceptionCollector.appendException(
108                 MissingRequiredFieldError(what='Output "%s"' % self.name,
109                                           required=self.VALUE))
110         if self.value is None:
111             ExceptionCollector.appendException(
112                 MissingRequiredFieldError(what='Output "%s"' % self.name,
113                                           required=self.VALUE))
114         for name in self.attrs:
115             if name not in self.OUTPUTFIELD:
116                 ExceptionCollector.appendException(
117                     UnknownFieldError(what='Output "%s"' % self.name,
118                                       field=name))