baea0515386c15df9ecc94b191c17e346a48b5a5
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / jsonjanusgraph / datamodel / ToscaElement.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.jsonjanusgraph.datamodel;
22
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.UUID;
27 import lombok.Getter;
28 import lombok.Setter;
29 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
31 import org.openecomp.sdc.be.datatypes.elements.DataTypeDataDefinition;
32 import org.openecomp.sdc.be.datatypes.elements.ListCapabilityDataDefinition;
33 import org.openecomp.sdc.be.datatypes.elements.ListRequirementDataDefinition;
34 import org.openecomp.sdc.be.datatypes.elements.MapPropertiesDataDefinition;
35 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
38 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
39 import org.openecomp.sdc.be.model.LifecycleStateEnum;
40 import org.openecomp.sdc.be.model.category.CategoryDefinition;
41 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.NodeTypeOperation;
42 import org.openecomp.sdc.common.log.api.ILogConfiguration;
43 import org.slf4j.MDC;
44
45 @Getter
46 @Setter
47 public abstract class ToscaElement {
48
49     protected Map<String, Object> metadata;
50     protected List<CategoryDefinition> categories;
51     protected Map<String, ArtifactDataDefinition> toscaArtifacts;
52     protected ToscaElementTypeEnum toscaType;
53     private Map<String, ArtifactDataDefinition> artifacts;
54     private Map<String, ArtifactDataDefinition> deploymentArtifacts;
55     private Map<String, AdditionalInfoParameterDataDefinition> additionalInformation;
56     private Map<String, PropertyDataDefinition> properties;
57     private Map<String, ListCapabilityDataDefinition> capabilities;
58     private Map<String, MapPropertiesDataDefinition> capabilitiesProperties;
59     private Map<String, ListRequirementDataDefinition> requirements;
60     private Map<String, DataTypeDataDefinition> dataTypes;
61     // User
62     private String creatorUserId;
63     private String creatorFullName;
64     private String lastUpdaterUserId;
65     private String lastUpdaterFullName;
66
67     private Map<String, String> allVersions;
68     private String toscaVersion;
69
70     public ToscaElement(ToscaElementTypeEnum toscaType){
71         this.toscaType = toscaType;
72     }
73
74     // metadata properties
75     // ----------------------------
76     public Object getMetadataValue(JsonPresentationFields name) {
77         return getMetadataValueOrDefault(name, null);
78     }
79
80     public Object getMetadataValueOrDefault(JsonPresentationFields name, Object defaultVal) {
81         if (metadata != null) {
82             return metadata.getOrDefault(name.getPresentation(), defaultVal);
83         }
84         return null;
85     }
86
87     public void setMetadataValue(JsonPresentationFields name, Object value) {
88         setMetadataValue(name.getPresentation(), value);
89     }
90     
91     public void setMetadataValue(String name, Object value) {
92         if (metadata == null) {
93             metadata = new HashMap<>();
94         }
95         metadata.put(name, value);
96     }
97     // --------------------
98     public String getUUID() {
99         return (String) getMetadataValue(JsonPresentationFields.UUID);
100     }
101
102     public void setUUID(String uuid) {
103         setMetadataValue(JsonPresentationFields.UUID, uuid);
104     }
105
106     public String getVersion() {
107         return (String) getMetadataValue(JsonPresentationFields.VERSION);
108     }
109
110     public String getNormalizedName() {
111         return (String) getMetadataValue(JsonPresentationFields.NORMALIZED_NAME);
112     }
113
114     public void setNormalizedName(String normaliseComponentName) {
115         setMetadataValue(JsonPresentationFields.NORMALIZED_NAME, normaliseComponentName);
116     }
117
118     public String getName() {
119         return (String) getMetadataValue(JsonPresentationFields.NAME);
120     }
121
122     public String getSystemName() {
123         return (String) getMetadataValue(JsonPresentationFields.SYSTEM_NAME);
124     }
125     public void setSystemName(String systemName) {
126         setMetadataValue(JsonPresentationFields.SYSTEM_NAME, systemName);
127     }
128
129     public void setLifecycleState(LifecycleStateEnum state) {
130         if(state != null)
131             setMetadataValue(JsonPresentationFields.LIFECYCLE_STATE, state.name());
132     }
133
134     public LifecycleStateEnum getLifecycleState() {
135         return LifecycleStateEnum.findState( (String) getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
136     }
137
138     public Long getCreationDate() {
139         return (Long) getMetadataValue(JsonPresentationFields.CREATION_DATE);
140     }
141
142     public void setCreationDate(Long currentDate) {
143         setMetadataValue(JsonPresentationFields.CREATION_DATE, currentDate);
144     }
145
146     public void setLastUpdateDate(Long currentDate) {
147         setMetadataValue(JsonPresentationFields.LAST_UPDATE_DATE, currentDate);
148     }
149     public Long getLastUpdateDate() {
150         return (Long) getMetadataValue(JsonPresentationFields.LAST_UPDATE_DATE);
151     }
152
153     public String getUniqueId() {
154         return (String) getMetadataValue(JsonPresentationFields.UNIQUE_ID);
155     }
156     public void setUniqueId(String uniqueId) {
157          setMetadataValue(JsonPresentationFields.UNIQUE_ID, uniqueId);
158     }
159
160     public void setHighestVersion(Boolean isHighest) {
161          setMetadataValue(JsonPresentationFields.HIGHEST_VERSION, isHighest);
162
163     }
164     public Boolean isHighestVersion() {
165         return (Boolean) getMetadataValue(JsonPresentationFields.HIGHEST_VERSION);
166
167     }
168     public ResourceTypeEnum getResourceType() {
169         String resourceType = (String) getMetadataValue(JsonPresentationFields.RESOURCE_TYPE);
170         return resourceType != null ? ResourceTypeEnum.valueOf(resourceType) : null;
171     }
172
173     public void setResourceType(ResourceTypeEnum resourceType) {
174         if(resourceType != null)
175             setMetadataValue(JsonPresentationFields.RESOURCE_TYPE, resourceType.name());
176     }
177
178     public ComponentTypeEnum getComponentType() {
179         return ComponentTypeEnum.valueOf((String) getMetadataValue(JsonPresentationFields.COMPONENT_TYPE));
180     }
181
182     public void setComponentType(ComponentTypeEnum componentType) {
183         if(componentType != null)
184             setMetadataValue(JsonPresentationFields.COMPONENT_TYPE, componentType.name());
185     }
186
187     public String getDerivedFromGenericType(){
188         return (String) getMetadataValue(JsonPresentationFields.DERIVED_FROM_GENERIC_TYPE);
189     }
190
191     public void setDerivedFromGenericType(String derivedFromGenericType){
192         setMetadataValue(JsonPresentationFields.DERIVED_FROM_GENERIC_TYPE, derivedFromGenericType);
193     }
194
195     public String getDerivedFromGenericVersion(){
196         return (String) getMetadataValue(JsonPresentationFields.DERIVED_FROM_GENERIC_VERSION);
197     }
198
199     public void setDerivedFromGenericVersion(String derivedFromGenericVersion){
200         setMetadataValue(JsonPresentationFields.DERIVED_FROM_GENERIC_VERSION, derivedFromGenericVersion);
201     }
202
203     public Boolean isArchived() { return (Boolean) getMetadataValue(JsonPresentationFields.IS_ARCHIVED); }
204
205     public void setArchived(Boolean archived) { setMetadataValue(JsonPresentationFields.IS_ARCHIVED, archived); }
206
207     public Long getArchiveTime() {
208         Object archiveTime = getMetadataValue(JsonPresentationFields.ARCHIVE_TIME);
209         if (archiveTime instanceof Integer){
210             return new Long((Integer)getMetadataValue(JsonPresentationFields.ARCHIVE_TIME));
211         }
212         return (Long)archiveTime;
213     }
214
215     public void setArchiveTime(Long archiveTime) {
216         setMetadataValue(JsonPresentationFields.ARCHIVE_TIME, archiveTime);
217     }
218
219     public Boolean isVspArchived() {
220         return (Boolean) getMetadataValue(JsonPresentationFields.IS_VSP_ARCHIVED);
221     }
222
223     public void setVspArchived(Boolean vspArchived) {
224         setMetadataValue(JsonPresentationFields.IS_VSP_ARCHIVED, vspArchived);
225     }
226
227     public void generateUUID() {
228         String prevUUID = getUUID();
229         String version = getVersion();
230         if ((prevUUID == null && NodeTypeOperation.uuidNormativeNewVersion.matcher(version).matches()) || NodeTypeOperation.uuidNewVersion.matcher(version).matches()) {
231             UUID uuid = UUID.randomUUID();
232             setUUID(uuid.toString());
233             MDC.put(ILogConfiguration.MDC_SERVICE_INSTANCE_ID, uuid.toString());
234         }
235     }
236
237 }