re base code
[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.google.common.collect.Lists;
25 import com.google.common.collect.Maps;
26 import org.apache.commons.collections.CollectionUtils;
27 import org.apache.commons.collections.MapUtils;
28 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.GroupDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.PropertiesOwner;
31 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
32
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.Map;
36
37 import static java.util.stream.Collectors.*;
38
39 public class GroupDefinition extends GroupDataDefinition implements PropertiesOwner{
40
41         private Map<String, List<CapabilityDefinition>> capabilities;
42         
43     public GroupDefinition() {
44         super();
45     }
46
47     public GroupDefinition(GroupDataDefinition other) {
48         super(other);
49     }
50
51     public GroupDefinition(GroupDefinition other) {
52         super(other);
53                 if(MapUtils.isNotEmpty(other.getCapabilities())) {
54                         this.setCapabilities(other.getCapabilities().entrySet()
55                                         .stream()
56                                         .collect(toMap(Map.Entry::getKey, e -> getCapabilitiesCopyList(e.getValue()))));
57                 }
58         }
59
60         public Map<String, List<CapabilityDefinition>> getCapabilities() {
61                 if(MapUtils.isEmpty(capabilities)) {
62                         capabilities = Maps.newHashMap();
63                 }
64                 return capabilities;
65         }
66
67         public void setCapabilities(Map<String, List<CapabilityDefinition>> capabilities) {
68                 this.capabilities = capabilities;
69     }
70
71     public List<GroupProperty> convertToGroupProperties() {
72         List<GroupProperty> properties = null;
73         List<PropertyDataDefinition> propList = super.getProperties();
74         if(propList != null && !propList .isEmpty()){
75                          properties = propList.stream().map(GroupProperty::new).collect(toList());
76         }
77         return properties;
78     }
79
80     public <T extends PropertyDataDefinition> void convertFromGroupProperties(List<T> properties) {
81         if(properties != null && !properties .isEmpty()){
82                         List<PropertyDataDefinition> propList = properties.stream().map(PropertyDataDefinition::new).collect(toList());
83             super.setProperties(propList);
84         }
85         }
86         
87     //returns true iff groupName has the same prefix has the resource
88     public boolean isSamePrefix(String resourceName){
89         return getName() != null  && getName().toLowerCase().trim().startsWith(resourceName.toLowerCase());
90     }
91
92     public void convertCapabilityDefinitions(Map<String, CapabilityDefinition> capabilities) {
93         if(MapUtils.isNotEmpty(capabilities)){
94             this.capabilities = capabilities.values().stream()
95                                                       .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()
124                                         .flatMap(Collection::stream)
125                                         .forEach(c -> c.updateEmptyCapabilityOwnerFields(getUniqueId(), getName(), CapabilityDataDefinition.OwnerType.GROUP));
126                 }
127         }
128
129 }