Search in sources :

Example 11 with MessageError

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

the class FlowsResource method getFlows.

/**
 * The method returns the flow for the switch from the request.
 */
@Get("json")
@SuppressWarnings("unchecked")
public Map<String, Object> getFlows() throws SwitchOperationException {
    Map<String, Object> response = new HashMap<>();
    String switchId = (String) this.getRequestAttributes().get("switch_id");
    LOGGER.debug("Get flows for switch: {}", switchId);
    ISwitchManager switchManager = (ISwitchManager) getContext().getAttributes().get(ISwitchManager.class.getCanonicalName());
    try {
        List<OFFlowStatsEntry> flowEntries = switchManager.dumpFlowTable(DatapathId.of(switchId));
        LOGGER.debug("OF_STATS: {}", flowEntries);
        if (flowEntries != null) {
            for (OFFlowStatsEntry entry : flowEntries) {
                String key = String.format("flow-0x%s", Long.toHexString(entry.getCookie().getValue()).toUpperCase());
                response.put(key, buildFlowStat(entry));
            }
        }
    } catch (IllegalArgumentException exception) {
        String messageString = "No such switch";
        LOGGER.error("{}: {}", messageString, switchId, exception);
        MessageError responseMessage = new MessageError(CorrelationContext.getId(), System.currentTimeMillis(), ErrorType.PARAMETERS_INVALID.toString(), messageString, exception.getMessage());
        response.putAll(MAPPER.convertValue(responseMessage, Map.class));
    }
    return response;
}
Also used : OFFlowStatsEntry(org.projectfloodlight.openflow.protocol.OFFlowStatsEntry) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) HashMap(java.util.HashMap) MessageError(org.openkilda.messaging.error.MessageError) Get(org.restlet.resource.Get)

Example 12 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?
/**
 * Gets meters.
 * @return the map of meters.
 */
@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 {
        List<OFMeterConfig> meters = switchManager.dumpMeters(DatapathId.of(switchId));
        if (meters != null) {
            logger.debug("Meters from switch {} received: {}", switchId, meters.size());
            for (OFMeterConfig entry : meters) {
                response.put(entry.getMeterId(), entry);
            }
        }
    } catch (UnsupportedSwitchOperationException ex) {
        String messageString = "Not supported";
        logger.error("{}: {}", messageString, switchId, ex);
        MessageError responseMessage = new MessageError(CorrelationContext.getId(), System.currentTimeMillis(), ErrorType.PARAMETERS_INVALID.toString(), messageString, ex.getMessage());
        response.putAll(MAPPER.convertValue(responseMessage, Map.class));
        getResponse().setStatus(Status.SERVER_ERROR_NOT_IMPLEMENTED);
    } catch (IllegalArgumentException | SwitchOperationException exception) {
        String messageString = "No such switch";
        logger.error("{}: {}", messageString, switchId, exception);
        MessageError responseMessage = new MessageError(CorrelationContext.getId(), System.currentTimeMillis(), ErrorType.PARAMETERS_INVALID.toString(), messageString, exception.getMessage());
        response.putAll(MAPPER.convertValue(responseMessage, Map.class));
        getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
    }
    return response;
}
Also used : SwitchOperationException(org.openkilda.floodlight.error.SwitchOperationException) UnsupportedSwitchOperationException(org.openkilda.floodlight.error.UnsupportedSwitchOperationException) ISwitchManager(org.openkilda.floodlight.switchmanager.ISwitchManager) HashMap(java.util.HashMap) UnsupportedSwitchOperationException(org.openkilda.floodlight.error.UnsupportedSwitchOperationException) MessageError(org.openkilda.messaging.error.MessageError) OFMeterConfig(org.projectfloodlight.openflow.protocol.OFMeterConfig) Get(org.restlet.resource.Get)

Example 13 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("/v1/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)

Example 14 with MessageError

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

the class GrpcBasicAuthenticationEntryPoint 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)

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