New York - A federal judge on Friday found that the National Security Agency's bulk collection of millions of Americans' telephone records is legal and a valuable part of the nation's arsenal to counter the threat of terrorism.The decision contrasts with a ruling earlier this month by another US District Court judge, Richard Leon, who granted a preliminary injunction against the collecting of phone records of two men who had challenged the programme. Leon said the programme likely violates the Constitution's ban on unreasonable search.On Friday, US District Judge William Pauley said in a written opinion that the programme "represents the government's counter-punch" to eliminate al-Qaeda's terror network by connecting fragmented and fleeting communications.In his ruling, the judge noted the 11 September 2011 terrorist attacks and how the phone data-collection system could have helped investigators connect information before the attacks occurred."The government learned from its mistake and adapted to confront a new enemy: a terror network capable of orchestrating attacks across the world. It launched a number of counter-measures, including a bulk telephony metadata collection programme - a wide net that could find and isolate gossamer contacts among suspected terrorists in an ocean of seemingly disconnected data," he said.Pauley dismissed a lawsuit brought by the American Civil Liberties Union. The rights group did not immediately respond to a message for comment."We are pleased with the decision," Justice Department spokesperson Peter Carr said.The ACLU sued earlier this year after former NSA analyst Edward Snowden leaked details of the secret programmes that critics say violate privacy rights. The NSA-run programmes pick up millions of telephone and Internet records that are routed through American networks each day.An ACLU lawyer had argued that the government's interpretation of its authority under the Patriot Act was so broad that it could justify the mass collection of financial, health and even library records of innocent Americans without their knowledge. A government lawyer had countered that counterterrorism investigators wouldn't find most personal information useful.