Fix checkstyle violations in sdc/jtosca
[sdc/sdc-tosca.git] / src / main / java / org / onap / sdc / toscaparser / api / utils / TOSCAVersionProperty.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.utils;
22
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
24
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 // test with functions/test_concat.yaml
29 public class TOSCAVersionProperty {
30
31     private String version;
32
33     private static final String VERSION_RE =
34             "^(?<gMajorVersion>([0-9][0-9]*))"
35                     + "(\\.(?<gMinorVersion>([0-9][0-9]*)))?"
36                     + "(\\.(?<gFixVersion>([0-9][0-9]*)))?"
37                     + "(\\.(?<gQualifier>([0-9A-Za-z]+)))?"
38                     + "(\\-(?<gBuildVersion>[0-9])*)?$";
39
40     private String minorVersion = null;
41     private String majorVersion = null;
42     private String fixVersion = null;
43     private String qualifier = null;
44     private String buildVersion = null;
45
46
47     public TOSCAVersionProperty(String version) {
48
49         if (version.equals("0") || version.equals("0.0") || version.equals("0.0.0")) {
50             return;
51         }
52
53         Pattern pattern = Pattern.compile(VERSION_RE);
54         Matcher matcher = pattern.matcher(version);
55         if (!matcher.find()) {
56             ThreadLocalsHolder.getCollector().appendValidationIssue(
57                     new JToscaValidationIssue(
58                             "JE252",
59                             "InvalidTOSCAVersionPropertyException: "
60                                     + "Value of TOSCA version property \"" + version + "\" is invalid"
61                     ));
62             return;
63         }
64         minorVersion = matcher.group("gMinorVersion");
65         majorVersion = matcher.group("gMajorVersion");
66         fixVersion = matcher.group("gFixVersion");
67         qualifier = validateQualifier(matcher.group("gQualifier"));
68         buildVersion = validateBuild(matcher.group("gBuildVersion"));
69         validateMajorVersion(majorVersion);
70
71         this.version = version;
72
73     }
74
75     private String validateMajorVersion(String value) {
76         // Validate major version
77
78         // Checks if only major version is provided and assumes
79         // minor version as 0.
80         // Eg: If version = 18, then it returns version = '18.0'
81
82         if (minorVersion == null && buildVersion == null && !value.equals("0")) {
83             //log.warning(_('Minor version assumed "0".'))
84             version = version + "0";
85         }
86         return value;
87     }
88
89     private String validateQualifier(String value) {
90         // Validate qualifier
91
92         // TOSCA version is invalid if a qualifier is present without the
93         // fix version or with all of major, minor and fix version 0s.
94
95         // For example, the following versions are invalid
96         //    18.0.abc
97         //    0.0.0.abc
98
99         if ((fixVersion == null && value != null) || (minorVersion.equals("0") && majorVersion.equals("0")
100                 && fixVersion.equals("0") && value != null)) {
101             ThreadLocalsHolder.getCollector().appendValidationIssue(
102                     new JToscaValidationIssue(
103                             "JE253",
104                             "InvalidTOSCAVersionPropertyException: Value of TOSCA version property \""
105                                     + version
106                                     + "\" is invalid"
107                     ));
108         }
109         return value;
110     }
111
112     private String validateBuild(String value) {
113         // Validate build version
114
115         // TOSCA version is invalid if build version is present without the qualifier.
116         // Eg: version = 18.0.0-1 is invalid.
117
118         if (qualifier == null && value != null) {
119             ThreadLocalsHolder.getCollector().appendValidationIssue(
120                     new JToscaValidationIssue(
121                             "JE254",
122                             "InvalidTOSCAVersionPropertyException: "
123                                     + "Value of TOSCA version property \"" + version + "\" is invalid"
124                     )
125             );
126         }
127         return value;
128     }
129
130     public Object getVersion() {
131         return version;
132     }
133
134 }
135
136 /*python
137
138 class TOSCAVersionProperty(object):
139
140     VERSION_RE = re.compile('^(?P<major_version>([0-9][0-9]*))'
141                             '(\.(?P<minor_version>([0-9][0-9]*)))?'
142                             '(\.(?P<fix_version>([0-9][0-9]*)))?'
143                             '(\.(?P<qualifier>([0-9A-Za-z]+)))?'
144                             '(\-(?P<build_version>[0-9])*)?$')
145
146     def __init__(self, version):
147         self.version = str(version)
148         match = self.VERSION_RE.match(self.version)
149         if not match:
150             ValidationIssueCollector.appendException(
151                 InvalidTOSCAVersionPropertyException(what=(self.version)))
152             return
153         ver = match.groupdict()
154         if self.version in ['0', '0.0', '0.0.0']:
155             log.warning(_('Version assumed as not provided'))
156             self.version = None
157         self.minor_version = ver['minor_version']
158         self.major_version = ver['major_version']
159         self.fix_version = ver['fix_version']
160         self.qualifier = self._validate_qualifier(ver['qualifier'])
161         self.build_version = self._validate_build(ver['build_version'])
162         self._validate_major_version(self.major_version)
163
164     def _validate_major_version(self, value):
165         """Validate major version
166
167         Checks if only major version is provided and assumes
168         minor version as 0.
169         Eg: If version = 18, then it returns version = '18.0'
170         """
171
172         if self.minor_version is None and self.build_version is None and \
173                 value != '0':
174             log.warning(_('Minor version assumed "0".'))
175             self.version = '.'.join([value, '0'])
176         return value
177
178     def _validate_qualifier(self, value):
179         """Validate qualifier
180
181            TOSCA version is invalid if a qualifier is present without the
182            fix version or with all of major, minor and fix version 0s.
183
184            For example, the following versions are invalid
185               18.0.abc
186               0.0.0.abc
187         """
188         if (self.fix_version is None and value) or \
189             (self.minor_version == self.major_version ==
190              self.fix_version == '0' and value):
191             ValidationIssueCollector.appendException(
192                 InvalidTOSCAVersionPropertyException(what=(self.version)))
193         return value
194
195     def _validate_build(self, value):
196         """Validate build version
197
198            TOSCA version is invalid if build version is present without the
199            qualifier.
200            Eg: version = 18.0.0-1 is invalid.
201         """
202         if not self.qualifier and value:
203             ValidationIssueCollector.appendException(
204                 InvalidTOSCAVersionPropertyException(what=(self.version)))
205         return value
206
207     def get_version(self):
208         return self.version
209 */