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