1
|
|
|
package com.hltech.judged.server.domain; |
2
|
|
|
|
3
|
|
|
import com.google.common.base.Joiner; |
4
|
|
|
import com.google.common.collect.HashMultimap; |
5
|
|
|
import com.google.common.collect.ImmutableSet; |
6
|
|
|
import com.google.common.collect.Maps; |
7
|
|
|
import com.google.common.collect.Multimap; |
8
|
|
|
import com.hltech.judged.server.domain.environment.Environment; |
9
|
|
|
import com.hltech.judged.server.domain.environment.EnvironmentRepository; |
10
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContracts; |
11
|
|
|
import com.hltech.judged.server.domain.contracts.ServiceContractsRepository; |
12
|
|
|
import com.hltech.judged.server.domain.environment.Space; |
13
|
|
|
import com.hltech.judged.server.domain.validation.EnvironmentValidatorResult; |
14
|
|
|
import com.hltech.judged.server.domain.validation.InterfaceContractValidator; |
15
|
|
|
import com.hltech.judged.server.interfaces.rest.RequestValidationException; |
16
|
|
|
import com.hltech.judged.server.interfaces.rest.ResourceNotFoundException; |
17
|
|
|
import lombok.RequiredArgsConstructor; |
18
|
|
|
import lombok.extern.slf4j.Slf4j; |
19
|
|
|
|
20
|
|
|
import java.util.Collection; |
21
|
|
|
import java.util.HashSet; |
22
|
|
|
import java.util.List; |
23
|
|
|
import java.util.Map; |
24
|
|
|
import java.util.Optional; |
25
|
|
|
import java.util.Set; |
26
|
|
|
|
27
|
|
|
import static com.google.common.base.MoreObjects.firstNonNull; |
28
|
|
|
import static com.hltech.judged.server.domain.environment.Environment.DEFAULT_NAMESPACE; |
29
|
|
|
import static com.hltech.judged.server.domain.validation.EnvironmentValidatorResult.getValidatorResult; |
30
|
|
|
import static java.util.stream.Collectors.toList; |
31
|
|
|
|
32
|
|
|
@Slf4j |
33
|
|
|
@RequiredArgsConstructor |
34
|
|
|
public class JudgeDApplicationService { |
35
|
|
|
|
36
|
|
|
private final EnvironmentRepository environmentRepository; |
37
|
|
|
private final ServiceContractsRepository serviceContractsRepository; |
38
|
|
|
private final List<InterfaceContractValidator<?, ?>> validators; |
39
|
|
|
|
40
|
|
|
public void overwriteEnvironment(String environmentName, String agentSpace, Set<ServiceId> serviceIds) { |
41
|
|
|
String space = firstNonNull(agentSpace, DEFAULT_NAMESPACE); |
42
|
|
|
|
43
|
|
|
Environment environment = environmentRepository.find(environmentName) |
44
|
|
|
.orElse(new Environment(environmentName, new HashSet<>())); |
45
|
|
|
|
46
|
|
|
Set<String> supportedSpaces = ImmutableSet.<String>builder() |
47
|
|
|
.addAll(environment.getSpaceNames()) |
48
|
|
|
.add(space) |
49
|
|
|
.build(); |
50
|
|
|
|
51
|
|
|
Set<Space> spaces = new HashSet<>(); |
52
|
|
|
for (String spaceName : supportedSpaces) { |
53
|
|
|
if (space.equals(spaceName)) { |
54
|
|
|
spaces.add(new Space(spaceName, serviceIds)); |
55
|
|
|
} else { |
56
|
|
|
spaces.add(new Space(spaceName, environment.getServices(spaceName))); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
environmentRepository.persist(new Environment(environmentName, spaces)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public Collection<EnvironmentValidatorResult> validateServiceAgainstEnvironments( |
64
|
|
|
ServiceId serviceId, |
65
|
|
|
List<String> environments) { |
66
|
|
|
|
67
|
|
|
ServiceContracts validatedServiceContracts = this.serviceContractsRepository.findOne(serviceId) |
68
|
|
|
.orElseThrow(ResourceNotFoundException::new); |
69
|
|
|
|
70
|
|
|
return this.validators.stream() |
71
|
|
|
.map(validator -> |
72
|
|
|
validateServiceAgainstEnvironments( |
73
|
|
|
validatedServiceContracts, |
74
|
|
|
environments, |
75
|
|
|
validator |
76
|
|
|
)) |
77
|
|
|
.collect(toList()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public Multimap<ServiceId, EnvironmentValidatorResult> validatedServicesAgainstEnvironment( |
81
|
|
|
List<ServiceId> serviceIds, |
82
|
|
|
String environment) { |
83
|
|
|
|
84
|
|
|
List<ServiceContracts> validatedServiceContracts = serviceIds.stream() |
85
|
|
|
.map(serviceContractsRepository::findOne) |
86
|
|
|
.map(o -> o.orElseThrow(RequestValidationException::new)) |
87
|
|
|
.collect(toList()); |
88
|
|
|
|
89
|
|
|
Multimap<ServiceId, EnvironmentValidatorResult> validationResults = HashMultimap.create(); |
90
|
|
|
this.validators |
91
|
|
|
.forEach(validator -> |
92
|
|
|
validatedServicesAgainstEnvironment( |
93
|
|
|
validatedServiceContracts, |
94
|
|
|
environment, |
95
|
|
|
validator |
96
|
|
|
) |
97
|
|
|
.forEach(validationResults::put) |
98
|
|
|
); |
99
|
|
|
|
100
|
|
|
return validationResults; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private <C, E> EnvironmentValidatorResult validateServiceAgainstEnvironments( |
104
|
|
|
ServiceContracts contractsToValidate, |
105
|
|
|
List<String> environments, |
106
|
|
|
InterfaceContractValidator<C, E> validator |
107
|
|
|
) { |
108
|
|
|
List<ServiceContracts> environmentContracts = environments.stream() |
109
|
|
|
.flatMap(envName -> getServicesIds(envName).stream()) |
110
|
|
|
.map(this.serviceContractsRepository::findOne) |
111
|
|
|
.filter(Optional::isPresent) |
112
|
|
|
.map(Optional::get) |
113
|
|
|
.collect(toList()); |
114
|
|
|
|
115
|
|
|
return getValidatorResult(contractsToValidate, environmentContracts, validator); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
private <C, E> Map<ServiceId, EnvironmentValidatorResult> validatedServicesAgainstEnvironment( |
119
|
|
|
List<ServiceContracts> contractToValidate, |
120
|
|
|
String env, |
121
|
|
|
InterfaceContractValidator<C, E> validator |
122
|
|
|
){ |
123
|
|
|
// find contracts on given env |
124
|
|
|
List<ServiceContracts> environmentContracts = getServicesIds(env).stream() |
125
|
|
|
.map(service -> this.serviceContractsRepository.findOne(new ServiceId(service.getName(), service.getVersion()))) |
126
|
|
|
.filter(Optional::isPresent) |
127
|
|
|
.map(Optional::get) |
128
|
|
|
.collect(toList()); |
129
|
|
|
|
130
|
|
|
log.info("checking how " + |
131
|
|
|
"["+ Joiner.on(", ").join(contractToValidate.stream().map(sc -> sc.getId().toString()).collect(toList()))+"] will impact the env " + |
132
|
|
|
"["+ Joiner.on(", ").join(environmentContracts.stream().map(sc -> sc.getId().toString()).collect(toList()))+"]"); |
133
|
|
|
|
134
|
|
|
// replace environment contracts with validated ones |
135
|
|
|
contractToValidate.forEach(validatedContract -> { |
136
|
|
|
environmentContracts.removeIf(serviceContracts -> serviceContracts.getName().equals(validatedContract.getId().getName())); |
137
|
|
|
environmentContracts.add(validatedContract); |
138
|
|
|
}); |
139
|
|
|
|
140
|
|
|
log.info("after the deployment env will contain: ["+Joiner.on(", ").join(environmentContracts.stream().map(sc -> sc.getId().toString()).collect(toList()))+"]"); |
141
|
|
|
|
142
|
|
|
Map<ServiceId, EnvironmentValidatorResult> hashMap = Maps.newHashMap(); |
143
|
|
|
for (ServiceContracts sc : contractToValidate) { |
144
|
|
|
hashMap.put(sc.getId(), getValidatorResult(sc, environmentContracts, validator)); |
145
|
|
|
} |
146
|
|
|
return hashMap; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private Set<ServiceId> getServicesIds(String environmentName) { |
150
|
|
|
var environment = this.environmentRepository.find(environmentName) |
151
|
|
|
.orElseThrow(ResourceNotFoundException::new); |
152
|
|
|
|
153
|
|
|
return environment.getAllServices(); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|