[sdc] rebase update
[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 java.util.HashMap;
24 import java.util.Map;
25 import java.util.Map.Entry;
26
27 import org.apache.commons.collections.MapUtils;
28 import org.apache.commons.lang.StringUtils;
29 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
32 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
33 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
34 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
35
36 import com.thinkaurelius.titan.core.TitanVertex;
37
38 public class GraphVertex {
39         private String uniqueId;
40
41         private TitanVertex vertex;
42         private VertexTypeEnum label;
43
44         private Map<String, ? extends ToscaDataDefinition> json;
45         private Map<String, Object> metadataJson;
46         private Map<GraphPropertyEnum, Object> metadataProperties;
47
48         public GraphVertex() {
49
50         }
51
52         public GraphVertex(VertexTypeEnum label) {
53                 super();
54                 this.label = label;
55         }
56
57         public String getUniqueId() {
58                 return uniqueId;
59         }
60
61         public void setUniqueId(String uniqueId) {
62                 this.uniqueId = 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                 ComponentTypeEnum type = ComponentTypeEnum.valueOf((String) getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE));
91                 return type;
92         }
93
94         public void setType(ComponentTypeEnum type) {
95                 addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, type.name());
96         }
97
98         public void addMetadataProperty(GraphPropertyEnum propName, Object propValue) {
99                 if (metadataProperties == null) {
100                         metadataProperties = new HashMap<>();
101                 }
102                 if (propValue != null) {
103                         metadataProperties.put(propName, propValue);
104                 }
105         }
106
107         public Object getMetadataProperty(GraphPropertyEnum metadataProperty) {
108                 if (metadataProperties != null) {
109                         return metadataProperties.get(metadataProperty);
110                 }
111                 return null;
112         }
113
114         public Map<GraphPropertyEnum, Object> getMetadataProperties() {
115                 return metadataProperties;
116         }
117
118         public void setMetadataProperties(Map<GraphPropertyEnum, Object> metadataProperties) {
119                 this.metadataProperties = metadataProperties;
120         }
121
122         public Map<String, Object> getMetadataJson() {
123                 return metadataJson;
124         }
125
126         public void setMetadataJson(Map<String, Object> metadataJson) {
127                 this.metadataJson = metadataJson;
128         }
129
130         /**
131          * used for clone vertex in case of copy on update
132          * 
133          * @param other
134          */
135         public void cloneData(GraphVertex other) {
136                 // need to be deep copy???
137                 json = other.getJson();
138                 metadataJson = other.getMetadataJson();
139                 metadataProperties = other.getMetadataProperties();
140         }
141
142         public void setJsonMetadataField(JsonPresentationFields field, Object value) {
143                 if (metadataJson == null) {
144                         metadataJson = new HashMap<>();
145                 }
146                 metadataJson.put(field.getPresentation(), value);
147         }
148
149         public Object getJsonMetadataField(JsonPresentationFields field) {
150                 if (metadataJson != null) {
151                         return metadataJson.get(field.getPresentation());
152                 }
153                 return null;
154         }
155
156         /**
157          * 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
158          * a property that is not presented in metadataProperties. In such case the property will be put in metadata json.
159          */
160         public void updateMetadataJsonWithCurrentMetadataProperties() {
161                 if (!MapUtils.isEmpty(metadataProperties)) {
162                         if (metadataJson == null) {
163                                 metadataJson = new HashMap<>();
164                         }
165                         for (Entry<GraphPropertyEnum, Object> entry : metadataProperties.entrySet()) {
166                                 String propertyName = JsonPresentationFields.getPresentationByGraphProperty(entry.getKey());
167                                 if (StringUtils.isNotEmpty(propertyName) && entry.getValue() != null) {
168                                         metadataJson.put(propertyName, entry.getValue());
169                                 }
170                         }
171                 }
172         }
173 }