### Question:
CommandBuilderImpl implements CommandBuilder { @Override public List<BaseFlow> buildCommandsToSyncMissingRules(SwitchId switchId, List<Long> switchRules) { List<BaseFlow> commands = new ArrayList<>(buildInstallDefaultRuleCommands(switchId, switchRules)); commands.addAll(buildInstallFlowSharedRuleCommands(switchId, switchRules)); flowPathRepository.findBySegmentDestSwitch(switchId) .forEach(flowPath -> { if (switchRules.contains(flowPath.getCookie().getValue())) { PathSegment segment = flowPath.getSegments().stream() .filter(pathSegment -> pathSegment.getDestSwitchId().equals(switchId)) .findAny() .orElseThrow(() -> new IllegalStateException( format("PathSegment not found, path %s, switch %s", flowPath, switchId))); log.info("Rule {} is to be (re)installed on switch {}", flowPath.getCookie(), switchId); commands.addAll(buildInstallCommandFromSegment(flowPath, segment)); } }); SwitchProperties switchProperties = getSwitchProperties(switchId); flowPathRepository.findByEndpointSwitch(switchId) .forEach(flowPath -> { if (switchRules.contains(flowPath.getCookie().getValue())) { Flow flow = getFlow(flowPath); if (flowPath.isOneSwitchFlow()) { log.info("One-switch flow {} is to be (re)installed on switch {}", flowPath.getCookie(), switchId); commands.add(flowCommandFactory.makeOneSwitchRule(flow, flowPath)); } else if (flowPath.getSrcSwitchId().equals(switchId)) { log.info("Ingress flow {} is to be (re)installed on switch {}", flowPath.getCookie(), switchId); if (flowPath.getSegments().isEmpty()) { log.warn("Output port was not found for ingress flow rule"); } else { PathSegment foundIngressSegment = flowPath.getSegments().get(0); EncapsulationResources encapsulationResources = getEncapsulationResources( flowPath, flow); commands.add(flowCommandFactory.buildInstallIngressFlow(flow, flowPath, foundIngressSegment.getSrcPort(), encapsulationResources, foundIngressSegment.isSrcWithMultiTable())); } } } long server42Cookie = flowPath.getCookie().toBuilder() .type(CookieType.SERVER_42_INGRESS) .build() .getValue(); if (switchRules.contains(server42Cookie) && !flowPath.isOneSwitchFlow() && flowPath.getSrcSwitchId().equals(switchId)) { log.info("Ingress server 42 flow {} is to be (re)installed on switch {}", server42Cookie, switchId); if (flowPath.getSegments().isEmpty()) { log.warn("Output port was not found for server 42 ingress flow rule {}", server42Cookie); } else { Flow flow = getFlow(flowPath); PathSegment foundIngressSegment = flowPath.getSegments().get(0); EncapsulationResources encapsulationResources = getEncapsulationResources(flowPath, flow); commands.add(flowCommandFactory.buildInstallServer42IngressFlow( flow, flowPath, foundIngressSegment.getSrcPort(), switchProperties.getServer42Port(), switchProperties.getServer42MacAddress(), encapsulationResources, foundIngressSegment.isSrcWithMultiTable())); } } }); return commands; } CommandBuilderImpl(PersistenceManager persistenceManager, FlowResourcesConfig flowResourcesConfig); @Override List<BaseFlow> buildCommandsToSyncMissingRules(SwitchId switchId, List<Long> switchRules); @Override List<RemoveFlow> buildCommandsToRemoveExcessRules(SwitchId switchId,
                                                             List<FlowEntry> flows,
                                                             List<Long> excessRulesCookies);  }### Answer:
@Test public void testCommandBuilder() { List<BaseFlow> response = commandBuilder .buildCommandsToSyncMissingRules(SWITCH_ID_B, Stream.of(1L, 2L, 3L, 4L) .map(effectiveId -> new FlowSegmentCookie(FlowPathDirection.FORWARD, effectiveId)) .map(Cookie::getValue) .collect(Collectors.toList())); assertEquals(4, response.size()); assertTrue(response.get(0) instanceof InstallEgressFlow); assertTrue(response.get(1) instanceof InstallTransitFlow); assertTrue(response.get(2) instanceof InstallOneSwitchFlow); assertTrue(response.get(3) instanceof InstallIngressFlow); }