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.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;
30 import java.util.HashSet;
33 public class VersionCalculatorImpl implements VersionCalculator {
34 private static final Logger LOGGER = LoggerFactory.getLogger(VersionCalculatorImpl.class);
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";
42 public String calculate(String baseVersion, VersionCreationMethod creationMethod) {
44 if (baseVersion == null) {
45 return INITIAL_VERSION;
48 String[] versionLevels = baseVersion.split("\\.");
49 if (versionLevels.length != 2) {
50 throw new IllegalArgumentException(VERSION_STRING_VIOLATION_MSG);
54 switch (creationMethod) {
56 index = Integer.parseInt(versionLevels[0]);
58 versionLevels[0] = Integer.toString(index);
59 versionLevels[1] = "0";
62 index = Integer.parseInt(versionLevels[1]);
64 versionLevels[1] = Integer.toString(index);
67 return CommonMethods.arrayToSeparatedString(versionLevels, '.');
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()) {
77 optionalVersion = calculate(version.getName(), versionCreationMethod);
78 if (!existingVersions.contains(optionalVersion)) {
79 optionalCreationMethods.add(versionCreationMethod);
81 } catch (IllegalArgumentException iae) {
82 LOGGER.error("{}:{}", INVALID_CREATION_METHOD_MSG, versionCreationMethod.name(), iae);
86 version.getAdditionalInfo().put("OptionalCreationMethods", optionalCreationMethods);