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