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
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
20 log = logging.getLogger('tosca')
23 class PortSpec(object):
24 '''Parent class for tosca.datatypes.network.PortSpec type.'''
26 SHORTNAME = 'PortSpec'
27 TYPE_URI = 'tosca.datatypes.network.' + SHORTNAME
30 PROTOCOL, SOURCE, SOURCE_RANGE,
33 'protocol', 'source', 'source_range',
34 'target', 'target_range'
37 # TODO(TBD) May want to make this a subclass of DataType
38 # and change init method to set PortSpec's properties
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.
52 def validate_additional_req(properties, prop_name, custom_def=None, ):
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)
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,
70 from toscaparser.dataentity import DataEntity
71 portdef = DataEntity('PortDef', source, None, PortSpec.SOURCE)
73 # Validate target value is in specified range
74 if target and target_range:
75 validateutils.validate_value_in_range(target, target_range,
78 from toscaparser.dataentity import DataEntity
79 portdef = DataEntity('PortDef', source, None, PortSpec.TARGET)
82 msg = _('"%(value)s" do not meet requirements '
83 'for type "%(type)s".') \
84 % {'value': properties, 'type': PortSpec.SHORTNAME}
85 ExceptionCollector.appendException(