7acfa2d7db8391d2f07ea966c6ef2a3959cf4890
[sdc.git] /
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.components.distribution.engine;
22
23 import java.util.ArrayList;
24 import java.util.LinkedList;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29
30 import org.openecomp.sdc.be.model.GroupDefinition;
31 import org.openecomp.sdc.be.model.GroupInstance;
32 import org.openecomp.sdc.be.model.GroupInstanceProperty;
33 import org.openecomp.sdc.be.model.GroupProperty;
34 import org.openecomp.sdc.common.api.Constants;
35
36 public class VfModuleArtifactPayload {
37
38     private String vfModuleModelName, vfModuleModelInvariantUUID, vfModuleModelVersion, vfModuleModelUUID, vfModuleModelCustomizationUUID, vfModuleModelDescription;
39     private Boolean isBase;
40     private List<String> artifacts;
41     private Map< String, Object> properties;
42
43     public VfModuleArtifactPayload(GroupDefinition group) {
44         vfModuleModelName = group.getName();
45         vfModuleModelInvariantUUID = group.getInvariantUUID();
46         vfModuleModelVersion = group.getVersion();
47         vfModuleModelUUID = group.getGroupUUID();
48         vfModuleModelDescription = group.getDescription();
49
50         artifacts = group.getArtifactsUuid();
51         // Base Value is set from properties
52         setBaseValue(group);
53
54     }
55
56     public VfModuleArtifactPayload(GroupInstance group) {
57         vfModuleModelName = group.getGroupName();
58         vfModuleModelInvariantUUID = group.getInvariantUUID();
59         vfModuleModelVersion = group.getVersion();
60         vfModuleModelUUID = group.getGroupUUID();
61         vfModuleModelCustomizationUUID = group.getCustomizationUUID();
62         vfModuleModelDescription = group.getDescription();
63
64         artifacts = new ArrayList<>(group.getArtifactsUuid() != null ? group.getArtifactsUuid() : new LinkedList<>());
65         artifacts.addAll(group.getGroupInstanceArtifactsUuid() != null ? group.getGroupInstanceArtifactsUuid() : new LinkedList<>());
66
67         // Base Value is set from properties
68         setBaseValue(group);
69
70         if(group.convertToGroupInstancesProperties() != null)
71             setProperties(group.convertToGroupInstancesProperties());
72     }
73
74     private void setBaseValue(GroupInstance group) {
75         if (group.convertToGroupInstancesProperties() != null) {
76             Optional<GroupInstanceProperty> findBaseProperty = group.convertToGroupInstancesProperties().stream().filter(p -> p.getName().equals(Constants.IS_BASE)).findAny();
77             if (findBaseProperty.isPresent()) {
78                 isBase = Boolean.valueOf(findBaseProperty.get().getValue());
79             }
80
81         }
82     }
83     private void setBaseValue(GroupDefinition group) {
84         if (group.getProperties() != null) {
85             Optional<GroupProperty> findBaseProperty = group.convertToGroupProperties().stream().filter(p -> p.getName().equals(Constants.IS_BASE)).findAny();
86             if (findBaseProperty.isPresent()) {
87                 isBase = Boolean.valueOf(findBaseProperty.get().getValue());
88             }
89
90         }
91     }
92
93
94
95     public List<String> getArtifacts() {
96         return artifacts;
97     }
98
99     public void setArtifacts(List<String> artifacts) {
100         this.artifacts = artifacts;
101     }
102
103
104
105     public Map<String, Object> getProperties() {
106         return properties;
107     }
108
109
110     public void setProperties(List<GroupInstanceProperty> properties) {
111         this.properties  = properties.stream().filter(p -> !p.getName().equals(Constants.IS_BASE)).collect(
112                     Collectors.toMap(x -> x.getName(), x -> x.getValue() == null? "":x.getValue() ));
113     }
114
115     public static int compareByGroupName(VfModuleArtifactPayload art1, VfModuleArtifactPayload art2) {
116         Float thisCounter = Float.parseFloat(art1.vfModuleModelName.split(Constants.MODULE_NAME_DELIMITER)[1].replace(' ', '.'));
117         Float otherCounter = Float.parseFloat(art2.vfModuleModelName.split(Constants.MODULE_NAME_DELIMITER)[1].replace(' ', '.'));
118         return thisCounter.compareTo(otherCounter);
119     }
120 }