re base code
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / jsongraph / GraphVertex.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.dao.jsongraph;
22
23 import com.thinkaurelius.titan.core.TitanVertex;
24 import org.apache.commons.collections.MapUtils;
25 import org.apache.commons.lang.StringUtils;
26 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
27 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
28 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
29 import org.openecomp.sdc.be.datatypes.enums.InstantiationTypes;
30 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
31 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
32
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Map.Entry;
36
37 public class GraphVertex {
38         private String uniqueId;
39
40         private TitanVertex vertex;
41         private VertexTypeEnum label;
42
43         private Map<String, ? extends ToscaDataDefinition> json;
44         private Map<String, Object> metadataJson;
45         private Map<GraphPropertyEnum, Object> metadataProperties;
46
47         public GraphVertex() {
48
49         }
50
51         public GraphVertex(VertexTypeEnum label) {
52                 super();
53                 this.label = label;
54         }
55
56         public String getUniqueId() {
57                 return uniqueId;
58         }
59
60         public void setUniqueId(String uniqueId) {
61                 this.uniqueId = uniqueId;
62                 addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
63         }
64
65         public Map<String, ? extends ToscaDataDefinition> getJson() {
66                 return json;
67         }
68
69         public void setJson(Map<String, ? extends ToscaDataDefinition> json) {
70                 this.json = json;
71         }
72
73         public TitanVertex getVertex() {
74                 return vertex;
75         }
76
77         public void setVertex(TitanVertex vertex) {
78                 this.vertex = vertex;
79         }
80
81         public VertexTypeEnum getLabel() {
82                 return label;
83         }
84
85         public void setLabel(VertexTypeEnum label) {
86                 this.label = label;
87         }
88
89         public ComponentTypeEnum getType() {
90         return ComponentTypeEnum.valueOf((String) getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE));
91         }
92
93         public void setType(ComponentTypeEnum type) {
94                 addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, type.name());
95         }
96
97         public void addMetadataProperty(GraphPropertyEnum propName, Object propValue) {
98                 if (metadataProperties == null) {
99                         metadataProperties = new HashMap<>();
100                 }
101                 if (propValue != null) {
102                         metadataProperties.put(propName, propValue);
103                 }
104         }
105
106         public Object getMetadataProperty(GraphPropertyEnum metadataProperty) {
107                 if (metadataProperties != null) {
108                         return metadataProperties.get(metadataProperty);
109                 }
110                 return null;
111         }
112
113         public Map<GraphPropertyEnum, Object> getMetadataProperties() {
114                 return metadataProperties;
115         }
116
117         public void setMetadataProperties(Map<GraphPropertyEnum, Object> metadataProperties) {
118                 this.metadataProperties = metadataProperties;
119         }
120
121         public void getOrSetDefaultInstantiationTypeForToscaElementJson(){
122                 String toscaVertexJsonInstantiationType;
123                 toscaVertexJsonInstantiationType = (String)(this.getJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE));
124                 if (toscaVertexJsonInstantiationType == StringUtils.EMPTY || toscaVertexJsonInstantiationType == null){
125                         this.setJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE, InstantiationTypes.A_LA_CARTE.getValue());
126                 };
127         };
128
129         public Map<String, Object> getMetadataJson() {
130                 return metadataJson;
131         }
132
133         public void setMetadataJson(Map<String, Object> metadataJson) {
134                 this.metadataJson = metadataJson;
135         }
136
137         /**
138          * used for clone vertex in case of copy on update
139          * 
140          * @param other
141          */
142         public void cloneData(GraphVertex other) {
143                 // need to be deep copy???
144                 json = other.getJson();
145                 metadataJson = other.getMetadataJson();
146                 metadataProperties = other.getMetadataProperties();
147         }
148
149         public void setJsonMetadataField(JsonPresentationFields field, Object value) {
150                 if (metadataJson == null) {
151                         metadataJson = new HashMap<>();
152                 }
153                 metadataJson.put(field.getPresentation(), value);
154         }
155
156         public Object getJsonMetadataField(JsonPresentationFields field) {
157                 if (metadataJson != null) {
158                         return metadataJson.get(field.getPresentation());
159                 }
160                 return null;
161         }
162
163         /**
164          * Updates metadata json with current metadataProperties. Note that already existing property containing in metadata json can be overrided by new value if metadataProperties contains the same property (by key). Note that metadata json can contain
165          * a property that is not presented in metadataProperties. In such case the property will be put in metadata json.
166          */
167         public void updateMetadataJsonWithCurrentMetadataProperties() {
168                 if (!MapUtils.isEmpty(metadataProperties)) {
169                         if (metadataJson == null) {
170                                 metadataJson = new HashMap<>();
171                         }
172                         for (Entry<GraphPropertyEnum, Object> entry : metadataProperties.entrySet()) {
173                                 String propertyName = JsonPresentationFields.getPresentationByGraphProperty(entry.getKey());
174                                 if (StringUtils.isNotEmpty(propertyName) && entry.getValue() != null) {
175                                         metadataJson.put(propertyName, entry.getValue());
176                                 }
177                         }
178                 }
179         }
180 }