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