Search in sources :

Example 1 with MessageError

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MessageError(org.openkilda.messaging.error.MessageError) MDCCloseable(org.slf4j.MDC.MDCCloseable) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 2 with MessageError

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);
}
Also used : MessageError(org.openkilda.messaging.error.MessageError)

Example 3 with MessageError

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);
}
Also used : MessageError(org.openkilda.messaging.error.MessageError) MvcResult(org.springframework.test.web.servlet.MvcResult) WithMockUser(org.springframework.security.test.context.support.WithMockUser) Test(org.junit.Test)

Example 4 with MessageError

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));
}
Also used : MessageError(org.openkilda.messaging.error.MessageError)

Example 5 with MessageError

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");
}
Also used : IOFSwitch(net.floodlightcontroller.core.IOFSwitch) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) MessageError(org.openkilda.messaging.error.MessageError) DatapathId(org.projectfloodlight.openflow.types.DatapathId) IOException(java.io.IOException) SwitchNotFoundException(org.openkilda.floodlight.error.SwitchNotFoundException) NoviBfdSession(org.openkilda.messaging.model.NoviBfdSession) OFPacketOut(org.projectfloodlight.openflow.protocol.OFPacketOut) Post(org.restlet.resource.Post) Put(org.restlet.resource.Put)

Aggregations

MessageError (org.openkilda.messaging.error.MessageError)14 Test (org.junit.Test)4 ISwitchManager (org.openkilda.floodlight.switchmanager.ISwitchManager)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Get (org.restlet.resource.Get)2 Post (org.restlet.resource.Post)2 Put (org.restlet.resource.Put)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpStatus (org.springframework.http.HttpStatus)2 WithMockUser (org.springframework.security.test.context.support.WithMockUser)2 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)2 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)2 IOFSwitch (net.floodlightcontroller.core.IOFSwitch)1 SwitchNotFoundException (org.openkilda.floodlight.error.SwitchNotFoundException)1 SwitchOperationException (org.openkilda.floodlight.error.SwitchOperationException)1 UnsupportedSwitchOperationException (org.openkilda.floodlight.error.UnsupportedSwitchOperationException)1 Message (org.openkilda.messaging.Message)1 CommandData (org.openkilda.messaging.command.CommandData)1