1 package org.openecomp.sdc.versioning.impl;
3 import org.openecomp.core.utilities.CommonMethods;
4 import org.openecomp.sdc.versioning.VersionCalculator;
5 import org.openecomp.sdc.versioning.dao.types.Version;
6 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
7 import org.openecomp.sdc.versioning.types.VersionCreationMethod;
9 import java.util.HashSet;
12 public class VersionCalculatorImpl implements VersionCalculator {
14 private static final String INITIAL_VERSION = "1.0";
15 private static final String VERSION_STRING_VIOLATION_MSG =
16 "Version string must be in the format of: {integer}.{integer}";
17 private static final String PARENT_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_TOP_LEVEL =
18 "Creation of parent level version on top level version is invalid.";
19 private static final String SUB_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_LOWEST_LEVEL =
20 "Creation of parent level version on top level version is invalid.";
22 private static final String VERSION_CALCULATION_ERROR_MSG =
23 "Version calculation error.";
25 private static final String INVALID_CREATION_METHOD_MSG = "Invalid creation method-";
29 public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
31 if (baseVersion == null) {
32 return INITIAL_VERSION;
35 String[] versionLevels = baseVersion.split("\\.");
36 if (versionLevels.length != 2) {
37 throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
41 switch (creationMethod) {
43 index = Integer.parseInt(versionLevels[0]);
45 versionLevels[0] = Integer.toString(index);
46 versionLevels[1] = "0";
49 index = Integer.parseInt(versionLevels[1]);
51 versionLevels[1] = Integer.toString(index);
54 return CommonMethods.arrayToSeparatedString(versionLevels, '.');
58 // version calculator when there are no version restrictions
60 public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
62 return calculate(baseVersion,creationMethod,2);
65 private String calculate(String baseVersion, VersionCreationMethod creationMethod,int
67 if (baseVersion == null) {
68 return INITIAL_VERSION;
71 String[] versionLevels = baseVersion.split("\\.");
72 if (versionLevels.length > maxVersionLevels) {
73 throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
77 int versionLevel = calcVersionLevel(versionLevels);
78 if (versionLevel == -1) {
79 throw new IllegalArgumentException(
80 VERSION_STRING_VIOLATION_MSG + " given version:" + baseVersion);
82 int requiredVersionLevelIncrease;
83 switch (creationMethod) {
85 if (versionLevel == 0) {
86 throw new IllegalArgumentException(
87 PARENT_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_TOP_LEVEL + " version:" + baseVersion);
90 requiredVersionLevelIncrease = versionLevel - 1;
91 versionLevels[versionLevel] = "0";
92 index = Integer.getInteger(versionLevels[requiredVersionLevelIncrease]);
94 versionLevels[requiredVersionLevelIncrease] = Integer.toString(index);
97 requiredVersionLevelIncrease = versionLevel;
98 index = Integer.valueOf(versionLevels[requiredVersionLevelIncrease]);
100 versionLevels[requiredVersionLevelIncrease] = Integer.toString(index);
103 if (versionLevel == versionLevels.length - 1) {
104 throw new IllegalArgumentException(
105 SUB_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_LOWEST_LEVEL + " version:" + baseVersion);
107 requiredVersionLevelIncrease = versionLevel + 1;
108 if(requiredVersionLevelIncrease>maxVersionLevels){
109 throw new IllegalArgumentException(INVALID_CREATION_METHOD_MSG+" max " +
110 "levels:"+maxVersionLevels + "requested level:"+requiredVersionLevelIncrease);
112 String newVersion = baseVersion + ".1";
113 versionLevels = newVersion.split("\\.");
117 throw new IllegalArgumentException(VERSION_CALCULATION_ERROR_MSG + " base " +
118 "version:" + baseVersion + " creation method:" + creationMethod);
122 return CommonMethods.arrayToSeparatedString(versionLevels, '.');
127 public void injectAdditionalInfo(Version version, Set<String> existingVersions) {
128 String optionalVersion;
129 Set<VersionCreationMethod> optionalCreationMethods = new HashSet<>();
130 if(version.getStatus().equals(VersionStatus.Certified)) {
131 for (VersionCreationMethod versionCreationMethod : VersionCreationMethod.values()) {
133 optionalVersion = calculate(version.getName(), versionCreationMethod);
134 if (!existingVersions.contains(optionalVersion)) {
135 optionalCreationMethods.add(versionCreationMethod);
137 } catch (IllegalArgumentException iae) {
138 //not a valid creation method.
142 version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);
147 private int calcVersionLevel(String[] versionLevels) {
148 for (int i = versionLevels.length - 1; i >= 0; i--) {
149 if (!versionLevels[i].equals("0")) {