Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / utils / GroupUtils.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.be.model.utils;
21
22 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
23 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaElementLifecycleOperation;
24 import org.openecomp.sdc.common.api.Constants;
25 import org.springframework.util.StringUtils;
26
27 public class GroupUtils {
28
29     public static boolean isVfModule(String type) {
30         return type.equals(Constants.DEFAULT_GROUP_VF_MODULE);
31     }
32
33     /**
34      * The version of the group/poloces is an integer. In order to support BC, we might get a version in a float format.
35      *
36      * @param promoteVersion
37      * @return
38      */
39     public static String updateVersion(PromoteVersionEnum promoteVersion, String currentVesion) {
40         if (StringUtils.isEmpty(currentVesion)) {
41             return "0.0";
42         }
43         String newVersion = currentVesion;
44         switch (promoteVersion) {
45             case MINOR:
46                 newVersion = GroupUtils.increaseMainorVersion(currentVesion);
47                 break;
48             case MAJOR:
49                 newVersion = GroupUtils.increaseMajorVersion(currentVesion);
50                 break;
51             default:
52                 break;
53         }
54         return newVersion;
55     }
56
57     private static String increaseMajorVersion(String version) {
58         String[] versionParts = version.split(ToscaElementLifecycleOperation.VERSION_DELIMITER_REGEXP);
59         Integer majorVersion = Integer.parseInt(versionParts[0]);
60         Integer mainorVersion = versionParts.length > 1 ? Integer.parseInt(versionParts[1]) : 0;
61         if (mainorVersion > 0 || majorVersion == 0) {
62             majorVersion++;
63         }
64         return String.valueOf(majorVersion);
65     }
66
67     private static String increaseMainorVersion(String version) {
68         String[] versionParts = version.split(ToscaElementLifecycleOperation.VERSION_DELIMITER_REGEXP);
69         Integer mainorVersion = versionParts.length > 1 ? Integer.parseInt(versionParts[1]) : 0;
70         mainorVersion++;
71         return versionParts[0] + ToscaElementLifecycleOperation.VERSION_DELIMITER + String.valueOf(mainorVersion);
72     }
73 }