Catalog alignment
[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 org.janusgraph.core.JanusGraphVertex;
24 import com.google.common.base.Strings;
25 import org.apache.commons.collections.MapUtils;
26 import org.apache.commons.lang.StringUtils;
27 import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
28 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
29 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
30 import org.openecomp.sdc.be.datatypes.enums.InstantiationTypes;
31 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
32 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
33
34 import java.util.HashMap;
35 import java.util.Map;
36 import java.util.Map.Entry;
37
38 public class GraphVertex {
39         private String uniqueId;
40
41         private JanusGraphVertex 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                 addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
64         }
65
66         public Map<String, ? extends ToscaDataDefinition> getJson() {
67                 return json;
68         }
69
70         public void setJson(Map<String, ? extends ToscaDataDefinition> json) {
71                 this.json = json;
72         }
73
74         public JanusGraphVertex getVertex() {
75                 return vertex;
76         }
77
78         public void setVertex(JanusGraphVertex vertex) {
79                 this.vertex = vertex;
80         }
81
82         public VertexTypeEnum getLabel() {
83                 return label;
84         }
85
86         public void setLabel(VertexTypeEnum label) {
87                 this.label = label;
88         }
89
90         public ComponentTypeEnum getType() {
91                 if(getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE) == null) {
92                         if (getMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE) != null) {
93                                 return ComponentTypeEnum.RESOURCE;
94                         }
95                         return null;
96                 }
97
98         return ComponentTypeEnum.valueOf((String) getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE));
99         }
100
101         public void setType(ComponentTypeEnum type) {
102                 addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, type.name());
103         }
104
105         public void addMetadataProperty(GraphPropertyEnum propName, Object propValue) {
106                 if (metadataProperties == null) {
107                         metadataProperties = new HashMap<>();
108                 }
109                 if (propValue != null) {
110                         metadataProperties.put(propName, propValue);
111                 }
112         }
113
114         public Object getMetadataProperty(GraphPropertyEnum metadataProperty) {
115                 if (metadataProperties != null) {
116                         return metadataProperties.get(metadataProperty);
117                 }
118                 return null;
119         }
120
121         public Map<GraphPropertyEnum, Object> getMetadataProperties() {
122                 return metadataProperties;
123         }
124
125         public void setMetadataProperties(Map<GraphPropertyEnum, Object> metadataProperties) {
126                 this.metadataProperties = metadataProperties;
127         }
128
129         public void getOrSetDefaultInstantiationTypeForToscaElementJson(){
130                 String toscaVertexJsonInstantiationType;
131                 toscaVertexJsonInstantiationType = (String)(this.getJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE));
132                 if (Strings.isNullOrEmpty(toscaVertexJsonInstantiationType)) {
133                         this.setJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE, InstantiationTypes.A_LA_CARTE.getValue());
134                 };
135         };
136
137         public Map<String, Object> getMetadataJson() {
138                 return metadataJson;
139         }
140
141         public void setMetadataJson(Map<String, Object> metadataJson) {
142                 this.metadataJson = metadataJson;
143         }
144
145         /**
146          * used for clone vertex in case of copy on update
147          * 
148          * @param other
149          */
150         public void cloneData(GraphVertex other) {
151                 // need to be deep copy???
152                 json = other.getJson();
153                 metadataJson = other.getMetadataJson();
154                 metadataProperties = other.getMetadataProperties();
155         }
156
157         public void setJsonMetadataField(JsonPresentationFields field, Object value) {
158                 if (metadataJson == null) {
159                         metadataJson = new HashMap<>();
160                 }
161                 metadataJson.put(field.getPresentation(), value);
162         }
163
164         public Object getJsonMetadataField(JsonPresentationFields field) {
165                 if (metadataJson != null) {
166                         return metadataJson.get(field.getPresentation());
167                 }
168                 return null;
169         }
170
171         /**
172          * 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
173          * a property that is not presented in metadataProperties. In such case the property will be put in metadata json.
174          */
175         public void updateMetadataJsonWithCurrentMetadataProperties() {
176                 if (!MapUtils.isEmpty(metadataProperties)) {
177                         if (metadataJson == null) {
178                                 metadataJson = new HashMap<>();
179                         }
180                         for (Entry<GraphPropertyEnum, Object> entry : metadataProperties.entrySet()) {
181                                 String propertyName = JsonPresentationFields.getPresentationByGraphProperty(entry.getKey());
182                                 if (StringUtils.isNotEmpty(propertyName) && entry.getValue() != null) {
183                                         metadataJson.put(propertyName, entry.getValue());
184                                 }
185                         }
186                 }
187         }
188 }