Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / elements / PortSpec.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.sdc.toscaparser.api.elements;
22
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;
27
28 import java.util.LinkedHashMap;
29
30 public class PortSpec {
31     // Parent class for tosca.datatypes.network.PortSpec type
32
33     private static final String SHORTNAME = "PortSpec";
34     private static final String TYPE_URI = "tosca.datatypes.network." + SHORTNAME;
35
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";
41
42     private static final String PROPERTY_NAMES[] = {
43             PROTOCOL, SOURCE, SOURCE_RANGE,
44             TARGET, TARGET_RANGE
45     };
46
47     // todo(TBD) May want to make this a subclass of DataType
48     // and change init method to set PortSpec's properties
49     public PortSpec() {
50
51     }
52
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,
63                                              String propName,
64                                              LinkedHashMap<String, Object> custom_def) {
65
66         try {
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);
72
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",
78                         TYPE_URI)));
79             }
80             // Validate source value is in specified range
81             if (source != null && sourceRange != null) {
82                 ValidateUtils.validateValueInRange(source, sourceRange, SOURCE);
83             } else {
84                 DataEntity portdef = new DataEntity("PortDef", source, null, SOURCE);
85                 portdef.validate();
86             }
87             // Validate target value is in specified range
88             if (target != null && targetRange != null) {
89                 ValidateUtils.validateValueInRange(target, targetRange, SOURCE);
90             } else {
91                 DataEntity portdef = new DataEntity("PortDef", source, null, TARGET);
92                 portdef.validate();
93             }
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)));
98         }
99     }
100
101 }
102
103 /*python
104
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
109
110 log = logging.getLogger('tosca')
111
112
113 class PortSpec(object):
114     '''Parent class for tosca.datatypes.network.PortSpec type.'''
115
116     SHORTNAME = 'PortSpec'
117     TYPE_URI = 'tosca.datatypes.network.' + SHORTNAME
118
119     PROPERTY_NAMES = (
120         PROTOCOL, SOURCE, SOURCE_RANGE,
121         TARGET, TARGET_RANGE
122     ) = (
123         'protocol', 'source', 'source_range',
124         'target', 'target_range'
125     )
126
127     # TODO(TBD) May want to make this a subclass of DataType
128     # and change init method to set PortSpec's properties
129     def __init__(self):
130         pass
131
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.
141     @staticmethod
142     def validate_additional_req(properties, prop_name, custom_def=None, ):
143         try:
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)
148
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,
158                                                       PortSpec.SOURCE)
159             else:
160                 from toscaparser.dataentity import DataEntity
161                 portdef = DataEntity('PortDef', source, None, PortSpec.SOURCE)
162                 portdef.validate()
163             # Validate target value is in specified range
164             if target and target_range:
165                 validateutils.validate_value_in_range(target, target_range,
166                                                       PortSpec.TARGET)
167             else:
168                 from toscaparser.dataentity import DataEntity
169                 portdef = DataEntity('PortDef', source, None, PortSpec.TARGET)
170                 portdef.validate()
171         except Exception:
172             msg = _('"%(value)s" do not meet requirements '
173                     'for type "%(type)s".') \
174                 % {'value': properties, 'type': PortSpec.SHORTNAME}
175             ValidationIssueCollector.appendException(
176                 ValueError(msg))
177 */