16 package org.openkilda.testing.service.northbound;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.beans.factory.annotation.Qualifier;
45 import org.springframework.http.HttpEntity;
46 import org.springframework.http.HttpHeaders;
47 import org.springframework.http.HttpMethod;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.stereotype.Service;
50 import org.springframework.web.client.HttpClientErrorException;
51 import org.springframework.web.client.RestTemplate;
52 import org.springframework.web.util.UriComponentsBuilder;
54 import java.util.Arrays;
55 import java.util.List;
56 import java.util.stream.Collectors;
57 import java.util.stream.Stream;
64 private static final String KILDA_CONTROLLER =
"kilda";
67 @Qualifier(
"northboundRestTemplate")
68 private RestTemplate restTemplate;
72 return restTemplate.exchange(
"/api/v1/health-check", HttpMethod.GET,
73 new HttpEntity(buildHeadersWithCorrelationId()),
HealthCheck.class).getBody();
79 return restTemplate.exchange(
"/api/v1/flows/{flow_id}", HttpMethod.GET,
80 new HttpEntity(buildHeadersWithCorrelationId()),
FlowPayload.class, flowId).getBody();
81 }
catch (HttpClientErrorException ex) {
82 if (ex.getStatusCode() != HttpStatus.NOT_FOUND) {
92 HttpEntity<FlowPayload> httpEntity =
new HttpEntity<>(payload, buildHeadersWithCorrelationId());
94 return restTemplate.exchange(
"/api/v1/flows", HttpMethod.PUT, httpEntity,
FlowPayload.class).getBody();
99 HttpEntity<FlowPayload> httpEntity =
new HttpEntity<>(payload, buildHeadersWithCorrelationId());
101 return restTemplate.exchange(
"/api/v1/flows/{flow_id}", HttpMethod.PUT, httpEntity,
108 return restTemplate.exchange(
"/api/v1/flows/{flow_id}", HttpMethod.DELETE,
109 new HttpEntity(buildHeadersWithCorrelationId()),
FlowPayload.class, flowId).getBody();
110 }
catch (HttpClientErrorException ex) {
111 if (ex.getStatusCode() != HttpStatus.NOT_FOUND) {
121 HttpHeaders httpHeaders = buildHeadersWithCorrelationId();
122 httpHeaders.set(
Utils.
EXTRA_AUTH, String.valueOf(System.currentTimeMillis()));
123 FlowPayload[] deletedFlows = restTemplate.exchange(
"/api/v1/flows", HttpMethod.DELETE,
124 new HttpEntity(httpHeaders),
FlowPayload[].
class).getBody();
125 return Arrays.asList(deletedFlows);
130 return restTemplate.exchange(
"/api/v1/flows/{flow_id}/path/", HttpMethod.GET,
131 new HttpEntity(buildHeadersWithCorrelationId()),
FlowPathPayload.class, flowId).getBody();
137 return restTemplate.exchange(
"/api/v1/flows/status/{flow_id}", HttpMethod.GET,
138 new HttpEntity(buildHeadersWithCorrelationId()),
FlowIdStatusPayload.class, flowId).getBody();
139 }
catch (HttpClientErrorException ex) {
140 if (ex.getStatusCode() != HttpStatus.NOT_FOUND) {
150 FlowPayload[] flows = restTemplate.exchange(
"/api/v1/flows", HttpMethod.GET,
151 new HttpEntity(buildHeadersWithCorrelationId()),
FlowPayload[].
class).getBody();
152 return Arrays.asList(flows);
157 HttpHeaders httpHeaders = buildHeadersWithCorrelationId();
158 httpHeaders.set(
Utils.
EXTRA_AUTH, String.valueOf(System.currentTimeMillis()));
160 Long[] deletedRules = restTemplate.exchange(
161 "/api/v1/switches/{switch_id}/rules?delete-action=IGNORE_DEFAULTS", HttpMethod.DELETE,
162 new HttpEntity(httpHeaders), Long[].
class, switchId).getBody();
163 return Arrays.asList(deletedRules);
168 return restTemplate.exchange(
"/api/v1/switches/{switch_id}/rules/synchronize", HttpMethod.GET,
169 new HttpEntity(buildHeadersWithCorrelationId()),
RulesSyncResult.class, switchId).getBody();
174 FlowValidationDto[] flowValidations = restTemplate.exchange(
"/api/v1/flows/{flow_id}/validate", HttpMethod.GET,
175 new HttpEntity(buildHeadersWithCorrelationId()),
FlowValidationDto[].
class, flowId).getBody();
176 return Arrays.asList(flowValidations);
181 return restTemplate.exchange(
"/api/v1/flows/{flowId}/reroute", HttpMethod.PATCH,
182 new HttpEntity(buildHeadersWithCorrelationId()),
FlowReroutePayload.class, flowId).getBody();
187 return restTemplate.exchange(
"/api/v1/switches/{switch_id}/rules", HttpMethod.GET,
188 new HttpEntity(buildHeadersWithCorrelationId()),
SwitchFlowEntries.class, switchId).getBody();
193 return restTemplate.exchange(
"/api/v1/switches/{switch_id}/rules/validate", HttpMethod.GET,
199 LinkDto[] links = restTemplate.exchange(
"/api/v1/links", HttpMethod.GET,
200 new HttpEntity(buildHeadersWithCorrelationId()),
LinkDto[].
class).getBody();
202 return Stream.of(links)
203 .map(this::convertToIslInfoData)
204 .collect(Collectors.toList());
214 UriComponentsBuilder uriBuilder = UriComponentsBuilder.fromUriString(
"/api/v1/link/props");
215 if (srcSwitch != null) {
216 uriBuilder.queryParam(
"src_switch", srcSwitch);
218 if (srcPort != null) {
219 uriBuilder.queryParam(
"src_port", srcPort);
221 if (dstSwitch != null) {
222 uriBuilder.queryParam(
"dst_switch", dstSwitch);
224 if (dstPort != null) {
225 uriBuilder.queryParam(
"dst_port", dstPort);
227 LinkPropsDto[] linkProps = restTemplate.exchange(uriBuilder.build().toString(), HttpMethod.GET,
228 new HttpEntity(buildHeadersWithCorrelationId()),
LinkPropsDto[].class).getBody();
229 return Arrays.asList(linkProps);
234 HttpEntity<List<LinkPropsDto>> httpEntity =
new HttpEntity<>(keys, buildHeadersWithCorrelationId());
235 return restTemplate.exchange(
"/api/v1/link/props", HttpMethod.PUT, httpEntity,
241 HttpEntity<List<LinkPropsDto>> httpEntity =
new HttpEntity<>(keys, buildHeadersWithCorrelationId());
242 return restTemplate.exchange(
"/api/v1/link/props", HttpMethod.DELETE, httpEntity,
248 return restTemplate.exchange(
"/api/v1/features", HttpMethod.GET,
254 HttpEntity<FeatureTogglePayload> httpEntity =
new HttpEntity<>(request, buildHeadersWithCorrelationId());
255 return restTemplate.exchange(
"/api/v1/features", HttpMethod.POST, httpEntity,
261 SwitchDto[] switches = restTemplate.exchange(
"/api/v1/switches", HttpMethod.GET,
262 new HttpEntity(buildHeadersWithCorrelationId()),
SwitchDto[].
class).getBody();
263 return Stream.of(switches)
264 .map(this::convertToSwitchInfoData)
265 .collect(Collectors.toList());
270 return restTemplate.exchange(
"/api/v1/switches/{switch_id}/meter/{meter_id}", HttpMethod.DELETE,
271 new HttpEntity(buildHeadersWithCorrelationId()),
DeleteMeterResult.class, switchId, meterId).getBody();
274 private HttpHeaders buildHeadersWithCorrelationId() {
275 HttpHeaders
headers =
new HttpHeaders();
280 private IslInfoData convertToIslInfoData(
LinkDto dto) {
281 List<PathNode>
path = dto.getPath().stream()
282 .map(pathDto ->
new PathNode(
283 new SwitchId(pathDto.getSwitchId()),
286 pathDto.getSegLatency()))
287 .collect(Collectors.toList());
288 return new IslInfoData(0,
path, dto.getSpeed(), IslChangeType.from(dto.getState().toString()),
289 dto.getAvailableBandwidth());
292 private SwitchInfoData convertToSwitchInfoData(
SwitchDto dto) {
293 return new SwitchInfoData(
294 new SwitchId(dto.getSwitchId()),
295 SwitchState.from(dto.getState()),
298 dto.getDescription(),
FlowPathPayload getFlowPath(String flowId)
List< IslInfoData > getAllLinks()
List< LinkPropsDto > getAllLinkProps()
FeatureTogglePayload toggleFeature(FeatureTogglePayload request)
List< FlowPayload > getAllFlows()
RulesValidationResult validateSwitchRules(SwitchId switchId)
List< SwitchInfoData > getAllSwitches()
FeatureTogglePayload getFeatureToggles()
SwitchFlowEntries getSwitchRules(SwitchId switchId)
BatchResults deleteLinkProps(List< LinkPropsDto > keys)
List< LinkPropsDto > getLinkProps(SwitchId srcSwitch, Integer srcPort, SwitchId dstSwitch, Integer dstPort)
RulesSyncResult synchronizeSwitchRules(SwitchId switchId)
List< Long > deleteSwitchRules(SwitchId switchId)
List< FlowValidationDto > validateFlow(String flowId)
static final String CORRELATION_ID
static final String EXTRA_AUTH
FlowPayload getFlow(String flowId)
BatchResults updateLinkProps(List< LinkPropsDto > keys)
FlowPayload updateFlow(String flowId, FlowPayload payload)
HealthCheck getHealthCheck()
DeleteMeterResult deleteMeter(SwitchId switchId, Integer meterId)
FlowIdStatusPayload getFlowStatus(String flowId)
FlowPayload deleteFlow(String flowId)
List< FlowPayload > deleteAllFlows()
FlowPayload addFlow(FlowPayload payload)
FlowReroutePayload rerouteFlow(String flowId)