5fe48ff7ca345f15938cb24452a28ae4ccc68a27
[sdc.git] /
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.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.openecomp.sdc.versioning.VersionCalculator;
26 import org.openecomp.sdc.versioning.dao.types.Version;
27 import org.openecomp.sdc.versioning.dao.types.VersionStatus;
28 import org.openecomp.sdc.versioning.types.VersionCreationMethod;
29
30 import java.util.HashSet;
31 import java.util.Set;
32
33 public class VersionCalculatorImpl implements VersionCalculator {
34   private static final Logger LOGGER = LoggerFactory.getLogger(VersionCalculatorImpl.class);
35
36   private static final String INITIAL_VERSION = "1.0";
37   private static final String VERSION_STRING_VIOLATION_MSG =
38       "Version string must be in the format of: {integer}.{integer}";
39   private static final String INVALID_CREATION_METHOD_MSG = "Invalid creation method";
40
41   @Override
42   public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
43
44     if (baseVersion == null) {
45       return INITIAL_VERSION;
46     }
47
48     String[] versionLevels = baseVersion.split("\\.");
49     if (versionLevels.length != 2) {
50       throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
51     }
52
53     int index;
54     switch (creationMethod) {
55       case major:
56         index = Integer.parseInt(versionLevels[0]);
57         index++;
58         versionLevels[0] = Integer.toString(index);
59         versionLevels[1] = "0";
60         break;
61       case minor:
62         index = Integer.parseInt(versionLevels[1]);
63         index++;
64         versionLevels[1] = Integer.toString(index);
65         break;
66     }
67     return CommonMethods.arrayToSeparatedString(versionLevels, '.');
68   }
69
70   @Override
71   public void injectAdditionalInfo(Version version, Set<String> existingVersions) {
72     String optionalVersion;
73     Set<VersionCreationMethod> optionalCreationMethods = new HashSet<>();
74     if(version.getStatus().equals(VersionStatus.Certified)) {
75       for (VersionCreationMethod versionCreationMethod : VersionCreationMethod.values()) {
76         try {
77           optionalVersion = calculate(version.getName(), versionCreationMethod);
78           if (!existingVersions.contains(optionalVersion)) {
79             optionalCreationMethods.add(versionCreationMethod);
80           }
81         } catch (IllegalArgumentException iae) {
82           LOGGER.error("{}:{}", INVALID_CREATION_METHOD_MSG, versionCreationMethod.name(), iae);
83         }
84       }
85     }
86     version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);
87   }
88 }