Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-dao / src / main / java / org / openecomp / sdc / be / dao / impl / HealingPipelineDao.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.dao.impl;
18
19 import static java.util.stream.Collectors.joining;
20
21 import com.google.common.collect.ImmutableList;
22 import com.google.common.collect.ImmutableListMultimap;
23 import org.janusgraph.core.JanusGraphVertex;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Objects;
28 import java.util.Optional;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31 import javax.annotation.PostConstruct;
32 import org.openecomp.sdc.be.dao.graph.datatype.GraphElement;
33 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
34 import org.openecomp.sdc.be.dao.impl.heal.HealGraphDao;
35 import org.openecomp.sdc.be.dao.impl.heal.HealNodeGraphDao;
36 import org.openecomp.sdc.be.dao.impl.heal.HealJanusGraphDao;
37 import org.openecomp.sdc.be.dao.impl.heal.HealVertexGraphDao;
38 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
39 import org.openecomp.sdc.be.dao.jsongraph.heal.Heal;
40 import org.openecomp.sdc.be.dao.jsongraph.heal.HealVersion;
41 import org.openecomp.sdc.be.dao.jsongraph.heal.HealVersionBuilder;
42 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
43 import org.openecomp.sdc.common.log.wrappers.Logger;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.stereotype.Component;
46
47 @Component("healingPipelineDao")
48 public class HealingPipelineDao {
49
50     private static Logger logger = Logger.getLogger(HealingPipelineDao.class.getName());
51
52     private HealVersion<Integer> currentHealVersion;
53
54     @Value("${current.heal.version}")
55     private Integer healVersion;
56
57     private ImmutableListMultimap<String, Heal> healingPipeline;
58
59     private HealGraphDao healNodeGraphDao;
60     private HealGraphDao healVertexGraphDao;
61     private HealGraphDao healJanusGraphVertexGraphDao;
62
63     public HealingPipelineDao() {
64         healingPipeline = ImmutableListMultimap.of();
65         checkValidation(healingPipeline);
66     }
67
68     @PostConstruct
69     public void initHealVersion() {
70         currentHealVersion = HealVersionBuilder.build(healVersion);
71     }
72
73     @PostConstruct
74     public void initGraphHealers() {
75         healNodeGraphDao = new HealNodeGraphDao(this);
76         healVertexGraphDao = new HealVertexGraphDao(this);
77         healJanusGraphVertexGraphDao = new HealJanusGraphDao(this);
78     }
79
80
81     private HealGraphDao supplyHealer(Object graphNode) {
82         if (graphNode instanceof GraphVertex) {
83             return healVertexGraphDao;
84         }
85         if (graphNode instanceof GraphElement) {
86             return healNodeGraphDao;
87         }
88         if (graphNode instanceof JanusGraphVertex) {
89             return healJanusGraphVertexGraphDao;
90         }
91
92         return null;
93     }
94
95
96     public ImmutableListMultimap<String, Heal> getHealingPipeline() {
97         return healingPipeline;
98     }
99
100     public boolean shouldHeal(HealVersion<Integer> healerVersion, HealVersion<Integer> vertexVersion) {
101         Objects.requireNonNull(healerVersion);
102         Objects.requireNonNull(vertexVersion);
103         if (healerVersion.compareTo(currentHealVersion) >= 0) {
104             return false;
105         }
106         return healerVersion.compareTo(vertexVersion) >= 0;
107     }
108
109     public void setHealVersion(Integer healVersion) {
110         this.healVersion = healVersion;
111     }
112
113
114     public ImmutableList<Heal> getHealersForVertex(String edgeLabelEnum, HealVersion<Integer> vertexVersion) {
115         final ImmutableList<Heal> vertexHeals = getHealingPipeline().get(edgeLabelEnum);
116         List<Heal> list = new ArrayList<>();
117         for (Heal heal : vertexHeals) {
118             if (shouldHeal(heal.fromVersion(), vertexVersion)) {
119                 list.add(heal);
120             }
121         }
122         return ImmutableList.copyOf(list);
123     }
124
125
126     public void setHealingPipeline(ImmutableListMultimap<String, Heal> healingPipeline) {
127         checkValidation(healingPipeline);
128         this.healingPipeline = healingPipeline;
129     }
130
131
132     public void setHealingVersion(final GraphVertex graphVertex) {
133         graphVertex.addMetadataProperty(GraphPropertyEnum.HEALING_VERSION, currentHealVersion.getVersion());
134     }
135
136     public void setHealingVersion(JanusGraphVertex graphVertex) {
137         graphVertex.property(GraphPropertyEnum.HEALING_VERSION.getProperty(), currentHealVersion.getVersion());
138     }
139
140     public void setHealingVersion(GraphNode graphNode) {
141         graphNode.setHealingVersion(currentHealVersion.getVersion());
142     }
143
144     public HealVersion<Integer> getCurrentHealVersion() {
145         return currentHealVersion;
146     }
147
148     public Optional performGraphReadHealing(Object graphNode, Object edgeLabel) {
149         HealGraphDao healGraphDao = supplyHealer(graphNode);
150         if (healGraphDao == null) {
151             logger.error("Unexpected graph node : {}", graphNode.getClass().getCanonicalName());
152             return Optional.empty();
153         }
154         return Optional.of(healGraphDao.performGraphReadHealing(graphNode, edgeLabel));
155     }
156
157     /**
158      * prevent duplicated healing version for same edge label.
159      */
160     private void checkValidation(ImmutableListMultimap<String, Heal> listMultimap)  {
161         listMultimap.keySet().forEach(key -> this.validNoDuplicates(key, listMultimap.get(key)));
162     }
163
164     private void validNoDuplicates(String key, List<Heal> heals) {
165         Set<Integer> duplicatedVersionSet = new HashSet<>();
166         Set<Integer> duplicatedNumbersSet = heals.stream().map(heal -> ((HealVersion<Integer>) heal.fromVersion()).getVersion()).filter(n -> !duplicatedVersionSet.add(n)).collect(Collectors.toSet());
167         if (!duplicatedNumbersSet.isEmpty()) {
168             throw new IllegalStateException(String.format("Edge label %s , contains multiple healing with same version %s", key, duplicatedNumbersSet.stream().map(Object::toString).collect(joining(" , ", "[ ", " ]"))));
169         }
170     }
171
172 }