Search in sources :

Example 6 with MessageError

use of org.openkilda.messaging.error.MessageError in project open-kilda by telstra.

the class MetersResource method getMeters.

// FIXME(surabujin): is it used anywhere?
@Get("json")
@SuppressWarnings("unchecked")
public Map<Long, Object> getMeters() {
    Map<Long, Object> response = new HashMap<>();
    String switchId = (String) this.getRequestAttributes().get("switch_id");
    logger.debug("Get meters for switch: {}", switchId);
    ISwitchManager switchManager = (ISwitchManager) getContext().getAttributes().get(ISwitchManager.class.getCanonicalName());
    try {
        OFMeterConfigStatsReply replay = switchManager.dumpMeters(DatapathId.of(switchId));
        logger.debug("Meters from switch {} received: {}", switchId, replay);
        if (replay != null) {
            for (OFMeterConfig entry : replay.getEntries()) {
                response.put(entry.getMeterId(), entry);
            }
        }
    } catch (IllegalArgumentException | SwitchOperationException exception) {
        String messageString = "No such switch";
        logger.error("{}: {}", messageString, switchId, exception);
        MessageError responseMessage = new MessageError(DEFAULT_CORRELATION_ID, System.currentTimeMillis(), ErrorType.PARAMETERS_INVALID.toString(), messageString, exception.getMessage());
        response.putAll(MAPPER.convertValue(responseMessage, Map.class));
    }
    return response;
}
Also used : SwitchOperationException(org.openkilda.floodlight.switchmanager.SwitchOperationException) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) OFMeterConfigStatsReply(org.projectfloodlight.openflow.protocol.OFMeterConfigStatsReply) HashMap(java.util.HashMap) MessageError(org.openkilda.messaging.error.MessageError) OFMeterConfig(org.projectfloodlight.openflow.protocol.OFMeterConfig) Get(org.restlet.resource.Get)

Example 7 with MessageError

use of org.openkilda.messaging.error.MessageError in project open-kilda by telstra.

the class TopologyBasicAuthenticationEntryPoint method commence.

/**
 * {@inheritDoc}
 */
@Override
public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
    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);
    MessageError error = new MessageError(request.getHeader(CORRELATION_ID), System.currentTimeMillis(), HttpStatus.UNAUTHORIZED.value(), HttpStatus.UNAUTHORIZED.getReasonPhrase(), ErrorType.AUTH_FAILED.toString(), exception.getClass().getSimpleName());
    response.getWriter().print(MAPPER.writeValueAsString(error));
}
Also used : MessageError(org.openkilda.messaging.error.MessageError)

Example 8 with MessageError

use of org.openkilda.messaging.error.MessageError in project open-kilda by telstra.

the class TopologyExceptionHandler method handleMessageException.

/**
 * Handles MessageException exception.
 *
 * @param exception the MessageException 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:
        case PARAMETERS_INVALID:
            status = HttpStatus.BAD_REQUEST;
            break;
        case ALREADY_EXISTS:
            status = HttpStatus.CONFLICT;
            break;
        case AUTH_FAILED:
            status = HttpStatus.UNAUTHORIZED;
            break;
        case OPERATION_TIMED_OUT:
        case INTERNAL_ERROR:
        default:
            status = HttpStatus.INTERNAL_SERVER_ERROR;
            break;
    }
    MessageError error = new MessageError(request.getHeader(CORRELATION_ID), exception.getTimestamp(), status.value(), status.getReasonPhrase(), exception.getMessage(), exception.getClass().getSimpleName());
    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) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 9 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("/flows/{flow-id}", ERROR_FLOW_ID).header(CORRELATION_ID, DEFAULT_CORRELATION_ID).contentType(APPLICATION_JSON_VALUE)).andExpect(status().isNotFound()).andExpect(content().contentType(APPLICATION_JSON_UTF8_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 10 with MessageError

use of org.openkilda.messaging.error.MessageError in project open-kilda by telstra.

the class FlowControllerTest method emptyCredentials.

@Test
public void emptyCredentials() throws Exception {
    MvcResult result = mockMvc.perform(get("/flows/path/{flow-id}", TestMessageMock.FLOW_ID).header(CORRELATION_ID, DEFAULT_CORRELATION_ID).contentType(APPLICATION_JSON_VALUE)).andExpect(status().isUnauthorized()).andExpect(content().contentType(APPLICATION_JSON_UTF8_VALUE)).andReturn();
    MessageError response = MAPPER.readValue(result.getResponse().getContentAsString(), MessageError.class);
    assertEquals(AUTH_ERROR, response);
}
Also used : MessageError(org.openkilda.messaging.error.MessageError) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.Test)

Aggregations

MessageError (org.openkilda.messaging.error.MessageError)10 ISwitchManager (org.openkilda.floodlight.switchmanager.ISwitchManager)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 Get (org.restlet.resource.Get)2 HttpHeaders (org.springframework.http.HttpHeaders)2 HttpStatus (org.springframework.http.HttpStatus)2 MvcResult (org.springframework.test.web.servlet.MvcResult)2 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)2 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)2 IOException (java.io.IOException)1 SwitchOperationException (org.openkilda.floodlight.switchmanager.SwitchOperationException)1 Message (org.openkilda.messaging.Message)1 CommandData (org.openkilda.messaging.command.CommandData)1 CommandMessage (org.openkilda.messaging.command.CommandMessage)1 BaseInstallFlow (org.openkilda.messaging.command.flow.BaseInstallFlow)1 OFFlowStatsEntry (org.projectfloodlight.openflow.protocol.OFFlowStatsEntry)1 OFFlowStatsReply (org.projectfloodlight.openflow.protocol.OFFlowStatsReply)1 OFMeterConfig (org.projectfloodlight.openflow.protocol.OFMeterConfig)1 OFMeterConfigStatsReply (org.projectfloodlight.openflow.protocol.OFMeterConfigStatsReply)1