@Override
  @SuppressWarnings("unchecked")
  public WorkflowContext invoke(WorkflowContext context) {
    List<ConnectionGroupPortfolioDto> portfolioDTOs =
        context.get(
            AgrNonUdiRetrieveAdsGoalRealizationParameter.IN.CURRENT_PORTFOLIO.name(), List.class);
    LocalDate period =
        context.get(AgrNonUdiRetrieveAdsGoalRealizationParameter.IN.PERIOD.name(), LocalDate.class);
    Integer ptuDuration =
        context.get(
            AgrNonUdiRetrieveAdsGoalRealizationParameter.IN.PTU_DURATION.name(), Integer.class);

    List<ConnectionGroupPortfolioDto> updatedPortfolio = new ArrayList<>();
    for (ConnectionGroupPortfolioDto connectionGroupPortfolioDto : portfolioDTOs) {
      if (connectionGroupPortfolioDto.getUsefRole() == USEFRoleDto.BRP) {
        updatedPortfolio.add(
            handleBrpConnectionGroupPortfolio(connectionGroupPortfolioDto, period, ptuDuration));
      } else {
        updatedPortfolio.add(
            handleCongestionPointConnectionGroupPortfolio(
                connectionGroupPortfolioDto, period, ptuDuration));
      }
    }
    context.setValue(
        AgrNonUdiRetrieveAdsGoalRealizationParameter.OUT.UPDATED_PORTFOLIO.name(),
        updatedPortfolio);
    return context;
  }
 private ConnectionGroupPortfolioDto handleBrpConnectionGroupPortfolio(
     ConnectionGroupPortfolioDto connectionGroupPortfolioDto,
     LocalDate period,
     Integer ptuDuration) {
   Optional<ObjectiveAgentStatusDto> objectiveAgentStatusDtoOptional =
       powerMatcher.retrieveBrpAdsGoalRealization(connectionGroupPortfolioDto.getUsefIdentifier());
   objectiveAgentStatusDtoOptional.ifPresent(
       objectiveAgentStatusDto -> {
         Integer ptuIndex = PtuUtil.getPtuIndex(new LocalDateTime(), ptuDuration);
         updateObservedValue(
             connectionGroupPortfolioDto,
             period,
             ptuIndex,
             objectiveAgentStatusDto.getCurrentAllocation().toBigInteger());
       });
   return connectionGroupPortfolioDto;
 }
  private void updateObservedValue(
      ConnectionGroupPortfolioDto connectionGroupPortfolioDto,
      LocalDate period,
      Integer ptuIndex,
      BigInteger observedPower) {
    if (connectionGroupPortfolioDto.getConnectionGroupPowerPerPTU().get(ptuIndex) == null) {
      connectionGroupPortfolioDto
          .getConnectionGroupPowerPerPTU()
          .put(ptuIndex, new PowerContainerDto(period, ptuIndex));
    }

    connectionGroupPortfolioDto
        .getConnectionGroupPowerPerPTU()
        .get(ptuIndex)
        .getObserved()
        .setUncontrolledLoad(BigInteger.ZERO);
    if (observedPower.compareTo(BigInteger.ZERO) >= 0) {
      connectionGroupPortfolioDto
          .getConnectionGroupPowerPerPTU()
          .get(ptuIndex)
          .getObserved()
          .setAverageConsumption(observedPower);
      connectionGroupPortfolioDto
          .getConnectionGroupPowerPerPTU()
          .get(ptuIndex)
          .getObserved()
          .setAverageProduction(BigInteger.ZERO);
    } else {
      connectionGroupPortfolioDto
          .getConnectionGroupPowerPerPTU()
          .get(ptuIndex)
          .getObserved()
          .setAverageProduction(observedPower);
      connectionGroupPortfolioDto
          .getConnectionGroupPowerPerPTU()
          .get(ptuIndex)
          .getObserved()
          .setAverageConsumption(BigInteger.ZERO);
    }
  }