Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / GroupDefinition.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 package org.openecomp.sdc.be.model;
21
22 import static java.util.stream.Collectors.groupingBy;
23 import static java.util.stream.Collectors.toList;
24 import static java.util.stream.Collectors.toMap;
25
26 import com.fasterxml.jackson.annotation.JsonIgnore;
27 import com.fasterxml.jackson.annotation.JsonInclude;
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Maps;
30 import java.util.Collection;
31 import java.util.List;
32 import java.util.Map;
33 import org.apache.commons.collections.CollectionUtils;
34 import org.apache.commons.collections.MapUtils;
35 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
36 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
37 import org.openecomp.sdc.be.datatypes.elements.PropertiesOwner;
38 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
39
40 public class GroupDefinition extends GroupDataDefinition implements PropertiesOwner {
41
42     @JsonInclude
43     private Map<String, List<CapabilityDefinition>> capabilities;
44
45     public GroupDefinition() {
46         super();
47     }
48
49     public GroupDefinition(GroupDataDefinition other) {
50         super(other);
51     }
52
53     public GroupDefinition(GroupDefinition other) {
54         super(other);
55         if (MapUtils.isNotEmpty(other.getCapabilities())) {
56             this.setCapabilities(
57                 other.getCapabilities().entrySet().stream().collect(toMap(Map.Entry::getKey, e -> getCapabilitiesCopyList(e.getValue()))));
58         }
59     }
60
61     public Map<String, List<CapabilityDefinition>> getCapabilities() {
62         if (MapUtils.isEmpty(capabilities)) {
63             capabilities = Maps.newHashMap();
64         }
65         return capabilities;
66     }
67
68     public void setCapabilities(Map<String, List<CapabilityDefinition>> capabilities) {
69         this.capabilities = capabilities;
70     }
71
72     public List<GroupProperty> convertToGroupProperties() {
73         List<GroupProperty> properties = null;
74         List<PropertyDataDefinition> propList = super.getProperties();
75         if (propList != null && !propList.isEmpty()) {
76             properties = propList.stream().map(GroupProperty::new).collect(toList());
77         }
78         return properties;
79     }
80
81     public <T extends PropertyDataDefinition> void convertFromGroupProperties(List<T> properties) {
82         if (properties != null && !properties.isEmpty()) {
83             List<PropertyDataDefinition> propList = properties.stream().map(PropertyDataDefinition::new).collect(toList());
84             super.setProperties(propList);
85         }
86     }
87
88     //returns true iff groupName has the same prefix has the resource
89     public boolean isSamePrefix(String resourceName) {
90         return getName() != null && getName().toLowerCase().trim().startsWith(resourceName.toLowerCase());
91     }
92
93     public void convertCapabilityDefinitions(Map<String, CapabilityDefinition> capabilities) {
94         if (MapUtils.isNotEmpty(capabilities)) {
95             this.capabilities = capabilities.values().stream().collect(groupingBy(CapabilityDefinition::getType));
96         }
97     }
98
99     @Override
100     public String getNormalizedName() {
101         return getName();
102     }
103
104     @JsonIgnore
105     private List<CapabilityDefinition> getCapabilitiesCopyList(List<CapabilityDefinition> capabilities) {
106         return Lists.newArrayList(capabilities.stream().map(CapabilityDefinition::new).collect(toList()));
107     }
108
109     public void updateCapabilitiesProperties(Map<String, Map<String, CapabilityDefinition>> capabilitiesInfo) {
110         if (MapUtils.isNotEmpty(capabilities) && MapUtils.isNotEmpty(capabilitiesInfo)) {
111             capabilities.entrySet().forEach(e -> updateCapabilitiesProperies(e.getValue(), capabilitiesInfo.get(e.getKey())));
112         }
113     }
114
115     private void updateCapabilitiesProperies(List<CapabilityDefinition> capabilities, Map<String, CapabilityDefinition> capabilitiesInfo) {
116         if (CollectionUtils.isNotEmpty(capabilities) && MapUtils.isNotEmpty(capabilitiesInfo)) {
117             capabilities.forEach(c -> c.updateCapabilityProperties(capabilitiesInfo.get(c.getName())));
118         }
119     }
120
121     public void updateEmptyCapabilitiesOwnerFields() {
122         if (MapUtils.isNotEmpty(this.capabilities)) {
123             this.capabilities.values().stream().flatMap(Collection::stream)
124                 .forEach(c -> c.updateEmptyCapabilityOwnerFields(getUniqueId(), getName(), CapabilityDataDefinition.OwnerType.GROUP));
125         }
126     }
127 }