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 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}";
37 public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
39 if (baseVersion == null) {
40 return INITIAL_VERSION;
43 String[] versionLevels = baseVersion.split("\\.");
44 if (versionLevels.length != 2) {
45 throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
48 int index = Integer.parseInt(versionLevels[0]);
50 versionLevels[0] = Integer.toString(index);
51 versionLevels[1] = "0";
53 return CommonMethods.arrayToSeparatedString(versionLevels, '.');
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)) {
62 optionalVersion = calculate(version.getName(), VersionCreationMethod.major);
63 if (!existingVersions.contains(optionalVersion)) {
64 optionalCreationMethods.add(VersionCreationMethod.major);
66 } catch (IllegalArgumentException iae) {
67 //not a valid creation method.
70 version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);