Added oparent to sdc main
[sdc.git] / openecomp-be / lib / openecomp-sdc-versioning-lib / openecomp-sdc-versioning-core / src / main / java / org / openecomp / sdc / versioning / impl / MajorVersionCalculatorImpl.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 package org.openecomp.sdc.versioning.impl;
21
22 import org.openecomp.core.utilities.CommonMethods;
23 import org.openecomp.sdc.versioning.VersionCalculator;
24 import org.openecomp.sdc.versioning.dao.types.Version;
25 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
26 import org.openecomp.sdc.versioning.types.VersionCreationMethod;
27
28 import java.util.HashSet;
29 import java.util.Set;
30
31 public class MajorVersionCalculatorImpl implements VersionCalculator {
32   private static final String INITIAL_VERSION = "1.0";
33   private static final String VERSION_STRING_VIOLATION_MSG =
34       "Version string must be in the format of: {integer}.{integer}";
35
36   @Override
37   public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
38
39     if (baseVersion == null) {
40       return INITIAL_VERSION;
41     }
42
43     String[] versionLevels = baseVersion.split("\\.");
44     if (versionLevels.length != 2) {
45       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
46     }
47
48     int index = Integer.parseInt(versionLevels[0]);
49     index++;
50     versionLevels[0] = Integer.toString(index);
51     versionLevels[1] = "0";
52
53     return CommonMethods.arrayToSeparatedString(versionLevels, '.');
54   }
55
56   @Override
57   public void injectAdditionalInfo(Version version, Set<String> existingVersions) {
58     String optionalVersion;
59     Set<VersionCreationMethod> optionalCreationMethods = new HashSet<>();
60     if(version.getStatus().equals(VersionStatus.Certified)) {
61       try {
62         optionalVersion = calculate(version.getName(), VersionCreationMethod.major);
63         if (!existingVersions.contains(optionalVersion)) {
64           optionalCreationMethods.add(VersionCreationMethod.major);
65         }
66       } catch (IllegalArgumentException iae) {
67         //not a valid creation method.
68       }
69     }
70     version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);
71   }
72 }