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