Open Kilda Java Documentation
FlowsIntegrationService.java
Go to the documentation of this file.
1 package org.openkilda.integration.service;
2 
3 import java.util.List;
4 
5 import org.apache.http.HttpResponse;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21 import org.springframework.beans.factory.annotation.Autowired;
22 import org.springframework.http.HttpMethod;
23 import org.springframework.stereotype.Service;
24 
25 import com.fasterxml.jackson.core.JsonProcessingException;
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 
33 @Service
35 
36 
37  private static final Logger LOGGER = LoggerFactory.getLogger(FlowsIntegrationService.class);
38 
39 
40  @Autowired
41  private RestClientManager restClientManager;
42 
43  @Autowired
44  FlowPathConverter flowPathConverter;
45 
46  @Autowired
47  FlowConverter flowConverter;
48 
49  @Autowired
50  private ApplicationProperties applicationProperties;
51 
52  @Autowired
53  private ApplicationService applicationService;
54 
55  @Autowired
56  private ObjectMapper objectMapper;
57 
58 
65  public List<FlowInfo> getFlows() {
66 
67  List<Flow> flowList = getAllFlowList();
68  if (flowList != null) {
69  return flowConverter.toFlowsInfo(flowList);
70  }
71  return null;
72  }
73 
74 
82  public FlowStatus getFlowStatusById(final String flowId) throws IntegrationException {
83  HttpResponse response =
84  restClientManager.invoke(applicationProperties.getFlowStatus() + flowId,
85  HttpMethod.GET, "", "", applicationService.getAuthHeader());
86  if (RestClientManager.isValidResponse(response)) {
87  return restClientManager.getResponse(response, FlowStatus.class);
88  }
89  return null;
90  }
91 
92 
99  public FlowPayload getFlowPath(final String flowId) {
100  try {
101  HttpResponse response = restClientManager.invoke(
102  applicationProperties.getFlowPath().replace("{flow_id}", flowId),
103  HttpMethod.GET, "", "", applicationService.getAuthHeader());
104  if (RestClientManager.isValidResponse(response)) {
105  FlowPayload flowPayload =
106  restClientManager.getResponse(response, FlowPayload.class);
107  return flowPathConverter.getFlowPath(flowId, flowPayload);
108  } else {
109  String content = IoUtil.toString(response.getEntity().getContent());
110  throw new InvalidResponseException(response.getStatusLine().getStatusCode(),
111  content);
112  }
113 
114  } catch (Exception exception) {
115  LOGGER.error("Exception in getFlowPaths " + exception.getMessage());
116  throw new IntegrationException(exception);
117  }
118  }
119 
126  public List<Flow> getAllFlowList() {
127  try {
128  HttpResponse response = restClientManager.invoke(applicationProperties.getFlows(),
129  HttpMethod.GET, "", "", applicationService.getAuthHeader());
130  if (RestClientManager.isValidResponse(response)) {
131  return restClientManager.getResponseList(response, Flow.class);
132  }
133  } catch (Exception exception) {
134  LOGGER.error("Exception in getAllFlowList " + exception.getMessage());
135  throw new IntegrationException(exception);
136  }
137  return null;
138  }
139 
146  public FlowPath rerouteFlow(String flowId) {
147  try {
148  HttpResponse response = restClientManager.invoke(
149  applicationProperties.getFlowReroute().replace("{flow_id}", flowId),
150  HttpMethod.PATCH, "", "", applicationService.getAuthHeader());
151  if (RestClientManager.isValidResponse(response)) {
152  FlowPath flowPath = restClientManager.getResponse(response, FlowPath.class);
153  return flowPath;
154  } else {
155  String content = IoUtil.toString(response.getEntity().getContent());
156  throw new InvalidResponseException(response.getStatusLine().getStatusCode(),
157  content);
158  }
159  } catch (Exception exception) {
160  LOGGER.error("Exception in rerouteFlow " + exception.getMessage());
161  throw new IntegrationException(exception);
162  }
163  }
164 
171  public String validateFlow(String flowId) {
172  try {
173  HttpResponse response = restClientManager.invoke(
174  applicationProperties.getFlowValidate().replace("{flow_id}", flowId),
175  HttpMethod.GET, "", "", applicationService.getAuthHeader());
176  return IoUtil.toString(response.getEntity().getContent());
177  } catch (Exception exception) {
178  LOGGER.error("Exception in validateFlow " + exception.getMessage());
179  throw new IntegrationException(exception);
180  }
181  }
182 
189  public Flow getFlowById(String flowId) {
190  try {
191  HttpResponse response =
192  restClientManager.invoke(applicationProperties.getFlows() + "/" + flowId,
193  HttpMethod.GET, "", "", applicationService.getAuthHeader());
194  if (RestClientManager.isValidResponse(response)) {
195  Flow flow = restClientManager.getResponse(response, Flow.class);
196  return flowConverter.toFlowWithSwitchNames(flow);
197  }
198  } catch (Exception exception) {
199  LOGGER.error("Exception in getFlowById " + exception.getMessage());
200  throw new IntegrationException(exception);
201  }
202  return null;
203  }
204 
211  public Flow createFlow(Flow flow){
212  try {
213  HttpResponse response = restClientManager.invoke(applicationProperties.getFlows(),
214  HttpMethod.PUT, objectMapper.writeValueAsString(flow), "application/json",
215  applicationService.getAuthHeader());
216  if (RestClientManager.isValidResponse(response)) {
217  return restClientManager.getResponse(response, Flow.class);
218  }
219  } catch (JsonProcessingException e) {
220  LOGGER.error("Inside createFlow Exception :", e);
221  throw new IntegrationException(e);
222  }
223  return null;
224  }
225 
233  public Flow updateFlow(String flowId, Flow flow){
234  try {
235  HttpResponse response = restClientManager.invoke(applicationProperties.getUpdateFlow().replace("{flow_id}", flowId),
236  HttpMethod.PUT, objectMapper.writeValueAsString(flow), "application/json",
237  applicationService.getAuthHeader());
238  if (RestClientManager.isValidResponse(response)) {
239  return restClientManager.getResponse(response, Flow.class);
240  }
241  } catch (JsonProcessingException e) {
242  LOGGER.error("Inside updateFlow Exception :", e);
243  throw new IntegrationException(e);
244  }
245  return null;
246  }
247 
255  public Flow deleteFlow(String flowId) {
256  HttpResponse response = restClientManager.invoke(
257  applicationProperties.getUpdateFlow().replace("{flow_id}", flowId),
258  HttpMethod.DELETE, "", "application/json", applicationService.getAuthHeader());
259  if (RestClientManager.isValidResponse(response)) {
260  return restClientManager.getResponse(response, Flow.class);
261  }
262  return null;
263  }
264 }
static boolean isValidResponse(final HttpResponse response)
static String toString(final InputStream inputStream)
Definition: IoUtil.java:29