FulfillPolicies extends CriteriaCached { @Override public List<TopologyWrapper> filter(List<TopologyWrapper> topologies) { List<TopologyWrapper> cloned = topologies.stream().map(t -> new TopologyWrapper(AllocationUtils.deepcopy(t.getTopology()))).collect(Collectors.toList()); Map<TNodeTemplate, List<PolicyWrapper>> policiesForNTs = getPoliciesForNTs(cloned.get(0), params); return new PoliciesFilter(cloned, policiesForNTs).filter(); } FulfillPolicies(JsonNode params, int outputCap); @Override List<TopologyWrapper> filter(List<TopologyWrapper> topologies); @Override List<TopologyWrapper> generateTargetLabelTopologies(TopologyWrapper topology);  }
@Test public void testSimpleOperators() throws Exception { JsonNode params = JacksonProvider.mapper.readTree("{\"policySelection\":[" + "{\"policy\":\"floorwarmth10\",\"property\":\"warmth\",\"operator\":\">\"}," + "{\"policy\":\"floorwarmth30\",\"property\":\"warmth\",\"operator\":\"<\"}]}"); Criteria fulfillPolicies = new FulfillPolicies(params, Integer.MAX_VALUE); List<TopologyWrapper> allocatedTopologies = fulfillPolicies.allocate(topology); assertEquals(4, allocatedTopologies.size()); TopologyWrapper allocated1 = allocatedTopologies.get(0); TopologyWrapper allocated2 = allocatedTopologies.get(1); TopologyWrapper allocated3 = allocatedTopologies.get(2); TopologyWrapper allocated4 = allocatedTopologies.get(3); assertEquals(8, allocated1.getNodeTemplates().size()); assertEquals(8, allocated2.getNodeTemplates().size()); assertEquals(8, allocated3.getNodeTemplates().size()); assertEquals(8, allocated4.getNodeTemplates().size()); List<String> targetLabels1 = getTargetLabels(allocated1); List<String> targetLabels2 = getTargetLabels(allocated2); List<String> targetLabels3 = getTargetLabels(allocated3); List<String> targetLabels4 = getTargetLabels(allocated4); assertEquals(3, targetLabels1.stream().filter("PastureProvider"::equalsIgnoreCase).count()); assertEquals(1, targetLabels1.stream().filter("FieldProvider"::equalsIgnoreCase).count()); assertEquals(2, targetLabels2.stream().filter("PastureProvider"::equalsIgnoreCase).count()); assertEquals(2, targetLabels2.stream().filter("FieldProvider"::equalsIgnoreCase).count()); assertEquals(2, targetLabels3.stream().filter("PastureProvider"::equalsIgnoreCase).count()); assertEquals(2, targetLabels3.stream().filter("FieldProvider"::equalsIgnoreCase).count()); assertEquals(1, targetLabels4.stream().filter("PastureProvider"::equalsIgnoreCase).count()); assertEquals(3, targetLabels4.stream().filter("FieldProvider"::equalsIgnoreCase).count()); }
@Test public void testFilter() throws Exception { Criteria minConnections = new MinExternalConnections(null, Integer.MAX_VALUE); List<TopologyWrapper> topologies = minConnections.allocate(topology); if (topologies.size() != 4) { LOGGER.debug("Min cut algorithm didn't find all results"); return; } boolean rightResultPresent = false; for (TopologyWrapper topology : topologies) { TTopologyTemplate topologyTemplate = topology.getTopology(); TNodeTemplate pony3 = topologyTemplate.getNodeTemplate("shetland_pony_3"); TNodeTemplate pony4 = topologyTemplate.getNodeTemplate("shetland_pony_4"); if (ModelUtilities.getTargetLabel(pony3).get().equals("PastureProvider") && ModelUtilities.getTargetLabel(pony4).get().equals("FieldProvider")) { LOGGER.debug("Right result present"); rightResultPresent = true; break; } } if (!rightResultPresent) { LOGGER.debug("Min cut algorithm didn't find needed result"); return; } JsonNode params = JacksonProvider.mapper.readTree("{\"policySelection\":[" + "{\"policy\":\"floorwarmth10\",\"property\":\"warmth\",\"operator\":\"min\"}," + "{\"policy\":\"floorwarmth30\",\"property\":\"warmth\",\"operator\":\"max\"}]}"); Criteria fulfillPolicies = new FulfillPolicies(params, Integer.MAX_VALUE); topologies = fulfillPolicies.filter(topologies); assertEquals(1, topologies.size()); TTopologyTemplate allocated = topologies.get(0).getTopology(); assertEquals(6, allocated.getNodeTemplates().size()); assertEquals("PastureProvider", ModelUtilities.getTargetLabel(allocated.getNodeTemplate("shetland_pony_3")).get()); assertEquals("FieldProvider", ModelUtilities.getTargetLabel(allocated.getNodeTemplate("shetland_pony_4")).get()); assertEquals("PastureProvider", ModelUtilities.getTargetLabel(allocated.getNodeTemplate("shetland_pony")).get()); assertEquals("FieldProvider", ModelUtilities.getTargetLabel(allocated.getNodeTemplate("shetland_pony_2")).get()); }