use of org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.RemoveFlow in project open-kilda by telstra.
the class Gate method listen.
@KafkaHandler
void listen(ListFlowsOnSwitch data, @Header(KafkaHeaders.RECEIVED_MESSAGE_KEY) String switchIdKey) {
CommandPacket commandPacket = getFlowListCommandPacket(switchIdKey);
try {
CommandPacketResponse serverResponse = zeroMqClient.send(commandPacket);
if (serverResponse == null) {
log.error("No response from server on {}", data.getHeaders().getCorrelationId());
return;
}
for (Any any : serverResponse.getResponseList()) {
String flowId = any.unpack(Flow.class).getFlowId();
if (!data.getFlowIds().contains(flowId)) {
removeFlow(flowId, FlowDirection.FORWARD);
removeFlow(flowId, FlowDirection.REVERSE);
}
}
} catch (InvalidProtocolBufferException e) {
log.error("Marshalling error on {}", data);
}
}
use of org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.RemoveFlow in project open-kilda by telstra.
the class Gate method removeFlow.
private void removeFlow(String flowId, FlowDirection direction) throws InvalidProtocolBufferException {
Builder builder = CommandPacket.newBuilder();
Flow flow = Flow.newBuilder().setFlowId(flowId).setDirection(FlowDirection.toBoolean(direction)).build();
FlowRttControl.RemoveFlow removeFlow = FlowRttControl.RemoveFlow.newBuilder().setFlow(flow).build();
builder.setType(Type.REMOVE_FLOW);
builder.addCommand(Any.pack(removeFlow));
CommandPacket packet = builder.build();
zeroMqClient.send(packet);
}
use of org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.RemoveFlow 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");
}
}
}
}
use of org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.RemoveFlow in project open-kilda by telstra.
the class FlowHandler method notifyDeactivateFlowMonitoring.
@Override
public void notifyDeactivateFlowMonitoring(SwitchId switchId, String flowId, boolean isForward) {
RemoveFlow removeFlow = RemoveFlow.builder().flowId(flowId).headers(buildHeader()).direction(isForward ? FlowDirection.FORWARD : FlowDirection.REVERSE).build();
emit(STREAM_CONTROL_COMMANDS_ID, getCurrentTuple(), new Values(switchId.toString(), removeFlow));
}
use of org.openkilda.server42.control.messaging.flowrtt.FlowRttControl.RemoveFlow in project open-kilda by telstra.
the class GateTest method removeFlow.
@Test
public void removeFlow() throws Exception {
RemoveFlow removeFlow = RemoveFlow.builder().flowId("some-flow-id").build();
gate.listen(removeFlow);
CommandPacket commandPacket = getCommandPacket();
assertThat(commandPacket.getType()).isEqualTo(Type.REMOVE_FLOW);
assertThat(commandPacket.getCommandList()).hasSize(1);
Any command = commandPacket.getCommand(0);
assertThat(command.is(FlowRttControl.RemoveFlow.class)).isTrue();
FlowRttControl.RemoveFlow unpack = command.unpack(FlowRttControl.RemoveFlow.class);
assertThat(unpack.getFlow().getFlowId()).isEqualTo(removeFlow.getFlowId());
}
Aggregations