class StackCreatorService {

    void fillInstanceMetadata(Stack stack) {
        long privateIdNumber = 0;
        //Gateway HostGroups are sorted first to start with privateIdNumber 0.
        List<InstanceGroup> sortedInstanceGroups = stack.getInstanceGroups().stream()
                .sorted(Comparator.comparing(InstanceGroup::getInstanceGroupType)
                        .thenComparing(InstanceGroup::getGroupName)).collect(Collectors.toList());
        for (InstanceGroup instanceGroup : sortedInstanceGroups) {
            for (InstanceMetaData instanceMetaData : instanceGroup.getAllInstanceMetaData()) {
                instanceMetaData.setPrivateId(privateIdNumber++);
                instanceMetaData.setInstanceStatus(InstanceStatus.REQUESTED);
            }
        }
    }

}