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); } for (IPolicy policy : policies) { Date breachesBegin = calcBreachesBegin(agreement, now, kpiName, policy, metrics); 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)) { List<IViolation> policyViolations = violationsFromBreachesBuilder.build( agreement, term, kpiName, breaches, policy); newViolations.addAll(policyViolations); 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"); } } } if (withPolicies) { saveBreaches(newBreaches); } 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); IViolationRepository getViolationRepository(); void setViolationRepository(IViolationRepository violationRepository);  }
@Test public void testGetViolationsWithPolicy1() { IPolicy policy; System.out.println("---testEvaluateWithPolicy1()---"); List<IMonitoringMetric> metrics = setUpWithPolicies(null, null); policy = newPolicy("LATENCY", 2, new Date(900)); term.setPolicies(Arrays.asList(new IPolicy[] { policy })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(0, violations.size()); }
@Test public void testNewBreachesAreSaved() { List<IMonitoringMetric> metrics = setUpWithPolicies(null, null); IPolicy policy = newPolicy("LATENCY", 2, new Date(3000)); term.setPolicies(Arrays.asList(new IPolicy[] { policy })); violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(3, breachRepository.getBreachesByTimeRange(contract, kpiName, new Date(0), now).size()); assertEquals(1, breachRepository.getBreachesByTimeRange( contract, kpiName, new Date(now.getTime() - 3500), now).size()); }
@Test public void testGetViolationsWithPolicy2() { IPolicy policy; System.out.println("---testEvaluateWithPolicy2()---"); List<IMonitoringMetric> metrics = setUpWithPolicies(null, null); policy = newPolicy("LATENCY", 2, new Date(4000)); 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(null, null); policy = newPolicy("LATENCY", 3, new Date(4000)); 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(null, null); 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(null, null); term.setPolicies(Arrays.asList(new IPolicy[] { newPolicy("LATENCY", 3, new Date(4000)), newPolicy("LATENCY", 2, new Date(3000)) })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(1, violations.size()); assertViolations(violations, t(-3), t(-2)); assertEquals(3000, violations.get(0).getPolicy().getTimeInterval().getTime()); }
@Test public void testGetViolationsWithPolicy6() { System.out.println("---testEvaluateWithPolicy6()---"); List<IMonitoringMetric> metrics = setUpWithPolicies(null, null); term.setPolicies(Arrays.asList(new IPolicy[] { newPolicy("LATENCY", 3, new Date(10000)), newPolicy("LATENCY", 2, new Date(4000)) })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(2, violations.size()); assertEquals(10000, violations.get(0).getPolicy().getTimeInterval().getTime()); assertEquals(4000, violations.get(1).getPolicy().getTimeInterval().getTime()); assertViolations( Collections.singletonList(violations.get(0)), t(-7), t(-3), t(-2) ); assertViolations( Collections.singletonList(violations.get(1)), t(-3), t(-2) ); }
@Test public void testPolicyWithMultipleViolationsRaised() { List<IMonitoringMetric> metrics = setUpWithPolicies(new String[] { "103" }, null); IPolicy policy = newPolicy("LATENCY", 2, new Date(10000)); term.setPolicies(Arrays.asList(new IPolicy[] { policy })); List<IViolation> violations = evaluator.evaluate(contract, term, metrics, now); assertEquals(2, violations.size()); assertViolations(violations, t(-8), t(-4), t(-3), t(0)); }
@Test public void testGapBetweenBreaches1() { String[] oldValues = new String[] { "99", "99", "100" }; String[] newValues = new String[] { "99", "101", "99", "99", "102", "103", "104", "99", "99", "105" }; List<IMonitoringMetric> newMetrics = setUpMetrics(oldValues, newValues, null); IPolicy policy = newPolicy("LATENCY", 2, new Date(1900)); term.setPolicies(Arrays.asList(new IPolicy[] { policy })); List<IViolation> violations = evaluator.evaluate(contract, term, newMetrics, now); assertEquals(1, violations.size()); assertViolations(violations, t(-5), t(-4)); }