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