Reformat catalog-model
[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 package org.openecomp.sdc.be.model.jsonjanusgraph.datamodel;
21
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.UUID;
26 import lombok.Getter;
27 import lombok.Setter;
28 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
29 import org.openecomp.sdc.be.datatypes.elements.ArtifactDataDefinition;
30 import org.openecomp.sdc.be.datatypes.elements.AttributeDataDefinition;
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, AttributeDataDefinition> attributes;
58     private Map<String, ListCapabilityDataDefinition> capabilities;
59     private Map<String, MapPropertiesDataDefinition> capabilitiesProperties;
60     private Map<String, ListRequirementDataDefinition> requirements;
61     private Map<String, DataTypeDataDefinition> dataTypes;
62     // User
63     private String creatorUserId;
64     private String creatorFullName;
65     private String lastUpdaterUserId;
66     private String lastUpdaterFullName;
67     private Map<String, String> allVersions;
68     private String toscaVersion;
69
70     protected ToscaElement(ToscaElementTypeEnum toscaType) {
71         this.toscaType = toscaType;
72     }
73     // metadata properties
74
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     // --------------------
99     public String getUUID() {
100         return (String) getMetadataValue(JsonPresentationFields.UUID);
101     }
102
103     public void setUUID(String uuid) {
104         setMetadataValue(JsonPresentationFields.UUID, uuid);
105     }
106
107     public String getVersion() {
108         return (String) getMetadataValue(JsonPresentationFields.VERSION);
109     }
110
111     public String getNormalizedName() {
112         return (String) getMetadataValue(JsonPresentationFields.NORMALIZED_NAME);
113     }
114
115     public void setNormalizedName(String normaliseComponentName) {
116         setMetadataValue(JsonPresentationFields.NORMALIZED_NAME, normaliseComponentName);
117     }
118
119     public String getName() {
120         return (String) getMetadataValue(JsonPresentationFields.NAME);
121     }
122
123     public String getSystemName() {
124         return (String) getMetadataValue(JsonPresentationFields.SYSTEM_NAME);
125     }
126
127     public void setSystemName(String systemName) {
128         setMetadataValue(JsonPresentationFields.SYSTEM_NAME, systemName);
129     }
130
131     public LifecycleStateEnum getLifecycleState() {
132         return LifecycleStateEnum.findState((String) getMetadataValue(JsonPresentationFields.LIFECYCLE_STATE));
133     }
134
135     public void setLifecycleState(LifecycleStateEnum state) {
136         if (state != null) {
137             setMetadataValue(JsonPresentationFields.LIFECYCLE_STATE, state.name());
138         }
139     }
140
141     public Long getCreationDate() {
142         return (Long) getMetadataValue(JsonPresentationFields.CREATION_DATE);
143     }
144
145     public void setCreationDate(Long currentDate) {
146         setMetadataValue(JsonPresentationFields.CREATION_DATE, currentDate);
147     }
148
149     public Long getLastUpdateDate() {
150         return (Long) getMetadataValue(JsonPresentationFields.LAST_UPDATE_DATE);
151     }
152
153     public void setLastUpdateDate(Long currentDate) {
154         setMetadataValue(JsonPresentationFields.LAST_UPDATE_DATE, currentDate);
155     }
156
157     public String getUniqueId() {
158         return (String) getMetadataValue(JsonPresentationFields.UNIQUE_ID);
159     }
160
161     public void setUniqueId(String uniqueId) {
162         setMetadataValue(JsonPresentationFields.UNIQUE_ID, uniqueId);
163     }
164
165     public void setHighestVersion(Boolean isHighest) {
166         setMetadataValue(JsonPresentationFields.HIGHEST_VERSION, isHighest);
167     }
168
169     public Boolean isHighestVersion() {
170         return (Boolean) getMetadataValue(JsonPresentationFields.HIGHEST_VERSION);
171     }
172
173     public ResourceTypeEnum getResourceType() {
174         String resourceType = (String) getMetadataValue(JsonPresentationFields.RESOURCE_TYPE);
175         return resourceType != null ? ResourceTypeEnum.valueOf(resourceType) : null;
176     }
177
178     public void setResourceType(ResourceTypeEnum resourceType) {
179         if (resourceType != null) {
180             setMetadataValue(JsonPresentationFields.RESOURCE_TYPE, resourceType.name());
181         }
182     }
183
184     public ComponentTypeEnum getComponentType() {
185         return ComponentTypeEnum.valueOf((String) getMetadataValue(JsonPresentationFields.COMPONENT_TYPE));
186     }
187
188     public void setComponentType(ComponentTypeEnum componentType) {
189         if (componentType != null) {
190             setMetadataValue(JsonPresentationFields.COMPONENT_TYPE, componentType.name());
191         }
192     }
193
194     public String getDerivedFromGenericType() {
195         return (String) getMetadataValue(JsonPresentationFields.DERIVED_FROM_GENERIC_TYPE);
196     }
197
198     public void setDerivedFromGenericType(String derivedFromGenericType) {
199         setMetadataValue(JsonPresentationFields.DERIVED_FROM_GENERIC_TYPE, derivedFromGenericType);
200     }
201
202     public String getDerivedFromGenericVersion() {
203         return (String) getMetadataValue(JsonPresentationFields.DERIVED_FROM_GENERIC_VERSION);
204     }
205
206     public void setDerivedFromGenericVersion(String derivedFromGenericVersion) {
207         setMetadataValue(JsonPresentationFields.DERIVED_FROM_GENERIC_VERSION, derivedFromGenericVersion);
208     }
209
210     public Boolean isArchived() {
211         return (Boolean) getMetadataValue(JsonPresentationFields.IS_ARCHIVED);
212     }
213
214     public void setArchived(Boolean archived) {
215         setMetadataValue(JsonPresentationFields.IS_ARCHIVED, archived);
216     }
217
218     public Long getArchiveTime() {
219         Object archiveTime = getMetadataValue(JsonPresentationFields.ARCHIVE_TIME);
220         if (archiveTime instanceof Integer) {
221             return Long.valueOf((Integer) getMetadataValue(JsonPresentationFields.ARCHIVE_TIME));
222         }
223         return (Long) archiveTime;
224     }
225
226     public void setArchiveTime(Long archiveTime) {
227         setMetadataValue(JsonPresentationFields.ARCHIVE_TIME, archiveTime);
228     }
229
230     public Boolean isVspArchived() {
231         return (Boolean) getMetadataValue(JsonPresentationFields.IS_VSP_ARCHIVED);
232     }
233
234     public void setVspArchived(Boolean vspArchived) {
235         setMetadataValue(JsonPresentationFields.IS_VSP_ARCHIVED, vspArchived);
236     }
237
238     public void generateUUID() {
239         String prevUUID = getUUID();
240         String version = getVersion();
241         if ((prevUUID == null && NodeTypeOperation.uuidNormativeNewVersion.matcher(version).matches()) || NodeTypeOperation.uuidNewVersion
242             .matcher(version).matches()) {
243             UUID uuid = UUID.randomUUID();
244             setUUID(uuid.toString());
245             MDC.put(ILogConfiguration.MDC_SERVICE_INSTANCE_ID, uuid.toString());
246         }
247     }
248 }