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