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