9222da0105adbadf502a83205ddfec12af965332
[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 package org.openecomp.sdc.be.components.distribution.engine;
21
22 import java.util.ArrayList;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Optional;
27 import java.util.stream.Collectors;
28 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
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         artifacts = group.getArtifactsUuid();
49         // Base Value is set from properties
50         setBaseValue(group);
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         artifacts = new ArrayList<>(group.getArtifactsUuid() != null ? group.getArtifactsUuid() : new LinkedList<>());
61         artifacts.addAll(group.getGroupInstanceArtifactsUuid() != null ? group.getGroupInstanceArtifactsUuid() : new LinkedList<>());
62         // Base Value is set from properties
63         setBaseValue(group);
64         if (group.convertToGroupInstancesProperties() != null) {
65             setProperties(group.convertToGroupInstancesProperties());
66         }
67     }
68
69     public static int compareByGroupName(VfModuleArtifactPayload art1, VfModuleArtifactPayload art2) {
70         Float thisCounter = Float.parseFloat(art1.vfModuleModelName.split(Constants.MODULE_NAME_DELIMITER)[1].replace(' ', '.'));
71         Float otherCounter = Float.parseFloat(art2.vfModuleModelName.split(Constants.MODULE_NAME_DELIMITER)[1].replace(' ', '.'));
72         return thisCounter.compareTo(otherCounter);
73     }
74
75     private void setBaseValue(GroupInstance group) {
76         if (group.convertToGroupInstancesProperties() != null) {
77             Optional<GroupInstanceProperty> findBaseProperty = group.convertToGroupInstancesProperties().stream()
78                 .filter(p -> p.getName().equals(Constants.IS_BASE)).findAny();
79             if (findBaseProperty.isPresent()) {
80                 isBase = Boolean.valueOf(findBaseProperty.get().getValue());
81             }
82         }
83     }
84
85     private void setBaseValue(GroupDefinition group) {
86         if (group.getProperties() != null) {
87             Optional<GroupProperty> findBaseProperty = group.convertToGroupProperties().stream().filter(p -> p.getName().equals(Constants.IS_BASE))
88                 .findAny();
89             if (findBaseProperty.isPresent()) {
90                 isBase = Boolean.valueOf(findBaseProperty.get().getValue());
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     public Map<String, Object> getProperties() {
104         return properties;
105     }
106
107     public void setProperties(List<GroupInstanceProperty> properties) {
108         this.properties = properties.stream().filter(p -> !p.getName().equals(Constants.IS_BASE))
109             .collect(Collectors.toMap(PropertyDataDefinition::getName, x -> x.getValue() == null ? "" : x.getValue()));
110     }
111 }