Search in sources :

Example 1 with CorrelationContextClosable

use of org.openkilda.floodlight.utils.CorrelationContext.CorrelationContextClosable in project open-kilda by telstra.

the class CorrelationContextTest method shouldRestoreCorrelationIdOnClose.

@Test
public void shouldRestoreCorrelationIdOnClose() {
    // given
    CorrelationContext.create(String.format("test-%s", UUID.randomUUID()));
    String before = CorrelationContext.getId();
    // when
    try (CorrelationContextClosable closable = CorrelationContext.create(String.format("test-%s", UUID.randomUUID()))) {
        // then
        assertThat(CorrelationContext.getId(), not(equalTo(before)));
    }
    assertThat(CorrelationContext.getId(), equalTo(before));
}
Also used : CorrelationContextClosable(org.openkilda.floodlight.utils.CorrelationContext.CorrelationContextClosable) Test(org.junit.Test)

Example 2 with CorrelationContextClosable

use of org.openkilda.floodlight.utils.CorrelationContext.CorrelationContextClosable in project open-kilda by telstra.

the class RecordHandler method parseRecord.

private void parseRecord(ConsumerRecord<String, String> record) {
    if (handleSpeakerCommand()) {
        return;
    }
    if (handleRuleManagerCommand()) {
        return;
    }
    CommandMessage message;
    try {
        String value = record.value();
        // TODO: Prior to Message changes, this MAPPER would read Message ..
        // but, changed to BaseMessage and got an error wrt "timestamp" ..
        // so, need to experiment with why CommandMessage can't be read as
        // a BaseMessage
        message = MAPPER.readValue(value, CommandMessage.class);
    } catch (Exception exception) {
        logger.error("error parsing record '{}'", record.value(), exception);
        return;
    }
    // Process the message within the message correlation context.
    try (CorrelationContextClosable closable = CorrelationContext.create(message.getCorrelationId())) {
        if (logger.isDebugEnabled()) {
            logger.debug("Receive command: key={}, payload={}", record.key(), message.getData());
        }
        CommandContext commandContext = new CommandContext(context.getModuleContext(), message.getCorrelationId(), record.key());
        if (!dispatch(commandContext, message)) {
            handleCommand(message);
        }
    } catch (Exception exception) {
        logger.error("error processing message '{}'", message, exception);
    }
}
Also used : CommandContext(org.openkilda.floodlight.command.CommandContext) CorrelationContextClosable(org.openkilda.floodlight.utils.CorrelationContext.CorrelationContextClosable) InvalidMeterIdException(org.openkilda.floodlight.error.InvalidMeterIdException) IOException(java.io.IOException) SwitchOperationException(org.openkilda.floodlight.error.SwitchOperationException) SwitchNotFoundException(org.openkilda.floodlight.error.SwitchNotFoundException) OfInstallException(org.openkilda.floodlight.error.OfInstallException) FlowCommandException(org.openkilda.floodlight.error.FlowCommandException) UnsupportedSwitchOperationException(org.openkilda.floodlight.error.UnsupportedSwitchOperationException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) CommandMessage(org.openkilda.messaging.command.CommandMessage)

Example 3 with CorrelationContextClosable

use of org.openkilda.floodlight.utils.CorrelationContext.CorrelationContextClosable in project open-kilda by telstra.

the class SwitchTrackingServiceTest method networkDumpTest.

@Test
public void networkDumpTest() throws Exception {
    // Cook mock data for ISwitchManager::getAllSwitchMap
    // Two switches with two ports on each
    // switches for ISwitchManager::getAllSwitchMap
    OFSwitch iofSwitch1 = mock(OFSwitch.class);
    OFSwitch iofSwitch2 = mock(OFSwitch.class);
    final DatapathId swAid = DatapathId.of(1);
    final DatapathId swBid = DatapathId.of(2);
    Map<DatapathId, IOFSwitch> switches = ImmutableMap.of(swAid, iofSwitch1, swBid, iofSwitch2);
    Map<DatapathId, InetSocketAddress> switchAddresses = ImmutableMap.of(swAid, new InetSocketAddress(Inet4Address.getByName("127.0.1.1"), 32768), swBid, new InetSocketAddress(Inet4Address.getByName("127.0.1.2"), 32768));
    SwitchDescription ofSwitchDescription = new SwitchDescription(switchDescription.getManufacturer(), switchDescription.getHardware(), switchDescription.getSoftware(), switchDescription.getSerialNumber(), switchDescription.getDatapath());
    OFFactoryVer13 ofFactory = new OFFactoryVer13();
    InetSocketAddress switchSocketAddress = new InetSocketAddress(Inet4Address.getByName("127.0.1.1"), 32768);
    for (DatapathId swId : switches.keySet()) {
        IOFSwitch sw = switches.get(swId);
        expect(sw.getOFFactory()).andStubReturn(ofFactory);
        expect(sw.isActive()).andReturn(true).anyTimes();
        expect(sw.getId()).andReturn(swId).anyTimes();
        expect(sw.getSwitchDescription()).andReturn(ofSwitchDescription);
        expect(sw.getInetAddress()).andReturn(switchAddresses.get(swId));
        expect(sw.getControllerRole()).andStubReturn(OFControllerRole.ROLE_EQUAL);
        OFConnection connect = createMock(OFConnection.class);
        expect(connect.getRemoteInetAddress()).andReturn(switchSocketAddress);
        expect(sw.getConnectionByCategory(eq(LogicalOFMessageCategory.MAIN))).andReturn(connect);
    }
    expect(switchManager.getAllSwitchMap(true)).andReturn(switches);
    expect(switchManager.getPhysicalPorts(eq(iofSwitch1))).andReturn(ImmutableList.of(makePhysicalPortMock(1, true), makePhysicalPortMock(2, true)));
    expect(switchManager.getPhysicalPorts(eq(iofSwitch2))).andReturn(ImmutableList.of(makePhysicalPortMock(3, true), makePhysicalPortMock(4, true), makePhysicalPortMock(5, false)));
    expect(featureDetector.detectSwitch(iofSwitch1)).andReturn(ImmutableSet.of(SwitchFeature.METERS));
    expect(featureDetector.detectSwitch(iofSwitch2)).andReturn(ImmutableSet.of(SwitchFeature.METERS, SwitchFeature.BFD));
    ArrayList<Message> producedMessages = new ArrayList<>();
    // setup hook for verify that we create new message for producer
    producerService.sendMessageAndTrack(eq(KAFKA_ISL_DISCOVERY_TOPIC), anyObject(), anyObject(InfoMessage.class));
    expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() {
            Message sentMessage = (Message) getCurrentArguments()[2];
            sentMessage.setTimestamp(0);
            producedMessages.add(sentMessage);
            return null;
        }
    }).anyTimes();
    replayAll();
    String correlationId = "unit-test-correlation-id";
    String dumpId = "dummy-dump-id";
    try (CorrelationContextClosable dummy = CorrelationContext.create(correlationId)) {
        service.dumpAllSwitches(dumpId);
    }
    verify(producerService);
    ArrayList<Message> expectedMessages = new ArrayList<>();
    expectedMessages.add(new InfoMessage(new NetworkDumpSwitchData(new SpeakerSwitchView(new SwitchId(swAid.getLong()), new IpSocketAddress("127.0.1.1", 32768), new IpSocketAddress("127.0.1.254", 6653), "127.0.1.1", "OF_13", switchDescription, ImmutableSet.of(SwitchFeature.METERS), ImmutableList.of(new SpeakerSwitchPortView(1, SpeakerSwitchPortView.State.UP), new SpeakerSwitchPortView(2, SpeakerSwitchPortView.State.UP))), dumpId, true), 0, correlationId));
    expectedMessages.add(new InfoMessage(new NetworkDumpSwitchData(new SpeakerSwitchView(new SwitchId(swBid.getLong()), new IpSocketAddress("127.0.1.2", 32768), new IpSocketAddress("127.0.1.254", 6653), "127.0.1.2", "OF_13", switchDescription, ImmutableSet.of(SwitchFeature.METERS, SwitchFeature.BFD), ImmutableList.of(new SpeakerSwitchPortView(3, SpeakerSwitchPortView.State.UP), new SpeakerSwitchPortView(4, SpeakerSwitchPortView.State.UP), new SpeakerSwitchPortView(5, SpeakerSwitchPortView.State.DOWN))), dumpId, true), 0, correlationId));
    assertEquals(expectedMessages, producedMessages);
}
Also used : IOFSwitch(net.floodlightcontroller.core.IOFSwitch) OFConnection(net.floodlightcontroller.core.internal.OFConnection) InfoMessage(org.openkilda.messaging.info.InfoMessage) Message(org.openkilda.messaging.Message) OFFactoryVer13(org.projectfloodlight.openflow.protocol.ver13.OFFactoryVer13) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) CorrelationContextClosable(org.openkilda.floodlight.utils.CorrelationContext.CorrelationContextClosable) DatapathId(org.projectfloodlight.openflow.types.DatapathId) SwitchId(org.openkilda.model.SwitchId) IpSocketAddress(org.openkilda.model.IpSocketAddress) IAnswer(org.easymock.IAnswer) SpeakerSwitchPortView(org.openkilda.messaging.model.SpeakerSwitchPortView) InfoMessage(org.openkilda.messaging.info.InfoMessage) SpeakerSwitchView(org.openkilda.messaging.model.SpeakerSwitchView) IOFSwitch(net.floodlightcontroller.core.IOFSwitch) OFSwitch(net.floodlightcontroller.core.internal.OFSwitch) SpeakerSwitchDescription(org.openkilda.messaging.model.SpeakerSwitchDescription) SwitchDescription(net.floodlightcontroller.core.SwitchDescription) NetworkDumpSwitchData(org.openkilda.messaging.info.discovery.NetworkDumpSwitchData) Test(org.junit.Test)

Example 4 with CorrelationContextClosable

use of org.openkilda.floodlight.utils.CorrelationContext.CorrelationContextClosable in project open-kilda by telstra.

the class RequestCorrelationFilter method doHandle.

@Override
protected int doHandle(Request request, Response response) {
    Form headers = (Form) request.getAttributes().get("org.restlet.http.headers");
    String correlationId = headers.getFirstValue(CORRELATION_ID);
    if (correlationId == null || correlationId.trim().isEmpty()) {
        correlationId = UUID.randomUUID().toString();
        LOGGER.warn("CorrelationId was not sent, generated one: {}", correlationId);
    } else {
        LOGGER.debug("Found correlationId in header: {}", correlationId);
    }
    try (CorrelationContextClosable closable = CorrelationContext.create(correlationId)) {
        return super.doHandle(request, response);
    }
}
Also used : Form(org.restlet.data.Form) CorrelationContextClosable(org.openkilda.floodlight.utils.CorrelationContext.CorrelationContextClosable)

Aggregations

CorrelationContextClosable (org.openkilda.floodlight.utils.CorrelationContext.CorrelationContextClosable)4 Test (org.junit.Test)2 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 IOException (java.io.IOException)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 IOFSwitch (net.floodlightcontroller.core.IOFSwitch)1 SwitchDescription (net.floodlightcontroller.core.SwitchDescription)1 OFConnection (net.floodlightcontroller.core.internal.OFConnection)1 OFSwitch (net.floodlightcontroller.core.internal.OFSwitch)1 IAnswer (org.easymock.IAnswer)1 CommandContext (org.openkilda.floodlight.command.CommandContext)1 FlowCommandException (org.openkilda.floodlight.error.FlowCommandException)1 InvalidMeterIdException (org.openkilda.floodlight.error.InvalidMeterIdException)1 OfInstallException (org.openkilda.floodlight.error.OfInstallException)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 CommandMessage (org.openkilda.messaging.command.CommandMessage)1