PoliciedServiceLevelEvaluator implements IServiceLevelEvaluator { @Override public List<IViolation> evaluate( IAgreement agreement, IGuaranteeTerm term, List<IMonitoringMetric> metrics, Date now) { logger.debug("evaluate(agreement={}, term={}, servicelevel={})", agreement.getAgreementId(), term.getKpiName(), term.getServiceLevel()); checkInitialized(); List<IViolation> newViolations = new ArrayList<IViolation>(); String kpiName = term.getKpiName(); String constraint = term.getServiceLevel(); List<IMonitoringMetric> newBreachMetrics = constraintEval.evaluate(kpiName, constraint, metrics); logger.debug("Found {} breaches in new metrics", newBreachMetrics.size()); List<IPolicy> policies = getPoliciesOrDefault(term); boolean withPolicies = !isDefaultPolicy(policies); List<IBreach> newBreaches = null; if (withPolicies) { newBreaches = breachesFromMetricsBuilder.build(newBreachMetrics, agreement, kpiName); saveBreaches(newBreaches); } for (IPolicy policy : policies) { Date breachesBegin = new Date(now.getTime() - policy.getTimeInterval().getTime()); logger.debug("Evaluating policy({},{}s) in interval({}, {})", policy.getCount(), policy.getTimeInterval().getTime() / 1000, breachesBegin, now); List<IBreach> oldBreaches; if (withPolicies) { oldBreaches = breachRepository.getBreachesByTimeRange(agreement, kpiName, breachesBegin, now); logger.debug("Found {} breaches", oldBreaches.size()); List<IBreach> breaches = new PoliciedServiceLevelEvaluator.CompositeList<IBreach>( oldBreaches, newBreaches); if (evaluatePolicy(policy, oldBreaches, newBreaches)) { IViolation violation = newViolation(agreement, term, policy, kpiName, breaches, now); newViolations.add(violation); violation.setBreaches(breaches); logger.debug("Violation raised"); } } else { oldBreaches = Collections.emptyList(); for (IMonitoringMetric breach : newBreachMetrics) { IViolation violation = newViolation(agreement, term, kpiName, breach); newViolations.add(violation); logger.debug("Violation raised"); } } } return newViolations; }  @Override List<IViolation> evaluate(
            IAgreement agreement, IGuaranteeTerm term, List<IMonitoringMetric> metrics, Date now); IConstraintEvaluator getConstraintEvaluator(); void setConstraintEvaluator(IConstraintEvaluator constraintEval); IBreachRepository getBreachRepository(); void setBreachRepository(IBreachRepository breachRepository);  }
@Test public void testGetViolationsWithPolicy1() { IPolicy policy; System.out.println("---testEvaluateWithPolicy1()---"); List<IMonitoringMetric> metrics = setUpWithPolicies(); policy = newPolicy("LATENCY", 2, new Date(3000)); term.setPolicies(Arrays.asList(new IPolicy[] { policy })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(0, violations.size()); }
@Test public void testGetViolationsWithPolicy2() { IPolicy policy; System.out.println("---testEvaluateWithPolicy2()---"); List<IMonitoringMetric> metrics = setUpWithPolicies(); policy = newPolicy("LATENCY", 2, new Date(5000)); term.setPolicies(Arrays.asList(new IPolicy[] { policy })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(1, violations.size()); assertViolations(violations, allMetrics.get(4), allMetrics.get(5)); }
@Test public void testGetViolationsWithPolicy3() { IPolicy policy; System.out.println("---testEvaluateWithPolicy3()---"); List<IMonitoringMetric> metrics = setUpWithPolicies(); policy = newPolicy("LATENCY", 3, new Date(7000)); term.setPolicies(Arrays.asList(new IPolicy[] { policy })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(0, violations.size()); }
@Test public void testGetViolationsWithPolicy4() { IPolicy policy; System.out.println("---testEvaluateWithPolicy4()---"); List<IMonitoringMetric> metrics = setUpWithPolicies(); policy = newPolicy("LATENCY", 3, new Date(10000)); term.setPolicies(Arrays.asList(new IPolicy[] { policy })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(1, violations.size()); assertViolations(violations, allMetrics.get(0), allMetrics.get(4), allMetrics.get(5)); }
@Test public void testGetViolationsWithPolicy5() { System.out.println("---testEvaluateWithPolicy5()---"); List<IMonitoringMetric> metrics = setUpWithPolicies(); term.setPolicies(Arrays.asList(new IPolicy[] { newPolicy("LATENCY", 3, new Date(10000)), newPolicy("LATENCY", 2, new Date(3000)) })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(1, violations.size()); assertViolations(violations, allMetrics.get(0), allMetrics.get(4), allMetrics.get(5)); assertEquals(10000, violations.get(0).getPolicy().getTimeInterval().getTime()); }
@Test public void testGetViolationsWithPolicy6() { System.out.println("---testEvaluateWithPolicy6()---"); List<IMonitoringMetric> metrics = setUpWithPolicies(); term.setPolicies(Arrays.asList(new IPolicy[] { newPolicy("LATENCY", 3, new Date(10000)), newPolicy("LATENCY", 2, new Date(5000)) })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(2, violations.size()); assertEquals(10000, violations.get(0).getPolicy().getTimeInterval().getTime()); assertEquals(5000, violations.get(1).getPolicy().getTimeInterval().getTime()); assertViolations( Collections.singletonList(violations.get(0)), allMetrics.get(0), allMetrics.get(4), allMetrics.get(5) ); assertViolations( Collections.singletonList(violations.get(1)), allMetrics.get(4), allMetrics.get(5) ); }