class CommunityContext {

  public static @Nonnull CommunityContext fromEnvironment(Environment environment) {
    CommunitySet inputCommunitySet;
    if (environment.getUseOutputAttributes()
        && environment.getOutputRoute() instanceof BgpRoute.Builder<?, ?>) {
      BgpRoute.Builder<?, ?> bgpRouteBuilder =
          (BgpRoute.Builder<?, ?>) environment.getOutputRoute();
      inputCommunitySet = CommunitySet.of(bgpRouteBuilder.getCommunities());
    } else if (environment.getReadFromIntermediateBgpAttributes()) {
      inputCommunitySet =
          CommunitySet.of(environment.getIntermediateBgpAttributes().getCommunities());
    } else if (environment.getOriginalRoute() instanceof BgpRoute) {
      BgpRoute<?, ?> bgpRoute = (BgpRoute<?, ?>) environment.getOriginalRoute();
      inputCommunitySet = bgpRoute.getCommunities();
    } else {
      inputCommunitySet = CommunitySet.empty();
    }
    return CommunityContext.builder()
        .setCommunityMatchExprs(environment.getCommunityMatchExprs())
        .setCommunitySetExprs(environment.getCommunitySetExprs())
        .setCommunitySetMatchExprs(environment.getCommunitySetMatchExprs())
        .setCommunitySets(environment.getCommunitySets())
        .setInputCommunitySet(inputCommunitySet)
        .build();
  }

  private  CommunityContext(
      Map<String, CommunityMatchExpr> communityMatchExprs,
      Map<String, CommunitySetExpr> communitySetExprs,
      Map<String, CommunitySetMatchExpr> communitySetMatchExprs,
      Map<String, CommunitySet> communitySets,
      CommunitySet inputCommunitySet);

}