Search in sources :

Example 1 with FlowPayload

use of org.openkilda.messaging.payload.flow.FlowPayload in project open-kilda by telstra.

the class FlowServiceImplTest method updateFlow.

@Test
public void updateFlow() throws Exception {
    long updatedBandwidth = 20000L;
    FlowEndpointPayload firstEndpoint = new FlowEndpointPayload(srcSwitchId, 10, 100);
    FlowEndpointPayload secondEndpoint = new FlowEndpointPayload(dstSwitchId, 20, 100);
    FlowPayload flowPayload = new FlowPayload(flowId, 0L, secondEndpoint, firstEndpoint, 10000L, "", "", OutputVlanType.NONE);
    FlowPayload newFlowPayload = new FlowPayload(flowId, 0L, secondEndpoint, firstEndpoint, updatedBandwidth, "", "", OutputVlanType.NONE);
    flowService.createFlow(flowPayload, DEFAULT_CORRELATION_ID);
    flowService.updateFlow(newFlowPayload, DEFAULT_CORRELATION_ID);
    Set<Flow> flows = flowRepository.findByFlowId(flowId);
    assertNotNull(flows);
    assertFalse(flows.isEmpty());
    assertEquals(2, flows.size());
    for (Flow flow : flowRepository.findAll()) {
        assertEquals(flowId, flow.getFlowId());
        assertEquals(updatedBandwidth, flow.getBandwidth());
    }
}
Also used : FlowPayload(org.openkilda.messaging.payload.flow.FlowPayload) FlowEndpointPayload(org.openkilda.messaging.payload.flow.FlowEndpointPayload) Flow(org.openkilda.topology.domain.Flow) Test(org.junit.Test)

Example 2 with FlowPayload

use of org.openkilda.messaging.payload.flow.FlowPayload in project open-kilda by telstra.

the class FlowUtils method getFlow.

/**
 * Gets flow through Northbound service.
 *
 * @param flowId flow id
 * @return The JSON document of the specified flow
 */
public static FlowPayload getFlow(final String flowId) {
    System.out.println("\n==> Northbound Get Flow");
    long current = System.currentTimeMillis();
    Client client = clientFactory();
    Response response = client.target(northboundEndpoint).path("/api/v1/flows").path("{flowid}").resolveTemplate("flowid", flowId).request(MediaType.APPLICATION_JSON).header(HttpHeaders.AUTHORIZATION, authHeaderValue).header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis())).get();
    System.out.println(format("===> Response = %s", response.toString()));
    System.out.println(format("===> Northbound Get Flow Time: %,.3f", getTimeDuration(current)));
    int responseCode = response.getStatus();
    if (responseCode == 200) {
        FlowPayload flow = response.readEntity(FlowPayload.class);
        System.out.println(format("====> Northbound Get Flow = %s", flow));
        return flow;
    } else {
        System.out.println(format("====> Error: Northbound Get Flow = %s", response.readEntity(MessageError.class)));
        return null;
    }
}
Also used : Response(javax.ws.rs.core.Response) FlowPayload(org.openkilda.messaging.payload.flow.FlowPayload) Client(javax.ws.rs.client.Client) DefaultParameters.topologyEndpoint(org.openkilda.DefaultParameters.topologyEndpoint) DefaultParameters.northboundEndpoint(org.openkilda.DefaultParameters.northboundEndpoint)

Example 3 with FlowPayload

use of org.openkilda.messaging.payload.flow.FlowPayload in project open-kilda by telstra.

the class FlowUtils method cleanupFlows.

/**
 * Cleanups all flows.
 */
public static void cleanupFlows() {
    try {
        Set<String> flows = new HashSet<>();
        // TODO: This method started with getting counts and compariing, but that shouldn't be
        // the responsibility of this method given its name - cleanupFlows.
        // So, the TODO is to determine whether this code exists elsewhere in tests,
        // and if not, move it somewhere after, or part of, create test.
        // Get the flows through the NB API
        List<FlowPayload> nbFlows = getFlowDump();
        System.out.println(format("=====> Cleanup Flows, nbflow count = %d", nbFlows.size()));
        nbFlows.forEach(flow -> flows.add(flow.getId()));
        // Get the flows through the TE Rest API ... loop until the math works out.
        List<Flow> tpeFlows = new ArrayList<>();
        for (int i = 0; i < 10; ++i) {
            tpeFlows = dumpFlows();
            if (tpeFlows.size() == nbFlows.size() * 2) {
                tpeFlows.forEach(flow -> flows.add(flow.getFlowId()));
                break;
            }
            TimeUnit.SECONDS.sleep(2);
        }
        System.out.println(format("=====> Cleanup Flows, tpeFlows count = %d", tpeFlows.size()));
        // Delete all the flows
        flows.forEach(FlowUtils::deleteFlow);
        // Wait for them to become zero
        int nb_count = -1;
        int ter_count = -1;
        for (int i = 0; i < 10; ++i) {
            TimeUnit.SECONDS.sleep(2);
            nb_count = dumpFlows().size();
            ter_count = getFlowDump().size();
            if (nb_count == 0 && ter_count == 0) {
                break;
            }
        }
        assertEquals(0, nb_count);
        assertEquals(0, ter_count);
    // (crimi) - it is unclear why we are doing a count validation here .. it makes sense to do this
    // in the creation. But on cleanup, we just want things to be zero.
    // assertEquals(nbFlows.size() * 2, tpeFlows.size());
    // assertEquals(nbFlows.size(), flows.size());
    } catch (Exception exception) {
        System.out.println(format("Error during flow deletion: %s", exception.getMessage()));
        exception.printStackTrace();
    }
}
Also used : FlowPayload(org.openkilda.messaging.payload.flow.FlowPayload) ArrayList(java.util.ArrayList) DefaultParameters.topologyEndpoint(org.openkilda.DefaultParameters.topologyEndpoint) DefaultParameters.northboundEndpoint(org.openkilda.DefaultParameters.northboundEndpoint) UnroutablePathException(org.openkilda.pce.provider.UnroutablePathException) TopologyProcessingException(org.openkilda.topo.exceptions.TopologyProcessingException) IOException(java.io.IOException) HashSet(java.util.HashSet) Flow(org.openkilda.messaging.model.Flow)

Example 4 with FlowPayload

use of org.openkilda.messaging.payload.flow.FlowPayload in project open-kilda by telstra.

the class FlowUtils method updateFlow.

/**
 * Updates flow through Northbound service.
 *
 * @param flowId flow id
 * @param payload flow JSON data
 * @return The JSON document of the created flow
 */
public static FlowPayload updateFlow(final String flowId, final FlowPayload payload) {
    System.out.println("\n==> Northbound Update Flow");
    long current = System.currentTimeMillis();
    Client client = clientFactory();
    Response response = client.target(northboundEndpoint).path("/api/v1/flows").path("{flowid}").resolveTemplate("flowid", flowId).request(MediaType.APPLICATION_JSON).header(HttpHeaders.AUTHORIZATION, authHeaderValue).header(Utils.CORRELATION_ID, String.valueOf(System.currentTimeMillis())).put(Entity.json(payload));
    System.out.println(format("===> Request Payload = %s", Entity.json(payload).getEntity()));
    System.out.println(format("===> Response = %s", response.toString()));
    System.out.println(format("===> Northbound Update Flow Time: %,.3f", getTimeDuration(current)));
    int responseCode = response.getStatus();
    if (responseCode == 200) {
        FlowPayload flow = response.readEntity(FlowPayload.class);
        System.out.println(format("====> Northbound Update Flow = %s", flow));
        return flow;
    } else {
        System.out.println(format("====> Error: Northbound Update Flow = %s", response.readEntity(MessageError.class)));
        return null;
    }
}
Also used : Response(javax.ws.rs.core.Response) FlowPayload(org.openkilda.messaging.payload.flow.FlowPayload) Client(javax.ws.rs.client.Client) DefaultParameters.topologyEndpoint(org.openkilda.DefaultParameters.topologyEndpoint) DefaultParameters.northboundEndpoint(org.openkilda.DefaultParameters.northboundEndpoint)

Example 5 with FlowPayload

use of org.openkilda.messaging.payload.flow.FlowPayload in project open-kilda by telstra.

the class KafkaMessageConsumer method receive.

/**
 * Receives messages from WorkFlowManager queue.
 *
 * @param record the message object instance
 */
@KafkaListener(topics = "kilda-test")
public void receive(final String record) {
    logger.debug("message received: {}", record);
    try {
        Message message = MAPPER.readValue(record, Message.class);
        if (message.getDestination() == null || Destination.TOPOLOGY_ENGINE.equals(message.getDestination())) {
            if (message instanceof CommandMessage) {
                CommandData data = ((CommandMessage) message).getData();
                if (data instanceof FlowCreateRequest) {
                    FlowPayload payload = ((FlowCreateRequest) data).getPayload();
                    logger.debug("FlowCreateRequest: {}", payload);
                    Set<CommandMessage> commands = flowService.createFlow(payload, message.getCorrelationId());
                    for (CommandMessage response : commands) {
                        kafkaMessageProducer.send(topic, response);
                    }
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowDeleteRequest) {
                    FlowIdStatusPayload payload = ((FlowDeleteRequest) data).getPayload();
                    logger.debug("FlowDeleteRequest: {}", payload);
                    Set<CommandMessage> commands = flowService.deleteFlow(payload, message.getCorrelationId());
                    for (CommandMessage response : commands) {
                        kafkaMessageProducer.send(topic, response);
                    }
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowUpdateRequest) {
                    FlowPayload payload = ((FlowUpdateRequest) data).getPayload();
                    logger.debug("FlowUpdateRequest: {}", payload);
                    Set<CommandMessage> commands = flowService.updateFlow(payload, message.getCorrelationId());
                    for (CommandMessage response : commands) {
                        kafkaMessageProducer.send(topic, response);
                    }
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowGetRequest) {
                    FlowIdStatusPayload payload = ((FlowGetRequest) data).getPayload();
                    logger.debug("FlowGetRequest: {}", payload);
                    InfoMessage response = flowService.getFlow(payload, message.getCorrelationId());
                    kafkaMessageProducer.send(topic, response);
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowsGetRequest) {
                    FlowIdStatusPayload payload = ((FlowsGetRequest) data).getPayload();
                    logger.debug("FlowsGetRequest: {}", payload);
                    InfoMessage response = flowService.getFlows(payload, message.getCorrelationId());
                    kafkaMessageProducer.send(topic, response);
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else if (data instanceof FlowPathRequest) {
                    FlowIdStatusPayload payload = ((FlowPathRequest) data).getPayload();
                    logger.debug("FlowPathRequest: {}", payload);
                    InfoMessage response = flowService.pathFlow(payload, message.getCorrelationId());
                    kafkaMessageProducer.send(topic, response);
                    logger.debug("Response send, {}={}", CORRELATION_ID, message.getCorrelationId());
                } else {
                    logger.error("Unexpected command message data type: {}", data);
                }
            } else if (message instanceof InfoMessage) {
                InfoData data = ((InfoMessage) message).getData();
                if (data instanceof SwitchInfoData) {
                    SwitchInfoData payload = (SwitchInfoData) data;
                    switch(payload.getState()) {
                        case ADDED:
                            switchService.add(payload);
                            break;
                        case ACTIVATED:
                            switchService.activate(payload);
                            break;
                        case DEACTIVATED:
                            switchService.deactivate(payload);
                            break;
                        case REMOVED:
                            switchService.remove(payload);
                            break;
                        case CHANGED:
                        default:
                            break;
                    }
                } else if (data instanceof IslInfoData) {
                    IslInfoData payload = (IslInfoData) data;
                    islService.discoverLink(payload);
                } else {
                    logger.debug("Unexpected info message data type: {}", data);
                }
            }
        } else {
            logger.debug("Skip message: {}", message);
        }
    } catch (IOException exception) {
        logger.error("Could not deserialize message: {}", record, exception);
    }
}
Also used : FlowsGetRequest(org.openkilda.messaging.command.flow.FlowsGetRequest) Set(java.util.Set) FlowUpdateRequest(org.openkilda.messaging.command.flow.FlowUpdateRequest) InfoMessage(org.openkilda.messaging.info.InfoMessage) Message(org.openkilda.messaging.Message) CommandMessage(org.openkilda.messaging.command.CommandMessage) FlowIdStatusPayload(org.openkilda.messaging.payload.flow.FlowIdStatusPayload) FlowDeleteRequest(org.openkilda.messaging.command.flow.FlowDeleteRequest) FlowCreateRequest(org.openkilda.messaging.command.flow.FlowCreateRequest) FlowGetRequest(org.openkilda.messaging.command.flow.FlowGetRequest) IOException(java.io.IOException) CommandMessage(org.openkilda.messaging.command.CommandMessage) FlowPayload(org.openkilda.messaging.payload.flow.FlowPayload) InfoMessage(org.openkilda.messaging.info.InfoMessage) IslInfoData(org.openkilda.messaging.info.event.IslInfoData) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData) InfoData(org.openkilda.messaging.info.InfoData) IslInfoData(org.openkilda.messaging.info.event.IslInfoData) CommandData(org.openkilda.messaging.command.CommandData) SwitchInfoData(org.openkilda.messaging.info.event.SwitchInfoData) FlowPathRequest(org.openkilda.messaging.command.flow.FlowPathRequest) KafkaListener(org.springframework.kafka.annotation.KafkaListener)

Aggregations

FlowPayload (org.openkilda.messaging.payload.flow.FlowPayload)48 Test (org.junit.Test)14 FlowEndpointPayload (org.openkilda.messaging.payload.flow.FlowEndpointPayload)9 Then (cucumber.api.java.en.Then)7 When (cucumber.api.java.en.When)7 ApiOperation (io.swagger.annotations.ApiOperation)6 ApiResponses (io.swagger.annotations.ApiResponses)6 DefaultParameters.northboundEndpoint (org.openkilda.DefaultParameters.northboundEndpoint)6 DefaultParameters.topologyEndpoint (org.openkilda.DefaultParameters.topologyEndpoint)6 PathInfoData (org.openkilda.messaging.info.event.PathInfoData)6 FlowIdStatusPayload (org.openkilda.messaging.payload.flow.FlowIdStatusPayload)6 HttpHeaders (org.springframework.http.HttpHeaders)6 ResponseEntity (org.springframework.http.ResponseEntity)6 WithMockUser (org.springframework.security.test.context.support.WithMockUser)6 MvcResult (org.springframework.test.web.servlet.MvcResult)6 And (cucumber.api.java.en.And)5 Client (javax.ws.rs.client.Client)5 Response (javax.ws.rs.core.Response)5 ArrayList (java.util.ArrayList)4 List (java.util.List)3