3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.rule.registration;
22 import com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.ImmutableSet;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.HashSet;
27 import java.util.LinkedHashMap;
28 import java.util.List;
30 import java.util.Objects;
31 import java.util.Optional;
33 import java.util.function.Function;
34 import java.util.stream.Collectors;
35 import java.util.stream.Stream;
36 import org.sonar.api.rule.RuleKey;
37 import org.sonar.api.server.rule.RulesDefinition;
38 import org.sonar.api.utils.log.Logger;
39 import org.sonar.api.utils.log.Loggers;
40 import org.sonar.db.DbClient;
41 import org.sonar.db.DbSession;
42 import org.sonar.db.rule.RuleDto;
43 import org.sonar.db.rule.RuleParamDto;
45 import static com.google.common.base.Preconditions.checkState;
46 import static java.util.Collections.emptyList;
47 import static java.util.Collections.emptySet;
48 import static java.util.Collections.unmodifiableMap;
50 class RulesRegistrationContext {
52 private static final Logger LOG = Loggers.get(RulesRegistrationContext.class);
54 // initial immutable data
55 private final Map<RuleKey, RuleDto> dbRules;
56 private final Set<RuleDto> known;
57 private final Map<String, Set<SingleDeprecatedRuleKey>> dbDeprecatedKeysByUuid;
58 private final Map<String, List<RuleParamDto>> ruleParamsByRuleUuid;
59 private final Map<RuleKey, RuleDto> dbRulesByDbDeprecatedKey;
61 private final Set<RuleDto> created = new HashSet<>();
62 private final Map<RuleDto, RuleKey> renamed = new HashMap<>();
63 private final Set<RuleDto> updated = new HashSet<>();
64 private final Set<RuleDto> unchanged = new HashSet<>();
65 private final Set<RuleDto> removed = new HashSet<>();
67 private RulesRegistrationContext(Map<RuleKey, RuleDto> dbRules, Map<String, Set<SingleDeprecatedRuleKey>> dbDeprecatedKeysByUuid,
68 Map<String, List<RuleParamDto>> ruleParamsByRuleUuid) {
69 this.dbRules = ImmutableMap.copyOf(dbRules);
70 this.known = ImmutableSet.copyOf(dbRules.values());
71 this.dbDeprecatedKeysByUuid = dbDeprecatedKeysByUuid;
72 this.ruleParamsByRuleUuid = ruleParamsByRuleUuid;
73 this.dbRulesByDbDeprecatedKey = buildDbRulesByDbDeprecatedKey(dbDeprecatedKeysByUuid, dbRules);
76 private static Map<RuleKey, RuleDto> buildDbRulesByDbDeprecatedKey(Map<String, Set<SingleDeprecatedRuleKey>> dbDeprecatedKeysByUuid,
77 Map<RuleKey, RuleDto> dbRules) {
78 Map<String, RuleDto> dbRulesByRuleUuid = dbRules.values().stream()
79 .collect(Collectors.toMap(RuleDto::getUuid, Function.identity()));
81 Map<RuleKey, RuleDto> rulesByKey = new LinkedHashMap<>();
82 for (Map.Entry<String, Set<SingleDeprecatedRuleKey>> entry : dbDeprecatedKeysByUuid.entrySet()) {
83 String ruleUuid = entry.getKey();
84 RuleDto rule = dbRulesByRuleUuid.get(ruleUuid);
86 LOG.warn("Could not retrieve rule with uuid %s referenced by a deprecated rule key. " +
87 "The following deprecated rule keys seem to be referencing a non-existing rule",
88 ruleUuid, entry.getValue());
90 entry.getValue().forEach(d -> rulesByKey.put(d.getOldRuleKeyAsRuleKey(), rule));
93 return unmodifiableMap(rulesByKey);
96 boolean hasDbRules() {
97 return !dbRules.isEmpty();
100 Optional<RuleDto> getDbRuleFor(RulesDefinition.Rule ruleDef) {
101 RuleKey ruleKey = RuleKey.of(ruleDef.repository().key(), ruleDef.key());
102 Optional<RuleDto> res = Stream.concat(Stream.of(ruleKey), ruleDef.deprecatedRuleKeys().stream())
104 .filter(Objects::nonNull)
106 // may occur in case of plugin downgrade
108 return Optional.ofNullable(dbRulesByDbDeprecatedKey.get(ruleKey));
113 Map<RuleKey, SingleDeprecatedRuleKey> getDbDeprecatedKeysByOldRuleKey() {
114 return dbDeprecatedKeysByUuid.values().stream()
115 .flatMap(Collection::stream)
116 .collect(Collectors.toMap(SingleDeprecatedRuleKey::getOldRuleKeyAsRuleKey, Function.identity()));
119 Set<SingleDeprecatedRuleKey> getDBDeprecatedKeysFor(RuleDto rule) {
120 return dbDeprecatedKeysByUuid.getOrDefault(rule.getUuid(), emptySet());
123 List<RuleParamDto> getRuleParametersFor(String ruleUuid) {
124 return ruleParamsByRuleUuid.getOrDefault(ruleUuid, emptyList());
127 Stream<RuleDto> getRemaining() {
128 Set<RuleDto> res = new HashSet<>(dbRules.values());
129 res.removeAll(unchanged);
130 res.removeAll(renamed.keySet());
131 res.removeAll(updated);
132 res.removeAll(removed);
136 Stream<RuleDto> getRemoved() {
137 return removed.stream();
140 public Stream<Map.Entry<RuleDto, RuleKey>> getRenamed() {
141 return renamed.entrySet().stream();
144 Stream<RuleDto> getAllModified() {
149 renamed.keySet().stream())
153 boolean isCreated(RuleDto ruleDto) {
154 return created.contains(ruleDto);
157 boolean isRenamed(RuleDto ruleDto) {
158 return renamed.containsKey(ruleDto);
161 boolean isUpdated(RuleDto ruleDto) {
162 return updated.contains(ruleDto);
165 void created(RuleDto ruleDto) {
166 checkState(!known.contains(ruleDto), "known RuleDto can't be created");
167 created.add(ruleDto);
170 void renamed(RuleDto ruleDto) {
171 ensureKnown(ruleDto);
172 renamed.put(ruleDto, ruleDto.getKey());
175 void updated(RuleDto ruleDto) {
176 ensureKnown(ruleDto);
177 updated.add(ruleDto);
180 void removed(RuleDto ruleDto) {
181 ensureKnown(ruleDto);
182 removed.add(ruleDto);
185 void unchanged(RuleDto ruleDto) {
186 ensureKnown(ruleDto);
187 unchanged.add(ruleDto);
190 private void ensureKnown(RuleDto ruleDto) {
191 checkState(known.contains(ruleDto), "unknown RuleDto");
194 static RulesRegistrationContext create(DbClient dbClient, DbSession dbSession) {
195 Map<RuleKey, RuleDto> allRules = dbClient.ruleDao().selectAll(dbSession).stream()
196 .collect(Collectors.toMap(RuleDto::getKey, Function.identity()));
197 Map<String, Set<SingleDeprecatedRuleKey>> existingDeprecatedKeysById = loadDeprecatedRuleKeys(dbClient, dbSession);
198 Map<String, List<RuleParamDto>> ruleParamsByRuleUuid = loadAllRuleParameters(dbClient, dbSession);
199 return new RulesRegistrationContext(allRules, existingDeprecatedKeysById, ruleParamsByRuleUuid);
202 private static Map<String, List<RuleParamDto>> loadAllRuleParameters(DbClient dbClient, DbSession dbSession) {
203 return dbClient.ruleDao().selectAllRuleParams(dbSession).stream()
204 .collect(Collectors.groupingBy(RuleParamDto::getRuleUuid));
207 private static Map<String, Set<SingleDeprecatedRuleKey>> loadDeprecatedRuleKeys(DbClient dbClient, DbSession dbSession) {
208 return dbClient.ruleDao().selectAllDeprecatedRuleKeys(dbSession).stream()
209 .map(SingleDeprecatedRuleKey::from)
210 .collect(Collectors.groupingBy(SingleDeprecatedRuleKey::getRuleUuid, Collectors.toSet()));