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 / VersionCalculatorImpl.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 VersionCalculatorImpl implements VersionCalculator {
32
33   private static final String INITIAL_VERSION = "1.0";
34   private static final String VERSION_STRING_VIOLATION_MSG =
35       "Version string must be in the format of: {integer}.{integer}";
36   private static final String PARENT_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_TOP_LEVEL =
37       "Creation of parent level version on top level version is invalid.";
38   private static final String SUB_LEVEL_VERSION_CANNOT_BE_CREATED_FROM_LOWEST_LEVEL =
39       "Creation of parent level version on top level version is invalid.";
40
41   private static final String VERSION_CALCULATION_ERROR_MSG =
42       "Version calculation error.";
43
44   private static final String INVALID_CREATION_METHOD_MSG = "Invalid creation method-";
45
46
47   @Override
48   public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
49
50     if (baseVersion == null) {
51       return INITIAL_VERSION;
52     }
53
54     String[] versionLevels = baseVersion.split("\\.");
55     if (versionLevels.length != 2) {
56       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
57     }
58
59     int index;
60     switch (creationMethod) {
61       case major:
62         index = Integer.parseInt(versionLevels[0]);
63         index++;
64         versionLevels[0] = Integer.toString(index);
65         versionLevels[1] = "0";
66         break;
67       case minor:
68         index = Integer.parseInt(versionLevels[1]);
69         index++;
70         versionLevels[1] = Integer.toString(index);
71         break;
72     }
73     return CommonMethods.arrayToSeparatedString(versionLevels, '.');
74   }
75
76   @Override
77   public void injectAdditionalInfo(Version version, Set<String> existingVersions) {
78     String optionalVersion;
79     Set<VersionCreationMethod> optionalCreationMethods = new HashSet<>();
80     if(version.getStatus().equals(VersionStatus.Certified)) {
81       for (VersionCreationMethod versionCreationMethod : VersionCreationMethod.values()) {
82         try {
83           optionalVersion = calculate(version.getName(), versionCreationMethod);
84           if (!existingVersions.contains(optionalVersion)) {
85             optionalCreationMethods.add(versionCreationMethod);
86           }
87         } catch (IllegalArgumentException iae) {
88           //not a valid creation method.
89         }
90       }
91     }
92     version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);
93
94   }
95
96 }