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