|
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.get(environmentName); |
|
44
|
|
|
Set<String> supportedSpaces = ImmutableSet.<String>builder() |
|
45
|
|
|
.addAll(environment.getSpaceNames()) |
|
46
|
|
|
.add(space) |
|
47
|
|
|
.build(); |
|
48
|
|
|
|
|
49
|
|
|
Set<Space> spaces = new HashSet<>(); |
|
50
|
|
|
for (String spaceName : supportedSpaces) { |
|
51
|
|
|
if (space.equals(spaceName)) { |
|
52
|
|
|
spaces.add(new Space(spaceName, serviceIds)); |
|
53
|
|
|
} else { |
|
54
|
|
|
spaces.add(new Space(spaceName, environment.getServices(spaceName))); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
environmentRepository.persist(new Environment(environmentName, spaces)); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public Collection<EnvironmentValidatorResult> validateServiceAgainstEnvironments( |
|
62
|
|
|
ServiceId serviceId, |
|
63
|
|
|
List<String> environments) { |
|
64
|
|
|
|
|
65
|
|
|
ServiceContracts validatedServiceContracts = this.serviceContractsRepository.findOne(serviceId) |
|
66
|
|
|
.orElseThrow(ResourceNotFoundException::new); |
|
67
|
|
|
|
|
68
|
|
|
return this.validators.stream() |
|
69
|
|
|
.map(validator -> |
|
70
|
|
|
validateServiceAgainstEnvironments( |
|
71
|
|
|
validatedServiceContracts, |
|
72
|
|
|
environments, |
|
73
|
|
|
validator |
|
74
|
|
|
)) |
|
75
|
|
|
.collect(toList()); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public Multimap<ServiceId, EnvironmentValidatorResult> validatedServicesAgainstEnvironment( |
|
79
|
|
|
List<ServiceId> serviceIds, |
|
80
|
|
|
String environment) { |
|
81
|
|
|
|
|
82
|
|
|
List<ServiceContracts> validatedServiceContracts = serviceIds.stream() |
|
83
|
|
|
.map(serviceContractsRepository::findOne) |
|
84
|
|
|
.map(o -> o.orElseThrow(RequestValidationException::new)) |
|
85
|
|
|
.collect(toList()); |
|
86
|
|
|
|
|
87
|
|
|
Multimap<ServiceId, EnvironmentValidatorResult> validationResults = HashMultimap.create(); |
|
88
|
|
|
this.validators |
|
89
|
|
|
.forEach(validator -> |
|
90
|
|
|
validatedServicesAgainstEnvironment( |
|
91
|
|
|
validatedServiceContracts, |
|
92
|
|
|
environment, |
|
93
|
|
|
validator |
|
94
|
|
|
) |
|
95
|
|
|
.forEach(validationResults::put) |
|
96
|
|
|
); |
|
97
|
|
|
|
|
98
|
|
|
return validationResults; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
private <C, E> EnvironmentValidatorResult validateServiceAgainstEnvironments( |
|
102
|
|
|
ServiceContracts contractsToValidate, |
|
103
|
|
|
List<String> environments, |
|
104
|
|
|
InterfaceContractValidator<C, E> validator |
|
105
|
|
|
) { |
|
106
|
|
|
List<ServiceContracts> environmentContracts = environments.stream() |
|
107
|
|
|
.flatMap(env -> this.environmentRepository.get(env).getAllServices().stream()) |
|
108
|
|
|
.map(service -> this.serviceContractsRepository.findOne(new ServiceId(service.getName(), service.getVersion()))) |
|
109
|
|
|
.filter(Optional::isPresent) |
|
110
|
|
|
.map(Optional::get) |
|
111
|
|
|
.collect(toList()); |
|
112
|
|
|
|
|
113
|
|
|
return getValidatorResult(contractsToValidate, environmentContracts, validator); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
private <C, E> Map<ServiceId, EnvironmentValidatorResult> validatedServicesAgainstEnvironment( |
|
117
|
|
|
List<ServiceContracts> contractToValidate, |
|
118
|
|
|
String env, |
|
119
|
|
|
InterfaceContractValidator<C, E> validator |
|
120
|
|
|
){ |
|
121
|
|
|
// find contracts on given env |
|
122
|
|
|
List<ServiceContracts> environmentContracts = this.environmentRepository.get(env).getAllServices() |
|
123
|
|
|
.stream() |
|
124
|
|
|
.map(service -> this.serviceContractsRepository.findOne(new ServiceId(service.getName(), service.getVersion()))) |
|
125
|
|
|
.filter(Optional::isPresent) |
|
126
|
|
|
.map(Optional::get) |
|
127
|
|
|
.collect(toList()); |
|
128
|
|
|
|
|
129
|
|
|
log.info("checking how " + |
|
130
|
|
|
"["+ Joiner.on(", ").join(contractToValidate.stream().map(sc -> sc.getId().toString()).collect(toList()))+"] will impact the env " + |
|
131
|
|
|
"["+ Joiner.on(", ").join(environmentContracts.stream().map(sc -> sc.getId().toString()).collect(toList()))+"]"); |
|
132
|
|
|
|
|
133
|
|
|
// replace environment contracts with validated ones |
|
134
|
|
|
contractToValidate.forEach(validatedContract -> { |
|
135
|
|
|
environmentContracts.removeIf(serviceContracts -> serviceContracts.getName().equals(validatedContract.getId().getName())); |
|
136
|
|
|
environmentContracts.add(validatedContract); |
|
137
|
|
|
}); |
|
138
|
|
|
|
|
139
|
|
|
log.info("after the deployment env will contain: ["+Joiner.on(", ").join(environmentContracts.stream().map(sc -> sc.getId().toString()).collect(toList()))+"]"); |
|
140
|
|
|
|
|
141
|
|
|
Map<ServiceId, EnvironmentValidatorResult> hashMap = Maps.newHashMap(); |
|
142
|
|
|
for (ServiceContracts sc : contractToValidate) { |
|
143
|
|
|
hashMap.put(sc.getId(), getValidatorResult(sc, environmentContracts, validator)); |
|
144
|
|
|
} |
|
145
|
|
|
return hashMap; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|