Reformat catalog-dao
[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 package org.openecomp.sdc.be.dao.impl;
17
18 import static java.util.stream.Collectors.joining;
19
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;
27 import java.util.Set;
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;
45
46 @Component("healingPipelineDao")
47 public class HealingPipelineDao {
48
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;
57
58     public HealingPipelineDao() {
59         healingPipeline = ImmutableListMultimap.of();
60         checkValidation(healingPipeline);
61     }
62
63     @PostConstruct
64     public void initHealVersion() {
65         currentHealVersion = HealVersionBuilder.build(healVersion);
66     }
67
68     @PostConstruct
69     public void initGraphHealers() {
70         healNodeGraphDao = new HealNodeGraphDao(this);
71         healVertexGraphDao = new HealVertexGraphDao(this);
72         healJanusGraphVertexGraphDao = new HealJanusGraphDao(this);
73     }
74
75     private HealGraphDao supplyHealer(Object graphNode) {
76         if (graphNode instanceof GraphVertex) {
77             return healVertexGraphDao;
78         }
79         if (graphNode instanceof GraphElement) {
80             return healNodeGraphDao;
81         }
82         if (graphNode instanceof JanusGraphVertex) {
83             return healJanusGraphVertexGraphDao;
84         }
85         return null;
86     }
87
88     public ImmutableListMultimap<String, Heal> getHealingPipeline() {
89         return healingPipeline;
90     }
91
92     public void setHealingPipeline(ImmutableListMultimap<String, Heal> healingPipeline) {
93         checkValidation(healingPipeline);
94         this.healingPipeline = healingPipeline;
95     }
96
97     public boolean shouldHeal(HealVersion<Integer> healerVersion, HealVersion<Integer> vertexVersion) {
98         Objects.requireNonNull(healerVersion);
99         Objects.requireNonNull(vertexVersion);
100         if (healerVersion.compareTo(currentHealVersion) >= 0) {
101             return false;
102         }
103         return healerVersion.compareTo(vertexVersion) >= 0;
104     }
105
106     public void setHealVersion(Integer healVersion) {
107         this.healVersion = healVersion;
108     }
109
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)) {
115                 list.add(heal);
116             }
117         }
118         return ImmutableList.copyOf(list);
119     }
120
121     public void setHealingVersion(final GraphVertex graphVertex) {
122         graphVertex.addMetadataProperty(GraphPropertyEnum.HEALING_VERSION, currentHealVersion.getVersion());
123     }
124
125     public void setHealingVersion(JanusGraphVertex graphVertex) {
126         graphVertex.property(GraphPropertyEnum.HEALING_VERSION.getProperty(), currentHealVersion.getVersion());
127     }
128
129     public void setHealingVersion(GraphNode graphNode) {
130         graphNode.setHealingVersion(currentHealVersion.getVersion());
131     }
132
133     public HealVersion<Integer> getCurrentHealVersion() {
134         return currentHealVersion;
135     }
136
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();
142         }
143         return Optional.of(healGraphDao.performGraphReadHealing(graphNode, edgeLabel));
144     }
145
146     /**
147      * prevent duplicated healing version for same edge label.
148      */
149     private void checkValidation(ImmutableListMultimap<String, Heal> listMultimap) {
150         listMultimap.keySet().forEach(key -> this.validNoDuplicates(key, listMultimap.get(key)));
151     }
152
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(" , ", "[ ", " ]"))));
160         }
161     }
162 }