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