static PiActionProfileGroup translate(Group group, PiPipeconf pipeconf, Device device)
            throws PiTranslationException {

        if (!SUPPORTED_GROUP_TYPES.contains(group.type())) {
            throw new PiTranslationException(format(
                    "group type %s not supported", group.type()));
        }

        // Get action profile from group key.
        // TODO: define proper field in group class.
        if (!(group.appCookie() instanceof PiGroupKey)) {
            throw new PiTranslationException(
                    "group app cookie is not PI (class should be PiGroupKey)");
        }
        final PiGroupKey groupKey = (PiGroupKey) group.appCookie();
        final PiActionProfileId actionProfileId = groupKey.actionProfileId();

        // Check validity of action profile against pipeconf.
        final PiActionProfileModel actionProfileModel = pipeconf.pipelineModel()
                .actionProfiles(actionProfileId)
                .orElseThrow(() -> new PiTranslationException(format(
                        "no such action profile '%s'", actionProfileId)));
        if (!actionProfileModel.hasSelector()) {
            throw new PiTranslationException(format(
                    "action profile '%s' does not support dynamic selection",
                    actionProfileId));
        }

        // Check if the table associated with the action profile supports only
        // one-shot action profile programming.
        boolean isTableOneShot = actionProfileModel.tables().stream()
                .map(tableId -> pipeconf.pipelineModel().table(tableId))
                .allMatch(piTableModel -> piTableModel.isPresent() &&
                        piTableModel.get().oneShotOnly());
        if (isTableOneShot) {
            throw new PiTranslationException(format(
                    "Table associated to action profile '%s' supports only one-shot action profile programming",
                    actionProfileId));
        }

        // Check group validity.
        if (actionProfileModel.maxGroupSize() > 0
                && group.buckets().buckets().size() > actionProfileModel.maxGroupSize()) {
            throw new PiTranslationException(format(
                    "too many buckets, max group size for action profile '%s' is %d",
                    actionProfileId, actionProfileModel.maxGroupSize()));
        }

        // If not INDIRECT, we set the maximum group size as specified in the
        // model, however this might be highly inefficient for some HW targets
        // which pre-allocate resources for the whole group.
        final int maxGroupSize = group.type() == GroupDescription.Type.INDIRECT
                ? 1 : actionProfileModel.maxGroupSize();

        final PiActionProfileGroup.Builder piActionGroupBuilder = PiActionProfileGroup.builder()
                .withId(PiActionProfileGroupId.of(group.id().id()))
                .withActionProfileId(groupKey.actionProfileId())
                .withMaxSize(maxGroupSize);

        // Translate group buckets to PI group members
        final PiPipelineInterpreter interpreter = getInterpreterOrNull(device, pipeconf);
        short bucketIdx = 0;
        for (GroupBucket bucket : group.buckets().buckets()) {
            /*
            FIXME: the way member IDs are computed can cause collisions!
            Problem: In P4Runtime action profile members, i.e. action buckets,
            are associated to a numeric ID chosen at member insertion time. This
            ID must be unique for the whole action profile (i.e. the group table
            in OpenFlow). In ONOS, GroupBucket doesn't specify any ID.

            Solutions:
            - Change GroupBucket API to force application wanting to perform
            group operations to specify a member id.
            - Maintain state to dynamically allocate/deallocate member IDs, e.g.
            in a dedicated service, or in a P4Runtime Group Provider.

            Hack: Statically derive member ID by combining groupId and position
            of the bucket in the list.
             */
            final int memberId = Objects.hash(group.id(), bucketIdx);
            if (memberId == 0) {
                throw new PiTranslationException(
                        "GroupBucket produces PiActionProfileMember " +
                                "with invalid ID 0");
            }
            bucketIdx++;

            final PiTableAction tableAction = translateTreatment(
                    bucket.treatment(), interpreter,
                    groupKey.tableId(), pipeconf.pipelineModel());
            if (tableAction == null) {
                throw new PiTranslationException(
                        "bucket treatment translator returned null");
            }

            if (tableAction.type() != ACTION) {
                throw new PiTranslationException(format(
                        "action of type '%s' cannot be used in action profile members",
                        tableAction.type()));
            }

            final PiActionProfileMember member = PiActionProfileMember.builder()
                    .forActionProfile(groupKey.actionProfileId())
                    .withId(PiActionProfileMemberId.of(memberId))
                    .withAction((PiAction) tableAction)
                    .build();

            // NOTE Indirect groups have weight set to -1 which is not supported
            // by P4RT - setting to 1 to avoid problems with the p4rt server.
            final int weight = group.type() == GroupDescription.Type.INDIRECT ? 1 : bucket.weight();
            piActionGroupBuilder.addMember(member, weight);
        }

        return piActionGroupBuilder.build();
    }
@Test
    public void testTranslateGroups() throws Exception {

        PiActionProfileGroup piGroup1 = PiGroupTranslatorImpl.translate(SELECT_GROUP, pipeconf, null);
        PiActionProfileGroup piGroup2 = PiGroupTranslatorImpl.translate(SELECT_GROUP, pipeconf, null);

        new EqualsTester()
                .addEqualityGroup(piGroup1, piGroup2)
                .testEquals();

        assertThat("Group ID must be equal",
                   piGroup1.id().id(), is(equalTo(GROUP_ID.id())));
        assertThat("Action profile ID must be equal",
                   piGroup1.actionProfile(), is(equalTo(INGRESS_WCMP_CONTROL_WCMP_SELECTOR)));

        // members installed
        Collection<PiActionProfileGroup.WeightedMember> weightedMembers = piGroup1.members();
        Collection<PiActionProfileMember> memberInstances = weightedMembers.stream()
                .map(PiActionProfileGroup.WeightedMember::instance)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        assertThat("The number of group members must be equal",
                   piGroup1.members().size(), is(expectedWeightedMembers.size()));
        assertThat("Group weighted members must be equal",
                   weightedMembers.containsAll(expectedWeightedMembers)
                           && expectedWeightedMembers.containsAll(weightedMembers));
        assertThat("Group member instances must be equal",
                   memberInstances.containsAll(expectedMemberInstances)
                           && expectedMemberInstances.containsAll(memberInstances));

    }