2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.versioning.impl;
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;
28 import java.util.HashSet;
31 public class VersionCalculatorImpl implements VersionCalculator {
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.";
41 private static final String VERSION_CALCULATION_ERROR_MSG =
42 "Version calculation error.";
44 private static final String INVALID_CREATION_METHOD_MSG = "Invalid creation method-";
48 public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
50 if (baseVersion == null) {
51 return INITIAL_VERSION;
54 String[] versionLevels = baseVersion.split("\\.");
55 if (versionLevels.length != 2) {
56 throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
60 switch (creationMethod) {
62 index = Integer.parseInt(versionLevels[0]);
64 versionLevels[0] = Integer.toString(index);
65 versionLevels[1] = "0";
68 index = Integer.parseInt(versionLevels[1]);
70 versionLevels[1] = Integer.toString(index);
73 return CommonMethods.arrayToSeparatedString(versionLevels, '.');
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()) {
83 optionalVersion = calculate(version.getName(), versionCreationMethod);
84 if (!existingVersions.contains(optionalVersion)) {
85 optionalCreationMethods.add(versionCreationMethod);
87 } catch (IllegalArgumentException iae) {
88 //not a valid creation method.
92 version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);