Update vulnerable package dependencies
[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 package org.openecomp.sdc.be.dao.jsongraph;
21
22 import com.google.common.base.Strings;
23 import java.util.HashMap;
24 import java.util.Map;
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;
35
36 public class GraphVertex {
37
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;
44
45     public GraphVertex() {
46     }
47
48     public GraphVertex(VertexTypeEnum label) {
49         super();
50         this.label = label;
51     }
52
53     public String getUniqueId() {
54         return uniqueId;
55     }
56
57     public void setUniqueId(String uniqueId) {
58         this.uniqueId = uniqueId;
59         addMetadataProperty(GraphPropertyEnum.UNIQUE_ID, uniqueId);
60     }
61
62     public Map<String, ? extends ToscaDataDefinition> getJson() {
63         return json;
64     }
65
66     public void setJson(Map<String, ? extends ToscaDataDefinition> json) {
67         this.json = json;
68     }
69
70     public JanusGraphVertex getVertex() {
71         return vertex;
72     }
73
74     public void setVertex(JanusGraphVertex vertex) {
75         this.vertex = vertex;
76     }
77
78     public VertexTypeEnum getLabel() {
79         return label;
80     }
81
82     public void setLabel(VertexTypeEnum label) {
83         this.label = label;
84     }
85
86     public ComponentTypeEnum getType() {
87         if (getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE) == null) {
88             if (getMetadataProperty(GraphPropertyEnum.RESOURCE_TYPE) != null) {
89                 return ComponentTypeEnum.RESOURCE;
90             }
91             return null;
92         }
93         return ComponentTypeEnum.valueOf((String) getMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE));
94     }
95
96     public void setType(ComponentTypeEnum type) {
97         addMetadataProperty(GraphPropertyEnum.COMPONENT_TYPE, type.name());
98     }
99
100     public void addMetadataProperty(GraphPropertyEnum propName, Object propValue) {
101         if (metadataProperties == null) {
102             metadataProperties = new HashMap<>();
103         }
104         if (propValue != null) {
105             metadataProperties.put(propName, propValue);
106         }
107     }
108
109     public Object getMetadataProperty(GraphPropertyEnum metadataProperty) {
110         if (metadataProperties != null) {
111             return metadataProperties.get(metadataProperty);
112         }
113         return null;
114     }
115
116     public Map<GraphPropertyEnum, Object> getMetadataProperties() {
117         return metadataProperties;
118     }
119
120     public void setMetadataProperties(Map<GraphPropertyEnum, Object> metadataProperties) {
121         this.metadataProperties = metadataProperties;
122     }
123
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());
129         }
130     }
131
132     public Map<String, Object> getMetadataJson() {
133         return metadataJson;
134     }
135
136     public void setMetadataJson(Map<String, Object> metadataJson) {
137         this.metadataJson = metadataJson;
138     }
139
140     /**
141      * used for clone vertex in case of copy on update
142      *
143      * @param other
144      */
145     public void cloneData(GraphVertex other) {
146         // need to be deep copy???
147         json = other.getJson();
148         metadataJson = other.getMetadataJson();
149         metadataProperties = other.getMetadataProperties();
150     }
151
152     public void setJsonMetadataField(JsonPresentationFields field, Object value) {
153         if (metadataJson == null) {
154             metadataJson = new HashMap<>();
155         }
156         metadataJson.put(field.getPresentation(), value);
157     }
158
159     public Object getJsonMetadataField(JsonPresentationFields field) {
160         if (metadataJson != null) {
161             return metadataJson.get(field.getPresentation());
162         }
163         return null;
164     }
165
166     public void setJsonMetadataField(String field, Object value) {
167         if (metadataJson == null) {
168             metadataJson = new HashMap<>();
169         }
170         metadataJson.put(field, value);
171     }
172
173     public Object getJsonMetadataField(String field) {
174         if (metadataJson != null) {
175             return metadataJson.get(field);
176         }
177         return null;
178     }
179
180     /**
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.
184      */
185     public void updateMetadataJsonWithCurrentMetadataProperties() {
186         if (!MapUtils.isEmpty(metadataProperties)) {
187             if (metadataJson == null) {
188                 metadataJson = new HashMap<>();
189             }
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());
194                 }
195             }
196         }
197     }
198 }