use of org.openkilda.messaging.error.MessageError in project open-kilda by telstra.
the class NorthboundExceptionHandler method handleMessageException.
/**
* Handles NorthboundException exception.
*
* @param exception the NorthboundException instance
* @param request the WebRequest caused exception
* @return the ResponseEntity object instance
*/
@ExceptionHandler(MessageException.class)
protected ResponseEntity<Object> handleMessageException(MessageException exception, WebRequest request) {
HttpStatus status;
switch(exception.getErrorType()) {
case NOT_FOUND:
status = HttpStatus.NOT_FOUND;
break;
case DATA_INVALID:
status = HttpStatus.BAD_REQUEST;
break;
case PARAMETERS_INVALID:
status = HttpStatus.BAD_REQUEST;
break;
case REQUEST_INVALID:
status = HttpStatus.BAD_REQUEST;
break;
case OPERATION_TIMED_OUT:
status = HttpStatus.REQUEST_TIMEOUT;
break;
case ALREADY_EXISTS:
status = HttpStatus.CONFLICT;
break;
case AUTH_FAILED:
status = HttpStatus.UNAUTHORIZED;
break;
case UNPROCESSABLE_REQUEST:
status = HttpStatus.UNPROCESSABLE_ENTITY;
break;
case INTERNAL_ERROR:
status = HttpStatus.INTERNAL_SERVER_ERROR;
break;
case NOT_PERMITTED:
status = HttpStatus.FORBIDDEN;
break;
case NOT_IMPLEMENTED:
status = HttpStatus.NOT_IMPLEMENTED;
break;
default:
status = HttpStatus.INTERNAL_SERVER_ERROR;
break;
}
MessageError error = new MessageError(exception.getCorrelationId(), exception.getTimestamp(), exception.getErrorType().toString(), exception.getMessage(), exception.getErrorDescription());
try (MDCCloseable mdcCloseable = MDC.putCloseable(CORRELATION_ID, exception.getCorrelationId())) {
logger.warn(format("Error %s caught.", error), exception);
}
return super.handleExceptionInternal(exception, error, new HttpHeaders(), status, request);
}
use of org.openkilda.messaging.error.MessageError in project open-kilda by telstra.
the class NorthboundExceptionHandler method handleExceptionInternal.
@Override
protected ResponseEntity<Object> handleExceptionInternal(Exception exception, Object body, HttpHeaders headers, HttpStatus status, WebRequest request) {
String correlationId = Optional.ofNullable(request.getHeader(CORRELATION_ID)).orElse(DEFAULT_CORRELATION_ID);
MessageError error = new MessageError(correlationId, System.currentTimeMillis(), ErrorType.REQUEST_INVALID.toString(), exception.getMessage(), exception.getClass().getSimpleName());
logger.error(format("Unknown error %s caught.", error), exception);
return super.handleExceptionInternal(exception, error, headers, status, request);
}
use of org.openkilda.messaging.error.MessageError in project open-kilda by telstra.
the class FlowControllerTest method getNonExistingFlow.
@Test
@WithMockUser(username = USERNAME, password = PASSWORD, roles = ROLE)
public void getNonExistingFlow() throws Exception {
MvcResult result = mockMvc.perform(get("/v1/flows/{flow-id}", ERROR_FLOW_ID).header(CORRELATION_ID, DEFAULT_CORRELATION_ID).contentType(APPLICATION_JSON_VALUE)).andExpect(status().isNotFound()).andExpect(content().contentType(APPLICATION_JSON_VALUE)).andReturn();
MessageError response = MAPPER.readValue(result.getResponse().getContentAsString(), MessageError.class);
assertEquals(NOT_FOUND_ERROR, response);
}
use of org.openkilda.messaging.error.MessageError in project open-kilda by telstra.
the class NorthboundBasicAuthenticationEntryPoint method commence.
/**
* {@inheritDoc}
*/
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException {
String realm = String.format("Basic realm=%s", getRealmName());
response.addHeader(HttpHeaders.WWW_AUTHENTICATE, realm);
response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
String correlationId = Optional.ofNullable(request.getHeader(CORRELATION_ID)).orElse(DEFAULT_CORRELATION_ID);
MessageError error = new MessageError(correlationId, System.currentTimeMillis(), ErrorType.AUTH_FAILED.toString(), DEFAULT_REALM, exception.getClass().getSimpleName());
response.getWriter().print(MAPPER.writeValueAsString(error));
}
use of org.openkilda.messaging.error.MessageError in project open-kilda by telstra.
the class EnableBfdResource method enableBfd.
/**
* Setting up BFD session.
*
* @param json the json from request.
* @return json response.
* @throws JsonProcessingException if response can't be wrote to string.
*/
@Post("json")
@Put("json")
public String enableBfd(String json) {
ISwitchManager switchManager = (ISwitchManager) getContext().getAttributes().get(ISwitchManager.class.getCanonicalName());
NoviBfdSession request;
try {
request = MAPPER.readValue(json, NoviBfdSession.class);
if (request.getIntervalMs() < CONSTRAINT_INTERVAL_MIN) {
throw new IllegalArgumentException(String.format("Invalid bfd session interval value: %d < %d", request.getIntervalMs(), CONSTRAINT_INTERVAL_MIN));
}
DatapathId datapathIdtarget = DatapathId.of(request.getTarget().getDatapath().toLong());
IOFSwitch iofSwitch = switchManager.lookupSwitch(datapathIdtarget);
OFPacketOut outPacket = makeSessionConfigMessage(request, iofSwitch, switchManager);
if (!iofSwitch.write(outPacket)) {
throw new IllegalStateException("Failed to set up BFD session");
}
} catch (IOException e) {
logger.error("Message received is not valid BFD Request: {}", json);
MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, now(), ErrorType.DATA_INVALID.toString(), "Message received is not valid BFD Request", e.getMessage());
return generateJson(responseMessage);
} catch (SwitchNotFoundException e) {
MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, now(), ErrorType.DATA_INVALID.toString(), "Switch not found", e.getMessage());
return generateJson(responseMessage);
}
return generateJson("ok");
}
Aggregations