2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.sdc.toscaparser.api.elements;
23 import org.onap.sdc.toscaparser.api.DataEntity;
24 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
25 import org.onap.sdc.toscaparser.api.utils.ThreadLocalsHolder;
26 import org.onap.sdc.toscaparser.api.utils.ValidateUtils;
28 import java.util.LinkedHashMap;
30 public class PortSpec {
31 // Parent class for tosca.datatypes.network.PortSpec type
33 private static final String SHORTNAME = "PortSpec";
34 private static final String TYPE_URI = "tosca.datatypes.network." + SHORTNAME;
36 private static final String PROTOCOL = "protocol";
37 private static final String SOURCE = "source";
38 private static final String SOURCE_RANGE = "source_range";
39 private static final String TARGET = "target";
40 private static final String TARGET_RANGE = "target_range";
42 private static final String PROPERTY_NAMES[] = {
43 PROTOCOL, SOURCE, SOURCE_RANGE,
47 // todo(TBD) May want to make this a subclass of DataType
48 // and change init method to set PortSpec's properties
53 // The following additional requirements MUST be tested:
54 // 1) A valid PortSpec MUST have at least one of the following properties:
55 // target, target_range, source or source_range.
56 // 2) A valid PortSpec MUST have a value for the source property that
57 // is within the numeric range specified by the property source_range
58 // when source_range is specified.
59 // 3) A valid PortSpec MUST have a value for the target property that is
60 // within the numeric range specified by the property target_range
61 // when target_range is specified.
62 public static void validateAdditionalReq(Object _properties,
64 LinkedHashMap<String, Object> custom_def) {
67 LinkedHashMap<String, Object> properties = (LinkedHashMap<String, Object>) _properties;
68 Object source = properties.get(PortSpec.SOURCE);
69 Object sourceRange = properties.get(PortSpec.SOURCE_RANGE);
70 Object target = properties.get(PortSpec.TARGET);
71 Object targetRange = properties.get(PortSpec.TARGET_RANGE);
73 // verify one of the specified values is set
74 if (source == null && sourceRange == null &&
75 target == null && targetRange == null) {
76 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE129", String.format(
77 "InvalidTypeAdditionalRequirementsError: Additional requirements for type \"%s\" not met",
80 // Validate source value is in specified range
81 if (source != null && sourceRange != null) {
82 ValidateUtils.validateValueInRange(source, sourceRange, SOURCE);
84 DataEntity portdef = new DataEntity("PortDef", source, null, SOURCE);
87 // Validate target value is in specified range
88 if (target != null && targetRange != null) {
89 ValidateUtils.validateValueInRange(target, targetRange, SOURCE);
91 DataEntity portdef = new DataEntity("PortDef", source, null, TARGET);
94 } catch (Exception e) {
95 ThreadLocalsHolder.getCollector().appendValidationIssue(new JToscaValidationIssue("JE130", String.format(
96 "ValueError: \"%s\" do not meet requirements for type \"%s\"",
97 _properties.toString(), SHORTNAME)));
105 from toscaparser.common.exception import ValidationIssueCollector
106 from toscaparser.common.exception import InvalidTypeAdditionalRequirementsError
107 from toscaparser.utils.gettextutils import _
108 import org.openecomp.sdc.toscaparser.api.utils.validateutils as validateutils
110 log = logging.getLogger('tosca')
113 class PortSpec(object):
114 '''Parent class for tosca.datatypes.network.PortSpec type.'''
116 SHORTNAME = 'PortSpec'
117 TYPE_URI = 'tosca.datatypes.network.' + SHORTNAME
120 PROTOCOL, SOURCE, SOURCE_RANGE,
123 'protocol', 'source', 'source_range',
124 'target', 'target_range'
127 # TODO(TBD) May want to make this a subclass of DataType
128 # and change init method to set PortSpec's properties
132 # The following additional requirements MUST be tested:
133 # 1) A valid PortSpec MUST have at least one of the following properties:
134 # target, target_range, source or source_range.
135 # 2) A valid PortSpec MUST have a value for the source property that
136 # is within the numeric range specified by the property source_range
137 # when source_range is specified.
138 # 3) A valid PortSpec MUST have a value for the target property that is
139 # within the numeric range specified by the property target_range
140 # when target_range is specified.
142 def validate_additional_req(properties, prop_name, custom_def=None, ):
144 source = properties.get(PortSpec.SOURCE)
145 source_range = properties.get(PortSpec.SOURCE_RANGE)
146 target = properties.get(PortSpec.TARGET)
147 target_range = properties.get(PortSpec.TARGET_RANGE)
149 # verify one of the specified values is set
150 if source is None and source_range is None and \
151 target is None and target_range is None:
152 ValidationIssueCollector.appendException(
153 InvalidTypeAdditionalRequirementsError(
154 type=PortSpec.TYPE_URI))
155 # Validate source value is in specified range
156 if source and source_range:
157 validateutils.validate_value_in_range(source, source_range,
160 from toscaparser.dataentity import DataEntity
161 portdef = DataEntity('PortDef', source, None, PortSpec.SOURCE)
163 # Validate target value is in specified range
164 if target and target_range:
165 validateutils.validate_value_in_range(target, target_range,
168 from toscaparser.dataentity import DataEntity
169 portdef = DataEntity('PortDef', source, None, PortSpec.TARGET)
172 msg = _('"%(value)s" do not meet requirements '
173 'for type "%(type)s".') \
174 % {'value': properties, 'type': PortSpec.SHORTNAME}
175 ValidationIssueCollector.appendException(