Catalog alignment
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / components / impl / group / GroupVersionUpdater.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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
21 package org.openecomp.sdc.be.components.impl.group;
22
23
24 import org.openecomp.sdc.be.components.impl.version.OnChangeVersionCommand;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.datatypes.enums.GroupTypeEnum;
27 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
28 import org.openecomp.sdc.be.impl.ComponentsUtils;
29 import org.openecomp.sdc.be.model.ArtifactDefinition;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.GroupDefinition;
32 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
33 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
34 import org.openecomp.sdc.be.model.utils.GroupUtils;
35 import org.openecomp.sdc.common.log.wrappers.Logger;
36
37 import java.util.List;
38 import java.util.Map;
39 import java.util.function.Consumer;
40 import java.util.stream.Collectors;
41
42 import static org.apache.commons.collections.CollectionUtils.isEmpty;
43
44
45 /**
46  * A Helper class which handles altering the version of a group
47  */
48 @org.springframework.stereotype.Component
49 public class GroupVersionUpdater implements OnChangeVersionCommand {
50     
51     private static final Logger log = Logger.getLogger(GroupVersionUpdater.class);
52     private final GroupsOperation groupsOperation;
53     private final ComponentsUtils componentsUtils;
54     
55
56     public GroupVersionUpdater(GroupsOperation groupsOperation, ComponentsUtils componentsUtils) {
57         this.groupsOperation = groupsOperation;
58         this.componentsUtils = componentsUtils;
59     
60     }
61     
62     
63     @Override
64     public ActionStatus onChangeVersion(Component container) {
65         log.debug("#onChangeVersion - replacing all group members for component instance");
66         Consumer<List<GroupDefinition>> replaceGroupMemberTask = (groups) -> increaseVersion(groups, container);
67         return updateGroupsVersion(container, replaceGroupMemberTask);
68     }
69
70     public void increaseVersion(List<GroupDefinition> groups, Component container) {
71         groups.forEach(group -> increaseMajorVersion(group, container));
72     }
73
74   
75     private void increaseMajorVersion(GroupDefinition group, Component container) {
76         String version = group.getVersion();
77         
78         String newVersion = GroupUtils.updateVersion(PromoteVersionEnum.MAJOR, group.getVersion());
79       
80         if(!version.equals(newVersion) ){
81             if(isGenerateGroupUUID(group, container)) {
82                 String groupUUID = UniqueIdBuilder.generateUUID();
83                 group.setGroupUUID(groupUUID);
84             }
85             group.setVersion(String.valueOf(newVersion));
86         }
87
88     }
89
90     private boolean isGenerateGroupUUID(GroupDefinition group, Component container) {
91         if(GroupTypeEnum.VF_MODULE.getGroupTypeName().equals(group.getType())){
92             List<String> artifactsUuid = group.getArtifactsUuid();
93             List<String> heatArtifactUniqueIDs = group.getArtifacts().stream().filter(a->!a.endsWith("env")).collect(Collectors.toList());
94             Map<String, ArtifactDefinition> deploymentArtifacts = container.getDeploymentArtifacts();
95             for (String heatArtifactUniqueID : heatArtifactUniqueIDs){
96                 ArtifactDefinition artifactDefinition = deploymentArtifacts.get(heatArtifactUniqueID.split("\\.", -1)[1]);
97                 if((artifactDefinition == null || artifactDefinition.isEmpty())
98                         && !artifactsUuid.contains(artifactDefinition.getArtifactUUID()) ){
99                     return true;
100                 }
101             }
102             return false;
103         }
104         return true;
105     }
106
107
108     private ActionStatus updateGroupsVersion(Component groupsContainer, Consumer<List<GroupDefinition>> updateGroupVersion) {
109         List<GroupDefinition> groups = groupsContainer.getGroups();
110         if (isEmpty(groups)) {
111             return ActionStatus.OK;
112         }
113         updateGroupVersion.accept(groups);
114         return updateGroups(groupsContainer.getUniqueId(), groups);
115     }  
116
117     
118     private ActionStatus updateGroups(String componentId, List<GroupDefinition> groupsToUpdate) {
119         log.debug("#updateGroups - updating {} groups for container {}", groupsToUpdate.size(), componentId);
120         return componentsUtils.convertFromStorageResponse(groupsOperation.updateGroupsOnComponent(componentId, groupsToUpdate));
121                
122     }
123
124 }
125