df52cdd0762d27ce514e5f5f4d2bf5bdfc129300
[sdc.git] /
1 package org.openecomp.sdc.versioning.impl;
2
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;
8
9 import java.util.HashSet;
10 import java.util.Set;
11
12 public class VersionCalculatorImpl implements VersionCalculator {
13
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.";
21
22   private static final String VERSION_CALCULATION_ERROR_MSG =
23       "Version calculation error.";
24
25   private static final String INVALID_CREATION_METHOD_MSG = "Invalid creation method-";
26
27
28   @Override
29   public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
30
31     if (baseVersion == null) {
32       return INITIAL_VERSION;
33     }
34
35     String[] versionLevels = baseVersion.split("\\.");
36     if (versionLevels.length != 2) {
37       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
38     }
39
40     int index;
41     switch (creationMethod) {
42       case major:
43         index = Integer.parseInt(versionLevels[0]);
44         index++;
45         versionLevels[0] = Integer.toString(index);
46         versionLevels[1] = "0";
47         break;
48       case minor:
49         index = Integer.parseInt(versionLevels[1]);
50         index++;
51         versionLevels[1] = Integer.toString(index);
52         break;
53     }
54     return CommonMethods.arrayToSeparatedString(versionLevels, '.');
55   }
56
57   @Override
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()) {
63         try {
64           optionalVersion = calculate(version.getName(), versionCreationMethod);
65           if (!existingVersions.contains(optionalVersion)) {
66             optionalCreationMethods.add(versionCreationMethod);
67           }
68         } catch (IllegalArgumentException iae) {
69           //not a valid creation method.
70         }
71       }
72     }
73     version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);
74
75   }
76
77 }