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 public void injectAdditionalInfo(Version version, Set<String> existingVersions) {
59 String optionalVersion;
60 Set<VersionCreationMethod> optionalCreationMethods = new HashSet<>();
61 if(version.getStatus().equals(VersionStatus.Certified)) {
62 for (VersionCreationMethod versionCreationMethod : VersionCreationMethod.values()) {
64 optionalVersion = calculate(version.getName(), versionCreationMethod);
65 if (!existingVersions.contains(optionalVersion)) {
66 optionalCreationMethods.add(versionCreationMethod);
68 } catch (IllegalArgumentException iae) {
69 //not a valid creation method.
73 version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);