Reformat catalog-model
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / CapabilityDefinition.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 com.google.common.collect.Lists;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.stream.Collectors;
27 import lombok.EqualsAndHashCode;
28 import lombok.Getter;
29 import lombok.NoArgsConstructor;
30 import lombok.Setter;
31 import lombok.ToString;
32 import org.apache.commons.collections.CollectionUtils;
33 import org.apache.commons.collections.MapUtils;
34 import org.apache.commons.lang3.StringUtils;
35 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
36
37 /**
38  * Specifies the capabilities that the Node Type exposes.
39  */
40 @Getter
41 @Setter
42 @NoArgsConstructor
43 @EqualsAndHashCode(callSuper = true)
44 @ToString
45 public class CapabilityDefinition extends CapabilityDataDefinition {
46
47     /**
48      * The properties field contains all properties defined for CapabilityDefinition
49      */
50     private List<ComponentInstanceProperty> properties;
51
52     public CapabilityDefinition(CapabilityDataDefinition cap) {
53         super(cap);
54     }
55
56     public CapabilityDefinition(CapabilityTypeDefinition other, String ownerName, String name, CapabilityDataDefinition.OwnerType ownerType) {
57         super(other);
58         this.setOwnerName(ownerName);
59         this.setOwnerType(ownerType);
60         this.setName(name);
61         this.setParentName(name);
62         if (MapUtils.isNotEmpty(other.getProperties())) {
63             this.properties = Lists
64                 .newArrayList(other.getProperties().values().stream().map(ComponentInstanceProperty::new).collect(Collectors.toList()));
65         }
66     }
67
68     public CapabilityDefinition(CapabilityDefinition other) {
69         super((CapabilityDefinition) other);
70         if (other.properties != null) {
71             this.properties = new ArrayList<>(other.properties.stream().map(ComponentInstanceProperty::new).collect(Collectors.toList()));
72         }
73     }
74
75     public void updateCapabilityProperties(CapabilityDefinition capabilityDefinition) {
76         if (CollectionUtils.isNotEmpty(getProperties()) && capabilityDefinition != null && CollectionUtils
77             .isNotEmpty(capabilityDefinition.getProperties())) {
78             Map<String, ComponentInstanceProperty> propertiesInfo = capabilityDefinition.getProperties().stream()
79                 .collect(Collectors.toMap(ComponentInstanceProperty::getName, p -> p));
80             getProperties().forEach(p -> p.updateCapabilityProperty(propertiesInfo.get(p.getName())));
81         }
82     }
83
84     public void updateEmptyCapabilityOwnerFields(String ownerId, String ownerName, OwnerType ownerType) {
85         if (StringUtils.isEmpty(getOwnerId())) {
86             setOwnerId(ownerId);
87             if (getPath() == null) {
88                 setPath(new ArrayList<>());
89             }
90             if (!getPath().contains(ownerId)) {
91                 getPath().add(ownerId);
92             }
93             setOwnerName(ownerName);
94             setOwnerTypeIfEmpty(ownerType);
95         }
96     }
97
98     private void setOwnerTypeIfEmpty(OwnerType ownerType) {
99         if (getOwnerType() == null) {
100             setOwnerType(ownerType);
101         }
102     }
103 }