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.
17 package org.openecomp.sdc.be.dao.impl;
19 import static java.util.stream.Collectors.joining;
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;
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;
47 @Component("healingPipelineDao")
48 public class HealingPipelineDao {
50 private static Logger logger = Logger.getLogger(HealingPipelineDao.class.getName());
52 private HealVersion<Integer> currentHealVersion;
54 @Value("${current.heal.version}")
55 private Integer healVersion;
57 private ImmutableListMultimap<String, Heal> healingPipeline;
59 private HealGraphDao healNodeGraphDao;
60 private HealGraphDao healVertexGraphDao;
61 private HealGraphDao healJanusGraphVertexGraphDao;
63 public HealingPipelineDao() {
64 healingPipeline = ImmutableListMultimap.of();
65 checkValidation(healingPipeline);
69 public void initHealVersion() {
70 currentHealVersion = HealVersionBuilder.build(healVersion);
74 public void initGraphHealers() {
75 healNodeGraphDao = new HealNodeGraphDao(this);
76 healVertexGraphDao = new HealVertexGraphDao(this);
77 healJanusGraphVertexGraphDao = new HealJanusGraphDao(this);
81 private HealGraphDao supplyHealer(Object graphNode) {
82 if (graphNode instanceof GraphVertex) {
83 return healVertexGraphDao;
85 if (graphNode instanceof GraphElement) {
86 return healNodeGraphDao;
88 if (graphNode instanceof JanusGraphVertex) {
89 return healJanusGraphVertexGraphDao;
96 public ImmutableListMultimap<String, Heal> getHealingPipeline() {
97 return healingPipeline;
100 public boolean shouldHeal(HealVersion<Integer> healerVersion, HealVersion<Integer> vertexVersion) {
101 Objects.requireNonNull(healerVersion);
102 Objects.requireNonNull(vertexVersion);
103 if (healerVersion.compareTo(currentHealVersion) >= 0) {
106 return healerVersion.compareTo(vertexVersion) >= 0;
109 public void setHealVersion(Integer healVersion) {
110 this.healVersion = healVersion;
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)) {
122 return ImmutableList.copyOf(list);
126 public void setHealingPipeline(ImmutableListMultimap<String, Heal> healingPipeline) {
127 checkValidation(healingPipeline);
128 this.healingPipeline = healingPipeline;
132 public void setHealingVersion(final GraphVertex graphVertex) {
133 graphVertex.addMetadataProperty(GraphPropertyEnum.HEALING_VERSION, currentHealVersion.getVersion());
136 public void setHealingVersion(JanusGraphVertex graphVertex) {
137 graphVertex.property(GraphPropertyEnum.HEALING_VERSION.getProperty(), currentHealVersion.getVersion());
140 public void setHealingVersion(GraphNode graphNode) {
141 graphNode.setHealingVersion(currentHealVersion.getVersion());
144 public HealVersion<Integer> getCurrentHealVersion() {
145 return currentHealVersion;
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();
154 return Optional.of(healGraphDao.performGraphReadHealing(graphNode, edgeLabel));
158 * prevent duplicated healing version for same edge label.
160 private void checkValidation(ImmutableListMultimap<String, Heal> listMultimap) {
161 listMultimap.keySet().forEach(key -> this.validNoDuplicates(key, listMultimap.get(key)));
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(" , ", "[ ", " ]"))));