Search in sources :

Example 1 with RemoveIsl

use of org.openkilda.server42.control.messaging.islrtt.IslRttControl.RemoveIsl in project open-kilda by telstra.

the class Gate method listen.

@KafkaHandler
void listen(@Payload ListIslPortsOnSwitch data) {
    Builder builder = CommandPacket.newBuilder();
    builder.setType(Type.LIST_ISLS);
    IslRttControl.ListIslsFilter listIslsFilter = IslRttControl.ListIslsFilter.newBuilder().setSwitchId(data.getSwitchId().toString()).build();
    builder.addCommand(Any.pack(listIslsFilter));
    try {
        CommandPacketResponse serverResponse = zeroMqClient.send(builder.build());
        if (serverResponse == null) {
            log.error("No response from server on {}", data.getHeaders().getCorrelationId());
            return;
        }
        for (Any any : serverResponse.getResponseList()) {
            IslRttControl.IslEndpoint endpoint = any.unpack(IslRttControl.IslEndpoint.class);
            if (!data.getIslPorts().contains(endpoint.getPort())) {
                removeIsl(data.getSwitchId(), endpoint.getPort());
            }
        }
    } catch (InvalidProtocolBufferException e) {
        log.error("Marshalling error on {}", data, e);
    }
}
Also used : IslEndpoint(org.openkilda.server42.control.messaging.islrtt.IslRttControl.IslEndpoint) Builder(org.openkilda.server42.control.messaging.Control.CommandPacket.Builder) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IslRttControl(org.openkilda.server42.control.messaging.islrtt.IslRttControl) CommandPacketResponse(org.openkilda.server42.control.messaging.Control.CommandPacketResponse) Any(com.google.protobuf.Any) KafkaHandler(org.springframework.kafka.annotation.KafkaHandler)

Example 2 with RemoveIsl

use of org.openkilda.server42.control.messaging.islrtt.IslRttControl.RemoveIsl in project open-kilda by telstra.

the class Gate method removeIsl.

private void removeIsl(SwitchId switchId, int port) {
    Builder builder = CommandPacket.newBuilder();
    IslEndpoint endpoint = IslEndpoint.newBuilder().setSwitchId(switchId.toString()).setPort(port).build();
    IslRttControl.RemoveIsl removeIsl = IslRttControl.RemoveIsl.newBuilder().setIsl(endpoint).build();
    builder.setType(Type.REMOVE_ISL);
    builder.addCommand(Any.pack(removeIsl));
    try {
        zeroMqClient.send(builder.build());
    } catch (InvalidProtocolBufferException e) {
        log.error("Marshalling error on {} / {}", switchId, port, e);
    }
}
Also used : IslEndpoint(org.openkilda.server42.control.messaging.islrtt.IslRttControl.IslEndpoint) Builder(org.openkilda.server42.control.messaging.Control.CommandPacket.Builder) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) IslRttControl(org.openkilda.server42.control.messaging.islrtt.IslRttControl)

Example 3 with RemoveIsl

use of org.openkilda.server42.control.messaging.islrtt.IslRttControl.RemoveIsl in project open-kilda by telstra.

the class ControlServer method run.

/**
 * We get commands from zmq socket and execute them one by one.
 */
@Override
public void run() {
    log.info("started");
    try (ZContext context = new ZContext()) {
        Socket server = context.createSocket(ZMQ.REP);
        server.bind(bindEndpoint);
        while (!isInterrupted()) {
            byte[] request = server.recv();
            try {
                CommandPacket commandPacket = CommandPacket.parseFrom(request);
                Builder builder = CommandPacketResponse.newBuilder();
                builder.setCommunicationId(commandPacket.getCommunicationId());
                log.info("command type {}", commandPacket.getType().toString());
                log.info("flow list before {}", flows.keySet().toString());
                switch(commandPacket.getType()) {
                    case ADD_FLOW:
                        for (Any any : commandPacket.getCommandList()) {
                            AddFlow addFlow = any.unpack(AddFlow.class);
                            flows.put(FlowKey.fromFlow(addFlow.getFlow()), addFlow.getFlow());
                            statsServer.addFlow(addFlow.getFlow());
                        }
                        break;
                    case REMOVE_FLOW:
                        for (Any any : commandPacket.getCommandList()) {
                            RemoveFlow removeFlow = any.unpack(RemoveFlow.class);
                            FlowKey flowKey = FlowKey.fromFlow(removeFlow.getFlow());
                            flows.remove(flowKey);
                            statsServer.removeFlow(flowKey);
                        }
                        break;
                    case CLEAR_FLOWS:
                        if (commandPacket.getCommandCount() > 0) {
                            Any command = commandPacket.getCommand(0);
                            ClearFlowsFilter filter = command.unpack(ClearFlowsFilter.class);
                            List<FlowKey> keys = flows.values().stream().filter(flow -> flow.getDstMac().equals(filter.getDstMac())).map(FlowKey::fromFlow).collect(Collectors.toList());
                            flows.keySet().removeAll(keys);
                            keys.forEach(statsServer::removeFlow);
                        } else {
                            flows.clear();
                            statsServer.clearFlows();
                        }
                        break;
                    case LIST_FLOWS:
                        if (commandPacket.getCommandCount() > 0) {
                            Any command = commandPacket.getCommand(0);
                            ListFlowsFilter filter = command.unpack(ListFlowsFilter.class);
                            flows.values().stream().filter(flow -> flow.getDstMac().equals(filter.getDstMac())).forEach(flow -> builder.addResponse(Any.pack(flow)));
                        } else {
                            for (Flow flow : flows.values()) {
                                builder.addResponse(Any.pack(flow));
                            }
                        }
                        break;
                    case PUSH_SETTINGS:
                        log.warn("will be shipped with stats application");
                        Any command = commandPacket.getCommand(0);
                        PushSettings settings = command.unpack(PushSettings.class);
                        log.info(settings.toString());
                        break;
                    case ADD_ISL:
                        for (Any any : commandPacket.getCommandList()) {
                            AddIsl addIsl = any.unpack(AddIsl.class);
                            IslEndpoint endpoint = addIsl.getIsl();
                            isls.computeIfAbsent(endpoint.getSwitchId(), k -> new HashSet<>()).add(endpoint.getPort());
                            statsServer.addIsl(endpoint.getSwitchId(), endpoint.getPort());
                        }
                        break;
                    case REMOVE_ISL:
                        for (Any any : commandPacket.getCommandList()) {
                            RemoveIsl removeIsl = any.unpack(RemoveIsl.class);
                            IslEndpoint endpoint = removeIsl.getIsl();
                            isls.getOrDefault(endpoint.getSwitchId(), emptySet()).remove(endpoint.getPort());
                            statsServer.removeIsl(endpoint.getSwitchId(), endpoint.getPort());
                        }
                        break;
                    case CLEAR_ISLS:
                        if (commandPacket.getCommandCount() > 0) {
                            ClearIslsFilter filter = commandPacket.getCommand(0).unpack(ClearIslsFilter.class);
                            Set<Integer> ports = Optional.ofNullable(isls.get(filter.getSwitchId())).orElse(emptySet());
                            isls.remove(filter.getSwitchId());
                            ports.forEach(p -> statsServer.removeIsl(filter.getSwitchId(), p));
                        } else {
                            isls.clear();
                            statsServer.clearIsls();
                        }
                        break;
                    case LIST_ISLS:
                        if (commandPacket.getCommandCount() > 0) {
                            ListIslsFilter filter = commandPacket.getCommand(0).unpack(ListIslsFilter.class);
                            Optional.ofNullable(isls.get(filter.getSwitchId())).orElse(emptySet()).forEach(port -> builder.addResponse(Any.pack(IslEndpoint.newBuilder().setSwitchId(filter.getSwitchId()).setPort(port).build())));
                        } else {
                            isls.forEach((switchId, ports) -> ports.forEach(port -> builder.addResponse(Any.pack(IslEndpoint.newBuilder().setSwitchId(switchId).setPort(port).build()))));
                        }
                        break;
                    case UNRECOGNIZED:
                    default:
                        log.error("Unknown command type");
                        break;
                }
                log.info("flow list after {}", flows.keySet().toString());
                server.send(builder.build().toByteArray());
            } catch (InvalidProtocolBufferException e) {
                log.error("marshalling error");
            }
        }
    }
}
Also used : CommandPacketResponse(org.openkilda.server42.control.messaging.Control.CommandPacketResponse) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) ZContext(org.zeromq.ZContext) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) ClearIslsFilter(org.openkilda.server42.control.messaging.islrtt.IslRttControl.ClearIslsFilter) ClearFlowsFilter(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.ClearFlowsFilter) RemoveFlow(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.RemoveFlow) Service(org.springframework.stereotype.Service) ListFlowsFilter(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.ListFlowsFilter) ZMQ(org.zeromq.ZMQ) Flow(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.Flow) RemoveIsl(org.openkilda.server42.control.messaging.islrtt.IslRttControl.RemoveIsl) Socket(org.zeromq.ZMQ.Socket) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) AddIsl(org.openkilda.server42.control.messaging.islrtt.IslRttControl.AddIsl) Collections.emptySet(java.util.Collections.emptySet) Set(java.util.Set) AddFlow(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.AddFlow) Builder(org.openkilda.server42.control.messaging.Control.CommandPacketResponse.Builder) Collectors(java.util.stream.Collectors) CommandPacket(org.openkilda.server42.control.messaging.Control.CommandPacket) ListIslsFilter(org.openkilda.server42.control.messaging.islrtt.IslRttControl.ListIslsFilter) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) PushSettings(org.openkilda.server42.control.messaging.Control.PushSettings) Optional(java.util.Optional) Any(com.google.protobuf.Any) IslEndpoint(org.openkilda.server42.control.messaging.islrtt.IslRttControl.IslEndpoint) ClearFlowsFilter(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.ClearFlowsFilter) Builder(org.openkilda.server42.control.messaging.Control.CommandPacketResponse.Builder) ClearIslsFilter(org.openkilda.server42.control.messaging.islrtt.IslRttControl.ClearIslsFilter) PushSettings(org.openkilda.server42.control.messaging.Control.PushSettings) CommandPacket(org.openkilda.server42.control.messaging.Control.CommandPacket) Any(com.google.protobuf.Any) ListFlowsFilter(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.ListFlowsFilter) AddIsl(org.openkilda.server42.control.messaging.islrtt.IslRttControl.AddIsl) HashSet(java.util.HashSet) RemoveIsl(org.openkilda.server42.control.messaging.islrtt.IslRttControl.RemoveIsl) IslEndpoint(org.openkilda.server42.control.messaging.islrtt.IslRttControl.IslEndpoint) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ListIslsFilter(org.openkilda.server42.control.messaging.islrtt.IslRttControl.ListIslsFilter) ZContext(org.zeromq.ZContext) RemoveFlow(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.RemoveFlow) Flow(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.Flow) AddFlow(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.AddFlow) AddFlow(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.AddFlow) RemoveFlow(org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.RemoveFlow) Socket(org.zeromq.ZMQ.Socket)

Example 4 with RemoveIsl

use of org.openkilda.server42.control.messaging.islrtt.IslRttControl.RemoveIsl in project open-kilda by telstra.

the class GateTest method testRemoveIsl.

@Test
public void testRemoveIsl() throws Exception {
    RemoveIsl removeIsl = RemoveIsl.builder().switchId(new SwitchId("00:00:1b:45:18:d6:71:5a")).port(42).build();
    gate.listen(removeIsl);
    CommandPacket commandPacket = getCommandPacket();
    assertThat(commandPacket.getType()).isEqualTo(Type.REMOVE_ISL);
    assertThat(commandPacket.getCommandList()).hasSize(1);
    Any command = commandPacket.getCommand(0);
    assertThat(command.is(IslRttControl.RemoveIsl.class)).isTrue();
    IslRttControl.RemoveIsl unpack = command.unpack(IslRttControl.RemoveIsl.class);
    IslRttControl.IslEndpoint endpoint = unpack.getIsl();
    String switchId = removeIsl.getSwitchId().toString();
    assertThat(endpoint.getSwitchId()).isEqualTo(switchId);
    assertThat(endpoint.getPort()).isEqualTo(removeIsl.getPort());
}
Also used : IslEndpoint(org.openkilda.server42.control.messaging.islrtt.IslRttControl.IslEndpoint) SwitchId(org.openkilda.model.SwitchId) IslRttControl(org.openkilda.server42.control.messaging.islrtt.IslRttControl) CommandPacket(org.openkilda.server42.control.messaging.Control.CommandPacket) Any(com.google.protobuf.Any) RemoveIsl(org.openkilda.server42.control.messaging.islrtt.RemoveIsl) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

IslEndpoint (org.openkilda.server42.control.messaging.islrtt.IslRttControl.IslEndpoint)4 Any (com.google.protobuf.Any)3 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)3 IslRttControl (org.openkilda.server42.control.messaging.islrtt.IslRttControl)3 CommandPacket (org.openkilda.server42.control.messaging.Control.CommandPacket)2 Builder (org.openkilda.server42.control.messaging.Control.CommandPacket.Builder)2 CommandPacketResponse (org.openkilda.server42.control.messaging.Control.CommandPacketResponse)2 Collections.emptySet (java.util.Collections.emptySet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 PostConstruct (javax.annotation.PostConstruct)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Test (org.junit.Test)1 SwitchId (org.openkilda.model.SwitchId)1 Builder (org.openkilda.server42.control.messaging.Control.CommandPacketResponse.Builder)1 PushSettings (org.openkilda.server42.control.messaging.Control.PushSettings)1