MinExternalConnections extends CriteriaCached { @Override public List<TopologyWrapper> filter(List<TopologyWrapper> topologies) { List<TopologyWrapper> minExternalRTs = new ArrayList<>(); int minExternalRTsCount = Integer.MAX_VALUE; for (TopologyWrapper topology : topologies) { int externalRTsCount = 0; for (TRelationshipTemplate relationshipTemplate : topology.getConnectsTos(topology.getRelationshipTemplates())) { TNodeTemplate source = topology.getSourceNT(relationshipTemplate); TNodeTemplate target = topology.getTargetNT(relationshipTemplate); if (!topology.isTopLevelNT(source) || !topology.isTopLevelNT(target)) { continue; } String sourceTargetLabel = ModelUtilities.getTargetLabel(source).get(); String targetTargetLabel = ModelUtilities.getTargetLabel(target).get(); if (!sourceTargetLabel.equals(targetTargetLabel)) { externalRTsCount++; } } if (externalRTsCount < minExternalRTsCount) { minExternalRTs.clear(); minExternalRTs.add(topology); minExternalRTsCount = externalRTsCount; } else if (externalRTsCount == minExternalRTsCount) { minExternalRTs.add(topology); } } return minExternalRTs; } MinExternalConnections(JsonNode params, int outputCap); @Override List<TopologyWrapper> filter(List<TopologyWrapper> topologies); @Override List<TopologyWrapper> generateTargetLabelTopologies(TopologyWrapper topology);  }
@Test public void filter() throws Exception { ServiceTemplateId id = new ServiceTemplateId("http: "MinExternalConnectionsTest1", false); TTopologyTemplate twoExternal = repository.getElement(id).getTopologyTemplate(); ModelUtilities.setTargetLabel(twoExternal.getNodeTemplate("shetland_pony_2"), "PastureProvider"); ModelUtilities.setTargetLabel(twoExternal.getNodeTemplate("shetland_pony_3"), "PastureProvider"); TTopologyTemplate oneExternal = AllocationUtils.deepcopy(twoExternal); ModelUtilities.setTargetLabel(oneExternal.getNodeTemplate("shetland_pony_2"), "FieldProvider"); ModelUtilities.setTargetLabel(oneExternal.getNodeTemplate("shetland_pony_3"), "FieldProvider"); List<TopologyWrapper> topologies = new ArrayList<>(); topologies.add(new TopologyWrapper(twoExternal)); topologies.add(new TopologyWrapper(oneExternal)); Criteria minExternalConnections = new MinExternalConnections(null, Integer.MAX_VALUE); List<TopologyWrapper> filtered = minExternalConnections.filter(topologies); assertEquals(1, filtered.size()); TTopologyTemplate result = filtered.get(0).getTopology(); assertEquals("PastureProvider".toLowerCase(), ModelUtilities.getTargetLabel(result.getNodeTemplate("shetland_pony")).get()); assertEquals("FieldProvider".toLowerCase(), ModelUtilities.getTargetLabel(result.getNodeTemplate("shetland_pony_2")).get()); assertEquals("FieldProvider".toLowerCase(), ModelUtilities.getTargetLabel(result.getNodeTemplate("shetland_pony_3")).get()); assertEquals("FieldProvider".toLowerCase(), ModelUtilities.getTargetLabel(result.getNodeTemplate("shetland_pony_4")).get()); }