Search in sources :

Example 16 with Contract

use of gov.cms.ab2d.common.model.Contract in project ab2d by CMSgov.

the class CoverageDriverImpl method isCoverageAvailable.

/**
 * Determine whether database contains all necessary enrollment for a contract and that no updates to that
 * enrollment are currently occurring.
 *
 * Steps
 *      - Lock coverage so no other workers can modify coverage while this check is occurring
 *      - Create any {@link CoveragePeriod}s that are currently missing
 *      - Check the following to determine whether a job can run (return false if any are not met)
 *          - Look for whether months have failed to update during earlier attempts
 *          - Look for coverage periods that have never been successfully searched and queue them
 *          - Look for coverage periods currently being updated
 *
 * @param job job to check for coverage
 * @throws CoverageDriverException if enrollment state violates assumed preconditions or database lock cannot be retrieved
 * @throws InterruptedException if trying to lock the table is interrupted
 */
@Trace(metricName = "EnrollmentIsAvailable", dispatcher = true)
@Override
public boolean isCoverageAvailable(Job job, ContractDTO contract) throws InterruptedException {
    String contractNumber = job.getContractNumber();
    assert contractNumber.equals(contract.getContractNumber());
    Lock coverageLock = coverageLockWrapper.getCoverageLock();
    // Track whether locked or not to prevent an illegal monitor exception
    boolean locked = false;
    try {
        locked = coverageLock.tryLock(MINUTE, TimeUnit.MINUTES);
        if (!locked) {
            log.warn("Could not retrieve lock after timeout of {} minute(s)." + " Cannot confirm coverage metadata is available", MINUTE);
            return false;
        }
        // Check whether a coverage period is missing for this contract.
        // If so then create those coverage periods.
        discoverCoveragePeriods(mapping.map(contract));
        log.info("queueing never searched coverage metadata periods for {}", contractNumber);
        /*
             * If any relevant coverage period has never been pulled from BFD successfully then automatically fail the
             * search
             */
        List<CoveragePeriod> neverSearched = coverageService.coveragePeriodNeverSearchedSuccessfully().stream().filter(period -> Objects.equals(contract.getContractNumber(), period.getContractNumber())).toList();
        if (!neverSearched.isEmpty()) {
            // Check that we've not submitted and failed these jobs
            neverSearched.forEach(period -> checkCoveragePeriodValidity(job, period));
            // Add all never searched coverage periods to the queue for processing
            neverSearched.forEach(period -> coverageProcessor.queueCoveragePeriod(period, false));
            return false;
        }
        log.info("checking whether any coverage metadata is currently being updated for {}", contractNumber);
        /*
             * If coverage periods are submitted, in progress or null then ignore for the moment.
             *
             * There will always be at least one coverage period returned.
             */
        List<CoveragePeriod> periods = coverageService.findAssociatedCoveragePeriods(contract.getContractNumber());
        if (periods.isEmpty()) {
            log.error("There are no existing coverage periods for this job so no metadata exists");
            throw new CoverageDriverException("There are no existing coverage periods for this job so no ");
        }
        return periods.stream().map(CoveragePeriod::getStatus).noneMatch(status -> status == null || status == CoverageJobStatus.IN_PROGRESS || status == CoverageJobStatus.SUBMITTED);
    } catch (InterruptedException interruptedException) {
        log.error("Interrupted attempting to retrieve lock. Cannot confirm coverage metadata is available");
        throw interruptedException;
    } finally {
        if (locked) {
            coverageLock.unlock();
        }
    }
}
Also used : Trace(com.newrelic.api.agent.Trace) ContractForCoverageDTO(gov.cms.ab2d.coverage.model.ContractForCoverageDTO) ZonedDateTime(java.time.ZonedDateTime) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) Constants(gov.cms.ab2d.common.util.Constants) Scheduled(org.springframework.scheduling.annotation.Scheduled) CoverageUtils.getEndDateTime(gov.cms.ab2d.worker.processor.coverage.CoverageUtils.getEndDateTime) CoverageJobStatus(gov.cms.ab2d.coverage.model.CoverageJobStatus) ArrayList(java.util.ArrayList) CoveragePagingRequest(gov.cms.ab2d.coverage.model.CoveragePagingRequest) CoveragePresentCheck(gov.cms.ab2d.worker.processor.coverage.check.CoveragePresentCheck) CoverageCount(gov.cms.ab2d.coverage.model.CoverageCount) CoverageNoDuplicatesCheck(gov.cms.ab2d.worker.processor.coverage.check.CoverageNoDuplicatesCheck) Service(org.springframework.stereotype.Service) AB2D_ZONE(gov.cms.ab2d.common.util.DateUtil.AB2D_ZONE) Map(java.util.Map) CoverageStableCheck(gov.cms.ab2d.worker.processor.coverage.check.CoverageStableCheck) ContractToContractCoverageMapping(gov.cms.ab2d.worker.config.ContractToContractCoverageMapping) ZoneOffset(java.time.ZoneOffset) ContractDTO(gov.cms.ab2d.common.dto.ContractDTO) LinkedHashSet(java.util.LinkedHashSet) DateUtil(gov.cms.ab2d.common.util.DateUtil) Contract(gov.cms.ab2d.common.model.Contract) Job(gov.cms.ab2d.common.model.Job) CoverageSearchRepository(gov.cms.ab2d.coverage.repository.CoverageSearchRepository) CoveragePagingResult(gov.cms.ab2d.coverage.model.CoveragePagingResult) CoverageUpToDateCheck(gov.cms.ab2d.worker.processor.coverage.check.CoverageUpToDateCheck) Set(java.util.Set) CoverageUtils.getAttestationTime(gov.cms.ab2d.worker.processor.coverage.CoverageUtils.getAttestationTime) CoverageService(gov.cms.ab2d.coverage.service.CoverageService) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) AB2D_EPOCH(gov.cms.ab2d.common.util.DateUtil.AB2D_EPOCH) List(java.util.List) Lock(java.util.concurrent.locks.Lock) Slf4j(lombok.extern.slf4j.Slf4j) OffsetDateTime(java.time.OffsetDateTime) ChronoUnit(java.time.temporal.ChronoUnit) DayOfWeek(java.time.DayOfWeek) PropertiesService(gov.cms.ab2d.common.service.PropertiesService) CoveragePeriodsPresentCheck(gov.cms.ab2d.worker.processor.coverage.check.CoveragePeriodsPresentCheck) PdpClientService(gov.cms.ab2d.common.service.PdpClientService) CoverageMapping(gov.cms.ab2d.coverage.model.CoverageMapping) CoveragePeriod(gov.cms.ab2d.coverage.model.CoveragePeriod) TemporalAdjusters(java.time.temporal.TemporalAdjusters) Optional(java.util.Optional) CoverageSearch(gov.cms.ab2d.coverage.model.CoverageSearch) CoveragePeriod(gov.cms.ab2d.coverage.model.CoveragePeriod) Lock(java.util.concurrent.locks.Lock) Trace(com.newrelic.api.agent.Trace)

Example 17 with Contract

use of gov.cms.ab2d.common.model.Contract in project ab2d by CMSgov.

the class HPMSManualModeTest method buildContract.

@NotNull
private Contract buildContract(Contract.UpdateMode updateMode) {
    Contract contract = new Contract();
    contract.setContractNumber(TEST_CONTRACT_NUMBER);
    contract.setContractName("Manual Mode Test");
    contract.setAttestedOn(OffsetDateTime.now());
    contract.setUpdateMode(updateMode);
    return contract;
}
Also used : Contract(gov.cms.ab2d.common.model.Contract) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with Contract

use of gov.cms.ab2d.common.model.Contract in project ab2d by CMSgov.

the class AttestationUpdaterServiceImpl method updateContract.

private Optional<Contract> updateContract(HPMSOrganizationInfo hpmsInfo) {
    Optional<Contract> contractHolder = contractRepository.findContractByContractNumber(hpmsInfo.getContractId());
    if (contractHolder.isEmpty())
        return contractHolder;
    Contract contract = contractHolder.get();
    return contract.isAutoUpdatable() && hpmsInfo.hasChanges(contract) ? Optional.of(hpmsInfo.updateContract(contract)) : Optional.empty();
}
Also used : Contract(gov.cms.ab2d.common.model.Contract)

Example 19 with Contract

use of gov.cms.ab2d.common.model.Contract in project ab2d by CMSgov.

the class AttestationUpdaterServiceTest method updateCoverage.

@Test
void updateCoverage() {
    HPMSOrganizationInfo info = new HPMSOrganizationInfo();
    info.setParentOrgId(2);
    Contract contract = new Contract();
    contract.setHpmsParentOrgId(1L);
    info.updateContract(contract);
    assertEquals(2L, (long) contract.getHpmsParentOrgId());
}
Also used : HPMSOrganizationInfo(gov.cms.ab2d.hpms.hmsapi.HPMSOrganizationInfo) Contract(gov.cms.ab2d.common.model.Contract) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 20 with Contract

use of gov.cms.ab2d.common.model.Contract in project ab2d by CMSgov.

the class AttestationUpdaterServiceTest method hasChanges.

@Test
void hasChanges() {
    HPMSOrganizationInfo info = new HPMSOrganizationInfo();
    info.setParentOrgId(2);
    Contract contract = new Contract();
    contract.setHpmsParentOrgId(1L);
    assertTrue(info.hasChanges(contract));
}
Also used : HPMSOrganizationInfo(gov.cms.ab2d.hpms.hmsapi.HPMSOrganizationInfo) Contract(gov.cms.ab2d.common.model.Contract) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Contract (gov.cms.ab2d.common.model.Contract)36 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)15 Test (org.junit.jupiter.api.Test)13 PdpClient (gov.cms.ab2d.common.model.PdpClient)6 CoveragePeriod (gov.cms.ab2d.coverage.model.CoveragePeriod)6 Job (gov.cms.ab2d.common.model.Job)5 ContractDTO (gov.cms.ab2d.common.dto.ContractDTO)4 OffsetDateTime (java.time.OffsetDateTime)4 ArrayList (java.util.ArrayList)4 DisplayName (org.junit.jupiter.api.DisplayName)4 PdpClientDTO (gov.cms.ab2d.common.dto.PdpClientDTO)3 PdpClientService (gov.cms.ab2d.common.service.PdpClientService)3 PropertiesService (gov.cms.ab2d.common.service.PropertiesService)3 Constants (gov.cms.ab2d.common.util.Constants)3 ContractForCoverageDTO (gov.cms.ab2d.coverage.model.ContractForCoverageDTO)3 CoverageMapping (gov.cms.ab2d.coverage.model.CoverageMapping)3 CoverageSearch (gov.cms.ab2d.coverage.model.CoverageSearch)3 CoverageSearchRepository (gov.cms.ab2d.coverage.repository.CoverageSearchRepository)3 CoverageService (gov.cms.ab2d.coverage.service.CoverageService)3 ContractToContractCoverageMapping (gov.cms.ab2d.worker.config.ContractToContractCoverageMapping)3