Sync Integ to Master
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / Resource.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 java.io.Serializable;
24 import java.util.List;
25 import java.util.Map;
26
27 import org.openecomp.sdc.be.config.ConfigurationManager;
28 import org.openecomp.sdc.be.dao.utils.MapUtil;
29 import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.OriginTypeEnum;
32 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
33 import org.openecomp.sdc.be.datatypes.elements.WorkflowOperationDataDefinition;
34 import static java.util.stream.Collectors.groupingBy;
35 public class Resource extends Component implements Serializable {
36
37         private static final long serialVersionUID = -6811540567661368482L;
38         public static final String ROOT_RESOURCE = "tosca.nodes.Root";
39
40         public Resource() {
41                 super(new ResourceMetadataDefinition());
42                 this.getComponentMetadataDefinition().getMetadataDataDefinition().setComponentType(ComponentTypeEnum.RESOURCE);
43         }
44
45         public Resource(ComponentMetadataDefinition componentMetadataDefinition) {
46                 super(componentMetadataDefinition);
47                 this.getComponentMetadataDefinition().getMetadataDataDefinition().setComponentType(ComponentTypeEnum.RESOURCE);
48         }
49
50         private List<String> derivedFrom;
51
52         private List<String> derivedList;
53
54         private List<PropertyDefinition> properties;
55
56         private List<PropertyDefinition> attributes;
57
58         // Later
59         private Map<String, InterfaceDefinition> interfaces;
60
61         private List<String> defaultCapabilities;
62
63         private Map<String, WorkflowOperationDataDefinition> workflowOperations;
64
65 //      private List<AdditionalInformationDefinition> additionalInformation;
66
67         /**
68          * Please note that more than one "derivedFrom" resource is not currently
69          * supported by the app. The first list element is always addressed.
70          * 
71          * @return
72          */
73         public List<String> getDerivedFrom() {
74                 return derivedFrom;
75         }
76
77         public void setDerivedFrom(List<String> derivedFrom) {
78                 this.derivedFrom = derivedFrom;
79         }
80
81         /**
82          * The derivedList is a chain of derivedFrom. e.g. if resource C is derived
83          * from resource B that is derived from resource A - then A, B is the
84          * "DerivedList" of resource C
85          * 
86          * @return
87          */
88         public List<String> getDerivedList() {
89                 return derivedList;
90         }
91
92         public void setDerivedList(List<String> derivedList) {
93                 this.derivedList = derivedList;
94         }
95
96         public List<PropertyDefinition> getProperties() {
97                 return properties;
98         }
99
100         public void setProperties(List<PropertyDefinition> properties) {
101                 this.properties = properties;
102         }
103
104         public List<PropertyDefinition> getAttributes() {
105                 return attributes;
106         }
107
108         public void setAttributes(List<PropertyDefinition> attributes) {
109                 this.attributes = attributes;
110         }
111
112         public Map<String, InterfaceDefinition> getInterfaces() {
113                 return interfaces;
114         }
115
116         public void setInterfaces(Map<String, InterfaceDefinition> interfaces) {
117                 this.interfaces = interfaces;
118         }
119
120         public Boolean isAbstract() {
121                 return ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
122                                 .isAbstract();
123         }
124
125         public void setAbstract(Boolean isAbstract) {
126                 ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
127                                 .setAbstract(isAbstract);
128         }
129
130         public List<String> getDefaultCapabilities() {
131                 return defaultCapabilities;
132         }
133
134         public void setDefaultCapabilities(List<String> defaultCapabilities) {
135                 this.defaultCapabilities = defaultCapabilities;
136         }
137
138         public String getCost() {
139                 return ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
140                                 .getCost();
141         }
142
143         public void setCost(String cost) {
144                 ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition()).setCost(cost);
145                 ;
146         }
147
148         public String getLicenseType() {
149                 return ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
150                                 .getLicenseType();
151         }
152
153         public void setLicenseType(String licenseType) {
154                 ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
155                                 .setLicenseType(licenseType);
156         }
157
158         public Map<String, WorkflowOperationDataDefinition> getWorkflowOperations() {
159                 return workflowOperations;
160         }
161
162         public void setWorkflowOperations(Map<String, WorkflowOperationDataDefinition> workflowOperations) {
163                 this.workflowOperations = workflowOperations;
164         }
165
166         @Override
167         public int hashCode() {
168                 final int prime = 31;
169                 int result = 1;
170                 result = prime * result + super.hashCode();
171
172                 result = prime * result + ((attributes == null) ? 0 : attributes.hashCode());
173                 // result = prime * result + ((capabilities == null) ? 0 :
174                 // capabilities.hashCode());
175                 result = prime * result + ((defaultCapabilities == null) ? 0 : defaultCapabilities.hashCode());
176                 result = prime * result + ((derivedFrom == null) ? 0 : derivedFrom.hashCode());
177                 result = prime * result + ((interfaces == null) ? 0 : interfaces.hashCode());
178                 result = prime * result + ((properties == null) ? 0 : properties.hashCode());
179                 result = prime * result + ((derivedList == null) ? 0 : derivedList.hashCode());
180                 result = prime * result + ((workflowOperations == null) ? 0 : workflowOperations.hashCode());
181                 // result = prime * result + ((requirements == null) ? 0 :
182                 // requirements.hashCode());
183                 return result;
184         }
185
186         @Override
187         public boolean equals(Object obj) {
188                 if (this == obj)
189                         return true;
190                 if (obj == null)
191                         return false;
192                 if (getClass() != obj.getClass())
193                         return false;
194
195                 Resource other = (Resource) obj;
196                 if (attributes == null) {
197                         if (other.attributes != null)
198                                 return false;
199                 } else if (!attributes.equals(other.attributes))
200                         return false;
201                 if (defaultCapabilities == null) {
202                         if (other.defaultCapabilities != null)
203                                 return false;
204                 } else if (!defaultCapabilities.equals(other.defaultCapabilities))
205                         return false;
206                 if (derivedFrom == null) {
207                         if (other.derivedFrom != null)
208                                 return false;
209                 } else if (!derivedFrom.equals(other.derivedFrom))
210                         return false;
211                 if (derivedList == null) {
212                         if (other.derivedList != null)
213                                 return false;
214                 } else if (!derivedList.equals(other.derivedList))
215                         return false;
216                 if (interfaces == null) {
217                         if (other.interfaces != null)
218                                 return false;
219                 } else if (!interfaces.equals(other.interfaces))
220                         return false;
221                 if (properties == null) {
222                         if (other.properties != null)
223                                 return false;
224                 } else if (!properties.equals(other.properties))
225                         return false;
226                 if (workflowOperations == null) {
227                         if (other.workflowOperations != null)
228                                 return false;
229                 } else if (!workflowOperations.equals(other.workflowOperations))
230                         return false;
231                 return super.equals(obj);
232         }
233
234         @Override
235         public String toString() {
236                 return "Resource [derivedFrom=" + derivedFrom + ", properties=" + properties + ", attributes=" + attributes
237                                 + ", interfaces=" + interfaces
238                                 // + ", capabilities=" + capabilities + ", requirements=" +
239                                 // requirements
240                                 + ", defaultCapabilities=" + defaultCapabilities + ", additionalInformation=" + additionalInformation
241                         + ", workflowOperations=" + workflowOperations
242                                 + "Metadata [" + getComponentMetadataDefinition().getMetadataDataDefinition().toString() + "]";
243         }
244
245         public String getToscaResourceName() {
246                 return ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
247                                 .getToscaResourceName();
248         }
249
250         public void setToscaResourceName(String toscaResourceName) {
251                 ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
252                                 .setToscaResourceName(toscaResourceName);
253         }
254
255         public ResourceTypeEnum getResourceType() {
256                 return ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
257                                 .getResourceType();
258         }
259
260         public void setResourceType(ResourceTypeEnum resourceType) {
261                 ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
262                                 .setResourceType(resourceType);
263         }
264
265         public void setVendorName(String vendorName) {
266                 ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
267                                 .setVendorName(vendorName);
268         }
269
270         public void setVendorRelease(String vendorRelease) {
271                 ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
272                                 .setVendorRelease(vendorRelease);
273         }
274         
275         public void setResourceVendorModelNumber(String resourceVendorModelNumber){
276                 ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition()).
277                 setResourceVendorModelNumber(resourceVendorModelNumber);
278         }
279
280         public String getVendorName() {
281                 return ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
282                                 .getVendorName();
283         }
284
285         public String getVendorRelease() {
286                 return ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
287                                 .getVendorRelease();
288         }
289         
290         public String getResourceVendorModelNumber(){
291                 return ((ResourceMetadataDataDefinition) getComponentMetadataDefinition().getMetadataDataDefinition())
292                                 .getResourceVendorModelNumber();
293         }
294         
295         @Override
296         public String fetchGenericTypeToscaNameFromConfig(){
297                 String result = super.fetchGenericTypeToscaNameFromConfig();
298                 if(null == result)
299                         result = ConfigurationManager.getConfigurationManager().getConfiguration().getGenericAssetNodeTypes().get(ResourceTypeEnum.VFC.getValue());
300                 return result;
301         }
302         
303         @Override
304         public String assetType(){
305                 return this.getResourceType().name();
306         }
307         
308         @Override
309         public boolean shouldGenerateInputs(){
310                 //TODO add complex VFC condition when supported
311                 return !(this.getResourceType().isAtomicType());
312         }
313         
314         @Override
315         public boolean deriveFromGeneric(){     
316                 return this.shouldGenerateInputs() || (derivedFrom != null && derivedFrom.contains(fetchGenericTypeToscaNameFromConfig()));
317         }
318
319         public Map<String, List<RequirementCapabilityRelDef>> groupRelationsByInstanceName(Resource resource) {
320                 Map<String, List<RequirementCapabilityRelDef>> relationsByInstanceId = MapUtil.groupListBy(resource.getComponentInstancesRelations(), RequirementCapabilityRelDef::getFromNode);
321                 return MapUtil.convertMapKeys(relationsByInstanceId, (instId) -> getInstanceNameFromInstanceId(resource, instId));
322         }
323
324         private String getInstanceNameFromInstanceId(Resource resource, String instId) {
325                 return resource.getComponentInstanceById(instId).get().getName();
326         }
327
328
329 }