Reformat catalog-model
[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(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         List<GroupInstanceProperty> groupInstancesProperties = null;
54         List<PropertyDataDefinition> propertiesList = super.getProperties();
55         if (propertiesList != null && !propertiesList.isEmpty()) {
56             groupInstancesProperties = propertiesList.stream().map(GroupInstanceProperty::new).collect(Collectors.toList());
57         }
58         return groupInstancesProperties;
59     }
60
61     /**
62      * Converts received list of GroupInstanceProperties to the list of PropertyDataDefinitions and sets It into the GroupInstanceDataDefinition as
63      * properties
64      *
65      * @param groupInstancesProperties
66      */
67     public void convertFromGroupInstancesProperties(List<GroupInstanceProperty> groupInstancesProperties) {
68         if (groupInstancesProperties != null && !groupInstancesProperties.isEmpty()) {
69             List<PropertyDataDefinition> propList = groupInstancesProperties.stream().map(PropertyDataDefinition::new).collect(Collectors.toList());
70             super.setProperties(propList);
71         }
72     }
73
74     private void removeArtifactsDuplicates() {
75         List<String> artifacts = getArtifacts();
76         Set<String> artifactsSet = new HashSet<>();
77         if (artifacts != null && !artifacts.isEmpty()) {
78             artifactsSet.addAll(artifacts);
79             artifacts.clear();
80             artifacts.addAll(artifactsSet);
81         }
82         List<String> giArtifacts = getGroupInstanceArtifacts();
83         Set<String> giArtifactsSet = new HashSet<>();
84         if (giArtifacts != null && !giArtifacts.isEmpty()) {
85             giArtifactsSet.addAll(giArtifacts);
86             giArtifacts.clear();
87             giArtifacts.addAll(giArtifactsSet);
88         }
89     }
90
91     private void clearArtifactsUuid() {
92         List<String> artifactsUuid = getArtifactsUuid();
93         if (CollectionUtils.isNotEmpty(artifactsUuid)) {
94             artifactsUuid.clear();
95         } else if (artifactsUuid == null) {
96             setArtifactsUuid(new ArrayList<>());
97         }
98         List<String> giartifactsUuid = this.getGroupInstanceArtifactsUuid();
99         if (CollectionUtils.isNotEmpty(giartifactsUuid)) {
100             giartifactsUuid.clear();
101         } else if (giartifactsUuid == null) {
102             setGroupInstanceArtifactsUuid(new ArrayList<>());
103         }
104     }
105
106     /**
107      * Aligns the list of artifacts UUIDs of group instance according to received deployment artifacts
108      *
109      * @param deploymentArtifacts
110      */
111     public void alignArtifactsUuid(Map<String, ArtifactDefinition> deploymentArtifacts) {
112         List<String> artifactIds = getArtifacts();
113         log.debug("the artifacts Id's are: {}, and the deployment artifacts Id's are: {}", artifactIds, deploymentArtifacts);
114         if (CollectionUtils.isNotEmpty(artifactIds) && deploymentArtifacts != null) {
115             removeArtifactsDuplicates();
116             clearArtifactsUuid();
117             List<String> artifactUuids = getArtifactsUuid();
118             List<String> giArtifactUuids = getGroupInstanceArtifactsUuid();
119             for (String artifactId : artifactIds) {
120                 String label = artifactId.substring(artifactId.lastIndexOf('.') + 1);
121                 ArtifactDefinition artifact = deploymentArtifacts.get(label);
122                 log.debug("current artifact id: {}, current artifact definition: {}", artifactId, artifact);
123                 ArtifactTypeEnum artifactType = ArtifactTypeEnum.parse(artifact.getArtifactType());
124                 if (artifactType != ArtifactTypeEnum.HEAT_ENV) {
125                     addArtifactsIdToCollection(artifactUuids, artifact);
126                 } else {
127                     addArtifactsIdToCollection(giArtifactUuids, artifact);
128                 }
129             }
130         }
131     }
132
133     private void addArtifactsIdToCollection(List<String> artifactUuids, ArtifactDefinition artifact) {
134         if (!artifactUuids.contains(artifact.getArtifactUUID()) && StringUtils.isNotEmpty(artifact.getArtifactUUID())) {
135             artifactUuids.add(artifact.getArtifactUUID());
136         }
137     }
138 }