a74243eb7e09e1ffa519084a9cc0f558eaed9e5a
[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 org.apache.commons.collections.CollectionUtils;
25 import org.apache.commons.collections.MapUtils;
26 import org.apache.commons.collections.SetUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.openecomp.sdc.be.datatypes.elements.CapabilityDataDefinition;
29
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.stream.Collectors;
34 /**
35  * Specifies the capabilities that the Node Type exposes.
36  */
37 public class CapabilityDefinition extends CapabilityDataDefinition {
38
39     /**
40      * The properties field contains all properties defined for
41      * CapabilityDefinition
42      */
43     private List<ComponentInstanceProperty> properties;
44
45
46     public CapabilityDefinition() {
47         super();
48     }
49
50     public CapabilityDefinition(CapabilityDataDefinition cap) {
51         super(cap);
52     }
53
54         public CapabilityDefinition(CapabilityTypeDefinition other, String ownerName, String name, CapabilityDataDefinition.OwnerType ownerType) {
55                 super(other);
56         this.setOwnerName(ownerName);
57         this.setOwnerType(ownerType);
58         this.setName(name);
59         this.setParentName(name);
60                 if (MapUtils.isNotEmpty(other.getProperties())) {
61                         this.properties = Lists.newArrayList(other.getProperties().values().stream().map(ComponentInstanceProperty::new).collect(Collectors.toList()));
62                 }
63         }
64
65     public CapabilityDefinition(CapabilityDefinition other) {
66         super((CapabilityDefinition)other);
67
68         if (other.properties != null) {
69                         this.properties = new ArrayList<>(other.properties.stream().map(ComponentInstanceProperty::new).collect(Collectors.toList()));
70         }
71     }
72
73     @Override
74     public int hashCode() {
75         final int prime = 31;
76         int result = super.hashCode();
77         result = prime * result + ((properties == null) ? 0 : properties.hashCode());
78         return result;
79     }
80
81     @Override
82     public boolean equals(Object obj) {
83         if (this == obj)
84             return true;
85         if (!super.equals(obj))
86             return false;
87         if (getClass() != obj.getClass())
88             return false;
89         CapabilityDefinition other = (CapabilityDefinition) obj;
90         if (properties == null) {
91             if (other.properties != null)
92                 return false;
93         } else if (!SetUtils.isEqualSet(properties, other.getProperties()))
94             return false;
95         return true;
96     }
97
98     @Override
99     public String toString() {
100         return "CapabilityDefinition [properties=" + properties + "]";
101     }
102
103     public List<ComponentInstanceProperty> getProperties() {
104         return properties;
105     }
106
107     public void setProperties(List<ComponentInstanceProperty> properties) {
108         this.properties = properties;
109     }
110
111         public void updateCapabilityProperties(CapabilityDefinition capabilityDefinition) {
112                 if(CollectionUtils.isNotEmpty(getProperties()) && capabilityDefinition!= null && CollectionUtils.isNotEmpty(capabilityDefinition.getProperties())){
113                         Map<String, ComponentInstanceProperty> propertiesInfo = capabilityDefinition.getProperties()
114                                         .stream()
115                                         .collect(Collectors.toMap(ComponentInstanceProperty::getName, p->p));
116                         getProperties().forEach(p->p.updateCapabilityProperty(propertiesInfo.get(p.getName())));
117                 }
118         }
119
120     public void updateEmptyCapabilityOwnerFields(String ownerId, String ownerName, OwnerType ownerType) {
121         if (StringUtils.isEmpty(getOwnerId())){
122             setOwnerId(ownerId);
123             if(getPath() == null){
124                 setPath(new ArrayList<>());
125             }
126             if(!getPath().contains(ownerId)){
127                 getPath().add(ownerId);
128             }
129             setOwnerName(ownerName);
130             setOwnerTypeIfEmpty(ownerType);
131         }
132     }
133
134     private void setOwnerTypeIfEmpty(OwnerType ownerType) {
135         if(getOwnerType() == null){
136             setOwnerType(ownerType);
137         }
138     }
139 }