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