2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.model;
22 import static java.util.stream.Collectors.groupingBy;
23 import static java.util.stream.Collectors.toList;
24 import static java.util.stream.Collectors.toMap;
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;
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;
40 public class GroupDefinition extends GroupDataDefinition implements PropertiesOwner {
43 private Map<String, List<CapabilityDefinition>> capabilities;
45 public GroupDefinition() {
49 public GroupDefinition(GroupDataDefinition other) {
53 public GroupDefinition(GroupDefinition other) {
55 if (MapUtils.isNotEmpty(other.getCapabilities())) {
57 other.getCapabilities().entrySet().stream().collect(toMap(Map.Entry::getKey, e -> getCapabilitiesCopyList(e.getValue()))));
61 public Map<String, List<CapabilityDefinition>> getCapabilities() {
62 if (MapUtils.isEmpty(capabilities)) {
63 capabilities = Maps.newHashMap();
68 public void setCapabilities(Map<String, List<CapabilityDefinition>> capabilities) {
69 this.capabilities = capabilities;
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());
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);
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());
93 public void convertCapabilityDefinitions(Map<String, CapabilityDefinition> capabilities) {
94 if (MapUtils.isNotEmpty(capabilities)) {
95 this.capabilities = capabilities.values().stream().collect(groupingBy(CapabilityDefinition::getType));
100 public String getNormalizedName() {
105 private List<CapabilityDefinition> getCapabilitiesCopyList(List<CapabilityDefinition> capabilities) {
106 return Lists.newArrayList(capabilities.stream().map(CapabilityDefinition::new).collect(toList()));
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())));
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())));
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));