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