summaryrefslogtreecommitdiffstats
path: root/catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/group/GroupVersionUpdater.java
blob: 8e9ba7a88b0773921a4a51a66f6a3bc9525c7aff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.impl.group;

import java.util.Collections;
import java.util.stream.Collectors;
import org.openecomp.sdc.be.components.impl.version.OnChangeVersionCommand;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.datatypes.enums.GroupTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.ArtifactDefinition;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.GroupDefinition;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.GroupsOperation;
import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
import org.openecomp.sdc.be.model.utils.GroupUtils;
import org.openecomp.sdc.common.log.wrappers.Logger;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import static org.apache.commons.collections.CollectionUtils.isEmpty;


/**
 * A Helper class which handles altering the version of a group
 */
@org.springframework.stereotype.Component
public class GroupVersionUpdater implements OnChangeVersionCommand {
    
    private static final Logger log = Logger.getLogger(GroupVersionUpdater.class);
    private final GroupsOperation groupsOperation;
    private final ComponentsUtils componentsUtils;
    

    public GroupVersionUpdater(GroupsOperation groupsOperation, ComponentsUtils componentsUtils) {
        this.groupsOperation = groupsOperation;
        this.componentsUtils = componentsUtils;
    
    }
    
    
    @Override
    public ActionStatus onChangeVersion(Component container) {
        log.debug("#onChangeVersion - replacing all group members for component instance");
        Consumer<List<GroupDefinition>> replaceGroupMemberTask = (groups) -> increaseVersion(groups, container);
        return updateGroupsVersion(container, replaceGroupMemberTask);
    }

    public void increaseVersion(List<GroupDefinition> groups, Component container) {
        groups.forEach(group -> increaseMajorVersion(group, container));
    }

  
    private void increaseMajorVersion(GroupDefinition group, Component container) {
        String version = group.getVersion();
        
        String newVersion = GroupUtils.updateVersion(PromoteVersionEnum.MAJOR, group.getVersion());
      
        if(!version.equals(newVersion) ){
            if(isGenerateGroupUUID(group, container)) {
                String groupUUID = UniqueIdBuilder.generateUUID();
                group.setGroupUUID(groupUUID);
            }
            group.setVersion(String.valueOf(newVersion));
        }
    }

    static <T> List<T> emptyIfNull(final List<T> list) {
        return (list == null? Collections.emptyList() : list);
    }

    static <T, U> Map<T, U> emptyIfNull(final Map<T, U> list) {
        return (list == null? Collections.emptyMap() : list);
    }

    static boolean isGenerateGroupUUID(GroupDefinition group, Component container) {
        if (!GroupTypeEnum.VF_MODULE.getGroupTypeName().equals(group.getType())) {
            return true;
        } else {
            List<String> artifactsUuid = emptyIfNull(group.getArtifactsUuid());
            Map<String, ArtifactDefinition> deploymentArtifacts = emptyIfNull(container.getDeploymentArtifacts());
            List<String> heatArtifactUniqueIDs = emptyIfNull(group.getArtifacts()).stream().filter(a -> !a.endsWith("env")).collect(
                Collectors.toList());

            for (String heatArtifactUniqueID : heatArtifactUniqueIDs) {
                ArtifactDefinition artifactDefinition = deploymentArtifacts
                    .get(heatArtifactUniqueID.split("\\.", -1)[1]);

                if ((artifactDefinition == null
                    || artifactDefinition.isEmpty())
                    || !artifactsUuid.contains(artifactDefinition.getArtifactUUID())) {
                    return true;
                }
            }
            return false;
        }
    }

    private ActionStatus updateGroupsVersion(Component groupsContainer, Consumer<List<GroupDefinition>> updateGroupVersion) {
        List<GroupDefinition> groups = groupsContainer.getGroups();
        if (isEmpty(groups)) {
            return ActionStatus.OK;
        }
        updateGroupVersion.accept(groups);
        return updateGroups(groupsContainer.getUniqueId(), groups);
    }  

    
    private ActionStatus updateGroups(String componentId, List<GroupDefinition> groupsToUpdate) {
        log.debug("#updateGroups - updating {} groups for container {}", groupsToUpdate.size(), componentId);
        return componentsUtils.convertFromStorageResponse(groupsOperation.updateGroupsOnComponent(componentId, groupsToUpdate));
               
    }

}