aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/jsongraph/GraphVertex.java
blob: 74852944d035fc4e672d1503dd3f36627df7644e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.dao.jsongraph;

import com.thinkaurelius.titan.core.TitanVertex;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.openecomp.sdc.be.dao.jsongraph.types.VertexTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
import org.openecomp.sdc.be.datatypes.enums.InstantiationTypes;
import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class GraphVertex {
	private String uniqueId;

	private TitanVertex vertex;
	private VertexTypeEnum label;

	private Map<String, ? extends ToscaDataDefinition> json;
	private Map<String, Object> metadataJson;
	private Map<GraphPropertyEnum, Object> metadataProperties;

	public GraphVertex() {

	}

	public GraphVertex(VertexTypeEnum label) {
		super();
		this.label = label;
	}

	public String getUniqueId() {
		return uniqueId;
	}

	public void setUniqueId(String uniqueId) {
		this.uniqueId = uniqueId;
		addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
	}

	public Map<String, ? extends ToscaDataDefinition> getJson() {
		return json;
	}

	public void setJson(Map<String, ? extends ToscaDataDefinition> json) {
		this.json = json;
	}

	public TitanVertex getVertex() {
		return vertex;
	}

	public void setVertex(TitanVertex vertex) {
		this.vertex = vertex;
	}

	public VertexTypeEnum getLabel() {
		return label;
	}

	public void setLabel(VertexTypeEnum label) {
		this.label = label;
	}

	public ComponentTypeEnum getType() {
        return ComponentTypeEnum.valueOf((String) getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE));
	}

	public void setType(ComponentTypeEnum type) {
		addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, type.name());
	}

	public void addMetadataProperty(GraphPropertyEnum propName, Object propValue) {
		if (metadataProperties == null) {
			metadataProperties = new HashMap<>();
		}
		if (propValue != null) {
			metadataProperties.put(propName, propValue);
		}
	}

	public Object getMetadataProperty(GraphPropertyEnum metadataProperty) {
		if (metadataProperties != null) {
			return metadataProperties.get(metadataProperty);
		}
		return null;
	}

	public Map<GraphPropertyEnum, Object> getMetadataProperties() {
		return metadataProperties;
	}

	public void setMetadataProperties(Map<GraphPropertyEnum, Object> metadataProperties) {
		this.metadataProperties = metadataProperties;
	}

	public void getOrSetDefaultInstantiationTypeForToscaElementJson(){
		String toscaVertexJsonInstantiationType;
		toscaVertexJsonInstantiationType = (String)(this.getJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE));
		if (toscaVertexJsonInstantiationType == StringUtils.EMPTY || toscaVertexJsonInstantiationType == null){
			this.setJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE, InstantiationTypes.A_LA_CARTE.getValue());
		};
	};

	public Map<String, Object> getMetadataJson() {
		return metadataJson;
	}

	public void setMetadataJson(Map<String, Object> metadataJson) {
		this.metadataJson = metadataJson;
	}

	/**
	 * used for clone vertex in case of copy on update
	 * 
	 * @param other
	 */
	public void cloneData(GraphVertex other) {
		// need to be deep copy???
		json = other.getJson();
		metadataJson = other.getMetadataJson();
		metadataProperties = other.getMetadataProperties();
	}

	public void setJsonMetadataField(JsonPresentationFields field, Object value) {
		if (metadataJson == null) {
			metadataJson = new HashMap<>();
		}
		metadataJson.put(field.getPresentation(), value);
	}

	public Object getJsonMetadataField(JsonPresentationFields field) {
		if (metadataJson != null) {
			return metadataJson.get(field.getPresentation());
		}
		return null;
	}

	/**
	 * 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
	 * a property that is not presented in metadataProperties. In such case the property will be put in metadata json.
	 */
	public void updateMetadataJsonWithCurrentMetadataProperties() {
		if (!MapUtils.isEmpty(metadataProperties)) {
			if (metadataJson == null) {
				metadataJson = new HashMap<>();
			}
			for (Entry<GraphPropertyEnum, Object> entry : metadataProperties.entrySet()) {
				String propertyName = JsonPresentationFields.getPresentationByGraphProperty(entry.getKey());
				if (StringUtils.isNotEmpty(propertyName) && entry.getValue() != null) {
					metadataJson.put(propertyName, entry.getValue());
				}
			}
		}
	}
}