2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.openecomp.sdc.be.dao.impl;
18 import static java.util.stream.Collectors.joining;
20 import com.google.common.collect.ImmutableList;
21 import com.google.common.collect.ImmutableListMultimap;
22 import java.util.ArrayList;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Objects;
26 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import javax.annotation.PostConstruct;
30 import org.janusgraph.core.JanusGraphVertex;
31 import org.openecomp.sdc.be.dao.graph.datatype.GraphElement;
32 import org.openecomp.sdc.be.dao.graph.datatype.GraphNode;
33 import org.openecomp.sdc.be.dao.impl.heal.HealGraphDao;
34 import org.openecomp.sdc.be.dao.impl.heal.HealJanusGraphDao;
35 import org.openecomp.sdc.be.dao.impl.heal.HealNodeGraphDao;
36 import org.openecomp.sdc.be.dao.impl.heal.HealVertexGraphDao;
37 import org.openecomp.sdc.be.dao.jsongraph.GraphVertex;
38 import org.openecomp.sdc.be.dao.jsongraph.heal.Heal;
39 import org.openecomp.sdc.be.dao.jsongraph.heal.HealVersion;
40 import org.openecomp.sdc.be.dao.jsongraph.heal.HealVersionBuilder;
41 import org.openecomp.sdc.be.datatypes.enums.GraphPropertyEnum;
42 import org.openecomp.sdc.common.log.wrappers.Logger;
43 import org.springframework.beans.factory.annotation.Value;
44 import org.springframework.stereotype.Component;
46 @Component("healingPipelineDao")
47 public class HealingPipelineDao {
49 private static Logger logger = Logger.getLogger(HealingPipelineDao.class.getName());
50 private HealVersion<Integer> currentHealVersion;
51 @Value("${current.heal.version}")
52 private Integer healVersion;
53 private ImmutableListMultimap<String, Heal> healingPipeline;
54 private HealGraphDao healNodeGraphDao;
55 private HealGraphDao healVertexGraphDao;
56 private HealGraphDao healJanusGraphVertexGraphDao;
58 public HealingPipelineDao() {
59 healingPipeline = ImmutableListMultimap.of();
60 checkValidation(healingPipeline);
64 public void initHealVersion() {
65 currentHealVersion = HealVersionBuilder.build(healVersion);
69 public void initGraphHealers() {
70 healNodeGraphDao = new HealNodeGraphDao(this);
71 healVertexGraphDao = new HealVertexGraphDao(this);
72 healJanusGraphVertexGraphDao = new HealJanusGraphDao(this);
75 private HealGraphDao supplyHealer(Object graphNode) {
76 if (graphNode instanceof GraphVertex) {
77 return healVertexGraphDao;
79 if (graphNode instanceof GraphElement) {
80 return healNodeGraphDao;
82 if (graphNode instanceof JanusGraphVertex) {
83 return healJanusGraphVertexGraphDao;
88 public ImmutableListMultimap<String, Heal> getHealingPipeline() {
89 return healingPipeline;
92 public void setHealingPipeline(ImmutableListMultimap<String, Heal> healingPipeline) {
93 checkValidation(healingPipeline);
94 this.healingPipeline = healingPipeline;
97 public boolean shouldHeal(HealVersion<Integer> healerVersion, HealVersion<Integer> vertexVersion) {
98 Objects.requireNonNull(healerVersion);
99 Objects.requireNonNull(vertexVersion);
100 if (healerVersion.compareTo(currentHealVersion) >= 0) {
103 return healerVersion.compareTo(vertexVersion) >= 0;
106 public void setHealVersion(Integer healVersion) {
107 this.healVersion = healVersion;
110 public ImmutableList<Heal> getHealersForVertex(String edgeLabelEnum, HealVersion<Integer> vertexVersion) {
111 final ImmutableList<Heal> vertexHeals = getHealingPipeline().get(edgeLabelEnum);
112 List<Heal> list = new ArrayList<>();
113 for (Heal heal : vertexHeals) {
114 if (shouldHeal(heal.fromVersion(), vertexVersion)) {
118 return ImmutableList.copyOf(list);
121 public void setHealingVersion(final GraphVertex graphVertex) {
122 graphVertex.addMetadataProperty(GraphPropertyEnum.HEALING_VERSION, currentHealVersion.getVersion());
125 public void setHealingVersion(JanusGraphVertex graphVertex) {
126 graphVertex.property(GraphPropertyEnum.HEALING_VERSION.getProperty(), currentHealVersion.getVersion());
129 public void setHealingVersion(GraphNode graphNode) {
130 graphNode.setHealingVersion(currentHealVersion.getVersion());
133 public HealVersion<Integer> getCurrentHealVersion() {
134 return currentHealVersion;
137 public Optional performGraphReadHealing(Object graphNode, Object edgeLabel) {
138 HealGraphDao healGraphDao = supplyHealer(graphNode);
139 if (healGraphDao == null) {
140 logger.error("Unexpected graph node : {}", graphNode.getClass().getCanonicalName());
141 return Optional.empty();
143 return Optional.of(healGraphDao.performGraphReadHealing(graphNode, edgeLabel));
147 * prevent duplicated healing version for same edge label.
149 private void checkValidation(ImmutableListMultimap<String, Heal> listMultimap) {
150 listMultimap.keySet().forEach(key -> this.validNoDuplicates(key, listMultimap.get(key)));
153 private void validNoDuplicates(String key, List<Heal> heals) {
154 Set<Integer> duplicatedVersionSet = new HashSet<>();
155 Set<Integer> duplicatedNumbersSet = heals.stream().map(heal -> ((HealVersion<Integer>) heal.fromVersion()).getVersion())
156 .filter(n -> !duplicatedVersionSet.add(n)).collect(Collectors.toSet());
157 if (!duplicatedNumbersSet.isEmpty()) {
158 throw new IllegalStateException(String.format("Edge label %s , contains multiple healing with same version %s", key,
159 duplicatedNumbersSet.stream().map(Object::toString).collect(joining(" , ", "[ ", " ]"))));