2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.dao.jsongraph;
22 import com.google.common.base.Strings;
23 import java.util.HashMap;
25 import java.util.Map.Entry;
26 import org.apache.commons.collections.MapUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.janusgraph.core.JanusGraphVertex;
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.InstantiationTypes;
33 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
34 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
36 public class GraphVertex {
38 private String uniqueId;
39 private JanusGraphVertex vertex;
40 private VertexTypeEnum label;
41 private Map<String, ? extends ToscaDataDefinition> json;
42 private Map<String, Object> metadataJson;
43 private Map<GraphPropertyEnum, Object> metadataProperties;
45 public GraphVertex() {
48 public GraphVertex(VertexTypeEnum label) {
53 public String getUniqueId() {
57 public void setUniqueId(String uniqueId) {
58 this.uniqueId = uniqueId;
59 addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
62 public Map<String, ? extends ToscaDataDefinition> getJson() {
66 public void setJson(Map<String, ? extends ToscaDataDefinition> json) {
70 public JanusGraphVertex getVertex() {
74 public void setVertex(JanusGraphVertex vertex) {
78 public VertexTypeEnum getLabel() {
82 public void setLabel(VertexTypeEnum label) {
86 public ComponentTypeEnum getType() {
87 if (getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE) == null) {
88 if (getMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE) != null) {
89 return ComponentTypeEnum.RESOURCE;
93 return ComponentTypeEnum.valueOf((String) getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE));
96 public void setType(ComponentTypeEnum type) {
97 addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, type.name());
100 public void addMetadataProperty(GraphPropertyEnum propName, Object propValue) {
101 if (metadataProperties == null) {
102 metadataProperties = new HashMap<>();
104 if (propValue != null) {
105 metadataProperties.put(propName, propValue);
109 public Object getMetadataProperty(GraphPropertyEnum metadataProperty) {
110 if (metadataProperties != null) {
111 return metadataProperties.get(metadataProperty);
116 public Map<GraphPropertyEnum, Object> getMetadataProperties() {
117 return metadataProperties;
120 public void setMetadataProperties(Map<GraphPropertyEnum, Object> metadataProperties) {
121 this.metadataProperties = metadataProperties;
124 public void getOrSetDefaultInstantiationTypeForToscaElementJson() {
125 String toscaVertexJsonInstantiationType;
126 toscaVertexJsonInstantiationType = (String) (this.getJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE));
127 if (Strings.isNullOrEmpty(toscaVertexJsonInstantiationType)) {
128 this.setJsonMetadataField(JsonPresentationFields.INSTANTIATION_TYPE, InstantiationTypes.A_LA_CARTE.getValue());
132 public Map<String, Object> getMetadataJson() {
136 public void setMetadataJson(Map<String, Object> metadataJson) {
137 this.metadataJson = metadataJson;
141 * used for clone vertex in case of copy on update
145 public void cloneData(GraphVertex other) {
146 // need to be deep copy???
147 json = other.getJson();
148 metadataJson = other.getMetadataJson();
149 metadataProperties = other.getMetadataProperties();
152 public void setJsonMetadataField(JsonPresentationFields field, Object value) {
153 if (metadataJson == null) {
154 metadataJson = new HashMap<>();
156 metadataJson.put(field.getPresentation(), value);
159 public Object getJsonMetadataField(JsonPresentationFields field) {
160 if (metadataJson != null) {
161 return metadataJson.get(field.getPresentation());
166 public void setJsonMetadataField(String field, Object value) {
167 if (metadataJson == null) {
168 metadataJson = new HashMap<>();
170 metadataJson.put(field, value);
173 public Object getJsonMetadataField(String field) {
174 if (metadataJson != null) {
175 return metadataJson.get(field);
181 * Updates metadata json with current metadataProperties. Note that already existing property containing in metadata json can be overrided by new
182 * value if metadataProperties contains the same property (by key). Note that metadata json can contain a property that is not presented in
183 * metadataProperties. In such case the property will be put in metadata json.
185 public void updateMetadataJsonWithCurrentMetadataProperties() {
186 if (!MapUtils.isEmpty(metadataProperties)) {
187 if (metadataJson == null) {
188 metadataJson = new HashMap<>();
190 for (Entry<GraphPropertyEnum, Object> entry : metadataProperties.entrySet()) {
191 String propertyName = JsonPresentationFields.getPresentationByGraphProperty(entry.getKey());
192 if (StringUtils.isNotEmpty(propertyName) && entry.getValue() != null) {
193 metadataJson.put(propertyName, entry.getValue());