A variational approach to the mean field planning problem