Release version 1.13.7
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / GroupInstance.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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;
21
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.lang3.StringUtils;
30 import org.openecomp.sdc.be.datatypes.elements.GroupInstanceDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
32 import org.openecomp.sdc.common.api.ArtifactTypeEnum;
33 import org.openecomp.sdc.common.log.wrappers.Logger;
34
35 public class GroupInstance extends GroupInstanceDataDefinition {
36
37     private static final Logger log = Logger.getLogger(GroupInstance.class);
38
39     public GroupInstance() {
40         super();
41     }
42
43     public GroupInstance(final GroupInstanceDataDefinition r) {
44         super(r);
45     }
46
47     /**
48      * Converts contained list of PropertyDataDefinitions to list of GroupInstanceProperties
49      *
50      * @return
51      */
52     public List<GroupInstanceProperty> convertToGroupInstancesProperties() {
53         final List<PropertyDataDefinition> propertiesList = super.getProperties();
54         if (CollectionUtils.isNotEmpty(propertiesList)) {
55             return propertiesList.stream().map(GroupInstanceProperty::new).collect(Collectors.toList());
56         }
57         return null;
58     }
59
60     /**
61      * Converts received list of GroupInstanceProperties to the list of PropertyDataDefinitions and sets It into the GroupInstanceDataDefinition as
62      * properties
63      *
64      * @param groupInstancesProperties
65      */
66     public void convertFromGroupInstancesProperties(final List<GroupInstanceProperty> groupInstancesProperties) {
67         if (groupInstancesProperties != null && !groupInstancesProperties.isEmpty()) {
68             super.setProperties(groupInstancesProperties.stream().map(PropertyDataDefinition::new).collect(Collectors.toList()));
69         }
70     }
71
72     private void removeArtifactsDuplicates() {
73         final List<String> artifacts = getArtifacts();
74         final Set<String> artifactsSet = new HashSet<>();
75         if (artifacts != null && !artifacts.isEmpty()) {
76             artifactsSet.addAll(artifacts);
77             artifacts.clear();
78             artifacts.addAll(artifactsSet);
79         }
80         final List<String> giArtifacts = getGroupInstanceArtifacts();
81         final Set<String> giArtifactsSet = new HashSet<>();
82         if (giArtifacts != null && !giArtifacts.isEmpty()) {
83             giArtifactsSet.addAll(giArtifacts);
84             giArtifacts.clear();
85             giArtifacts.addAll(giArtifactsSet);
86         }
87     }
88
89     private void clearArtifactsUuid() {
90         final List<String> artifactsUuid = getArtifactsUuid();
91         if (CollectionUtils.isNotEmpty(artifactsUuid)) {
92             artifactsUuid.clear();
93         } else if (artifactsUuid == null) {
94             setArtifactsUuid(new ArrayList<>());
95         }
96         final List<String> groupInstanceArtifactsUuid = this.getGroupInstanceArtifactsUuid();
97         if (CollectionUtils.isNotEmpty(groupInstanceArtifactsUuid)) {
98             groupInstanceArtifactsUuid.clear();
99         } else if (groupInstanceArtifactsUuid == null) {
100             setGroupInstanceArtifactsUuid(new ArrayList<>());
101         }
102     }
103
104     /**
105      * Aligns the list of artifacts UUIDs of group instance according to received deployment artifacts
106      *
107      * @param deploymentArtifacts
108      */
109     public void alignArtifactsUuid(final Map<String, ArtifactDefinition> deploymentArtifacts) {
110         final List<String> artifactIds = getArtifacts();
111         log.debug("the artifacts Id's are: {}, and the deployment artifacts Id's are: {}", artifactIds, deploymentArtifacts);
112         if (CollectionUtils.isNotEmpty(artifactIds) && deploymentArtifacts != null) {
113             removeArtifactsDuplicates();
114             clearArtifactsUuid();
115             final List<String> artifactUuids = getArtifactsUuid();
116             final List<String> giArtifactUuids = getGroupInstanceArtifactsUuid();
117             for (final String artifactId : artifactIds) {
118                 final var label = artifactId.substring(artifactId.lastIndexOf('.') + 1);
119                 final ArtifactDefinition artifact = deploymentArtifacts.get(label);
120                 log.debug("current artifact id: {}, current artifact definition: {}", artifactId, artifact);
121                 final var artifactType = ArtifactTypeEnum.parse(artifact.getArtifactType());
122                 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
123                     addArtifactsIdToCollection(artifactUuids, artifact);
124                 } else {
125                     addArtifactsIdToCollection(giArtifactUuids, artifact);
126                 }
127             }
128         }
129     }
130
131     private void addArtifactsIdToCollection(final List<String> artifactUuids, final ArtifactDefinition artifact) {
132         if (StringUtils.isNotEmpty(artifact.getArtifactUUID()) && !artifactUuids.contains(artifact.getArtifactUUID())) {
133             artifactUuids.add(artifact.getArtifactUUID());
134         }
135     }
136 }