Reformat catalog-dao
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / graph / datatype / GraphNode.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.graph.datatype;
21
22 import com.google.gson.Gson;
23 import java.util.List;
24 import java.util.Map;
25 import org.apache.commons.lang3.tuple.ImmutablePair;
26 import org.openecomp.sdc.be.dao.jsongraph.heal.HealConstants;
27 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
28 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
29 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
30
31 public abstract class GraphNode extends GraphElement {
32
33     private static final Gson gson = new Gson();
34     private NodeTypeEnum label;
35
36     protected GraphNode(NodeTypeEnum label) {
37         super(GraphElementTypeEnum.Node);
38         this.label = label;
39     }
40
41     protected Gson getGson() {
42         return gson;
43     }
44
45     public String getLabel() {
46         return label.getName();
47     }
48
49     public ImmutablePair<String, Object> getKeyValueId() {
50         return new ImmutablePair<>(getUniqueIdKey(), getUniqueId());
51     }
52
53     public ImmutablePair<String, Object> getKeyValueIdForLog() {
54         return getKeyValueId();
55     }
56
57     protected void addIfExists(Map<String, Object> map, GraphPropertiesDictionary property, Object value) {
58         if (value != null) {
59             if (value instanceof List || value instanceof Map) {
60                 value = getGson().toJson(value);
61             }
62             map.put(property.getProperty(), value);
63         }
64     }
65
66     public abstract String getUniqueId();
67
68     public String getUniqueIdKey() {
69         return GraphPropertiesDictionary.UNIQUE_ID.getProperty();
70     }
71
72     public String getHealingVersionKey() {
73         return GraphPropertyEnum.HEALING_VERSION.getProperty();
74     }
75
76     /**
77      * Must be overridden in implelemting classes
78      *
79      * @return current heal version. Default heal version if function not implemented.
80      */
81     public Integer getHealingVersion() {
82         return HealConstants.DEFAULT_HEAL_VERSION;
83     }
84
85     /**
86      * Must be overriden in implementing classes
87      *
88      * @param version healing version number
89      */
90     public void setHealingVersion(Integer version) {
91     }
92
93     @Override
94     public String toString() {
95         return "GraphNode [label=" + label + ", parent: " + super.toString() + "]";
96     }
97 }