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=========================================================
21 package org.openecomp.sdc.be.dao.jsongraph;
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;
34 import java.util.HashMap;
36 import java.util.Map.Entry;
38 public class GraphVertex {
39 private String uniqueId;
41 private JanusGraphVertex vertex;
42 private VertexTypeEnum label;
44 private Map<String, ? extends ToscaDataDefinition> json;
45 private Map<String, Object> metadataJson;
46 private Map<GraphPropertyEnum, Object> metadataProperties;
48 public GraphVertex() {
52 public GraphVertex(VertexTypeEnum label) {
57 public String getUniqueId() {
61 public void setUniqueId(String uniqueId) {
62 this.uniqueId = uniqueId;
63 addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
66 public Map<String, ? extends ToscaDataDefinition> getJson() {
70 public void setJson(Map<String, ? extends ToscaDataDefinition> json) {
74 public JanusGraphVertex getVertex() {
78 public void setVertex(JanusGraphVertex vertex) {
82 public VertexTypeEnum getLabel() {
86 public void setLabel(VertexTypeEnum label) {
90 public ComponentTypeEnum getType() {
91 if(getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE) == null) {
92 if (getMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE) != null) {
93 return ComponentTypeEnum.RESOURCE;
98 return ComponentTypeEnum.valueOf((String) getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE));
101 public void setType(ComponentTypeEnum type) {
102 addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, type.name());
105 public void addMetadataProperty(GraphPropertyEnum propName, Object propValue) {
106 if (metadataProperties == null) {
107 metadataProperties = new HashMap<>();
109 if (propValue != null) {
110 metadataProperties.put(propName, propValue);
114 public Object getMetadataProperty(GraphPropertyEnum metadataProperty) {
115 if (metadataProperties != null) {
116 return metadataProperties.get(metadataProperty);
121 public Map<GraphPropertyEnum, Object> getMetadataProperties() {
122 return metadataProperties;
125 public void setMetadataProperties(Map<GraphPropertyEnum, Object> metadataProperties) {
126 this.metadataProperties = metadataProperties;
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());
137 public Map<String, Object> getMetadataJson() {
141 public void setMetadataJson(Map<String, Object> metadataJson) {
142 this.metadataJson = metadataJson;
146 * used for clone vertex in case of copy on update
150 public void cloneData(GraphVertex other) {
151 // need to be deep copy???
152 json = other.getJson();
153 metadataJson = other.getMetadataJson();
154 metadataProperties = other.getMetadataProperties();
157 public void setJsonMetadataField(JsonPresentationFields field, Object value) {
158 if (metadataJson == null) {
159 metadataJson = new HashMap<>();
161 metadataJson.put(field.getPresentation(), value);
164 public Object getJsonMetadataField(JsonPresentationFields field) {
165 if (metadataJson != null) {
166 return metadataJson.get(field.getPresentation());
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.
175 public void updateMetadataJsonWithCurrentMetadataProperties() {
176 if (!MapUtils.isEmpty(metadataProperties)) {
177 if (metadataJson == null) {
178 metadataJson = new HashMap<>();
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());