Merge "Support version of tosca_simple_yaml_1_1"
[parser.git] / tosca2heat / tosca-parser / toscaparser / elements / portspectype.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 import logging
14
15 from toscaparser.common.exception import ExceptionCollector
16 from toscaparser.common.exception import InvalidTypeAdditionalRequirementsError
17 from toscaparser.utils.gettextutils import _
18 import toscaparser.utils.validateutils as validateutils
19
20 log = logging.getLogger('tosca')
21
22
23 class PortSpec(object):
24     '''Parent class for tosca.datatypes.network.PortSpec type.'''
25
26     SHORTNAME = 'PortSpec'
27     TYPE_URI = 'tosca.datatypes.network.' + SHORTNAME
28
29     PROPERTY_NAMES = (
30         PROTOCOL, SOURCE, SOURCE_RANGE,
31         TARGET, TARGET_RANGE
32     ) = (
33         'protocol', 'source', 'source_range',
34         'target', 'target_range'
35     )
36
37     # TODO(TBD) May want to make this a subclass of DataType
38     # and change init method to set PortSpec's properties
39     def __init__(self):
40         pass
41
42     # The following additional requirements MUST be tested:
43     # 1) A valid PortSpec MUST have at least one of the following properties:
44     #   target, target_range, source or source_range.
45     # 2) A valid PortSpec MUST have a value for the source property that
46     #    is within the numeric range specified by the property source_range
47     #    when source_range is specified.
48     # 3) A valid PortSpec MUST have a value for the target property that is
49     #    within the numeric range specified by the property target_range
50     #    when target_range is specified.
51     @staticmethod
52     def validate_additional_req(properties, prop_name, custom_def=None, ):
53         try:
54             source = properties.get(PortSpec.SOURCE)
55             source_range = properties.get(PortSpec.SOURCE_RANGE)
56             target = properties.get(PortSpec.TARGET)
57             target_range = properties.get(PortSpec.TARGET_RANGE)
58
59             # verify one of the specified values is set
60             if source is None and source_range is None and \
61                     target is None and target_range is None:
62                 ExceptionCollector.appendException(
63                     InvalidTypeAdditionalRequirementsError(
64                         type=PortSpec.TYPE_URI))
65             # Validate source value is in specified range
66             if source and source_range:
67                 validateutils.validate_value_in_range(source, source_range,
68                                                       PortSpec.SOURCE)
69             else:
70                 from toscaparser.dataentity import DataEntity
71                 portdef = DataEntity('PortDef', source, None, PortSpec.SOURCE)
72                 portdef.validate()
73             # Validate target value is in specified range
74             if target and target_range:
75                 validateutils.validate_value_in_range(target, target_range,
76                                                       PortSpec.TARGET)
77             else:
78                 from toscaparser.dataentity import DataEntity
79                 portdef = DataEntity('PortDef', source, None, PortSpec.TARGET)
80                 portdef.validate()
81         except Exception:
82             msg = _('"%(value)s" do not meet requirements '
83                     'for type "%(type)s".') \
84                 % {'value': properties, 'type': PortSpec.SHORTNAME}
85             ExceptionCollector.appendException(
86                 ValueError(msg))