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