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.utils;
23 import org.onap.sdc.toscaparser.api.common.JToscaValidationIssue;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
28 // test with functions/test_concat.yaml
29 public class TOSCAVersionProperty {
31 private String version;
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])*)?$";
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;
47 public TOSCAVersionProperty(String version) {
49 if (version.equals("0") || version.equals("0.0") || version.equals("0.0.0")) {
53 Pattern pattern = Pattern.compile(VERSION_RE);
54 Matcher matcher = pattern.matcher(version);
55 if (!matcher.find()) {
56 ThreadLocalsHolder.getCollector().appendValidationIssue(
57 new JToscaValidationIssue(
59 "InvalidTOSCAVersionPropertyException: "
60 + "Value of TOSCA version property \"" + version + "\" is invalid"
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);
71 this.version = version;
75 private String validateMajorVersion(String value) {
76 // Validate major version
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'
82 if (minorVersion == null && buildVersion == null && !value.equals("0")) {
83 //log.warning(_('Minor version assumed "0".'))
84 version = version + "0";
89 private String validateQualifier(String value) {
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.
95 // For example, the following versions are invalid
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(
104 "InvalidTOSCAVersionPropertyException: Value of TOSCA version property \""
112 private String validateBuild(String value) {
113 // Validate build version
115 // TOSCA version is invalid if build version is present without the qualifier.
116 // Eg: version = 18.0.0-1 is invalid.
118 if (qualifier == null && value != null) {
119 ThreadLocalsHolder.getCollector().appendValidationIssue(
120 new JToscaValidationIssue(
122 "InvalidTOSCAVersionPropertyException: "
123 + "Value of TOSCA version property \"" + version + "\" is invalid"
130 public Object getVersion() {
138 class TOSCAVersionProperty(object):
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])*)?$')
146 def __init__(self, version):
147 self.version = str(version)
148 match = self.VERSION_RE.match(self.version)
150 ValidationIssueCollector.appendException(
151 InvalidTOSCAVersionPropertyException(what=(self.version)))
153 ver = match.groupdict()
154 if self.version in ['0', '0.0', '0.0.0']:
155 log.warning(_('Version assumed as not provided'))
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)
164 def _validate_major_version(self, value):
165 """Validate major version
167 Checks if only major version is provided and assumes
169 Eg: If version = 18, then it returns version = '18.0'
172 if self.minor_version is None and self.build_version is None and \
174 log.warning(_('Minor version assumed "0".'))
175 self.version = '.'.join([value, '0'])
178 def _validate_qualifier(self, value):
179 """Validate qualifier
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.
184 For example, the following versions are invalid
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)))
195 def _validate_build(self, value):
196 """Validate build version
198 TOSCA version is invalid if build version is present without the
200 Eg: version = 18.0.0-1 is invalid.
202 if not self.qualifier and value:
203 ValidationIssueCollector.appendException(
204 InvalidTOSCAVersionPropertyException(what=(self.version)))
207 def get_version(self):