@VisibleForTesting
  static ImmutableSet<Port> portMapToSet(@Nullable Map<String, Map<String, String>> portMap)
      throws BadContainerConfigurationFormatException {
    if (portMap == null) {
      return ImmutableSet.of();
    }
    ImmutableSet.Builder<Port> ports = new ImmutableSet.Builder<>();
    for (Map.Entry<String, Map<String, String>> entry : portMap.entrySet()) {
      String port = entry.getKey();
      Matcher matcher = PORT_PATTERN.matcher(port);
      if (!matcher.matches()) {
        throw new BadContainerConfigurationFormatException(
            "Invalid port configuration: '" + port + "'.");
      }

      int portNumber = Integer.parseInt(matcher.group("portNum"));
      String protocol = matcher.group("protocol");
      ports.add(Port.parseProtocol(portNumber, protocol));
    }
    return ports.build();
  }
@Test
  public void testPortMapToList() throws BadContainerConfigurationFormatException {
    ImmutableSortedMap<String, Map<String, String>> input =
        ImmutableSortedMap.of(
            "1000",
            ImmutableMap.of(),
            "2000/tcp",
            ImmutableMap.of(),
            "3000/udp",
            ImmutableMap.of());
    ImmutableSet<Port> expected = ImmutableSet.of(Port.tcp(1000), Port.tcp(2000), Port.udp(3000));
    Assert.assertEquals(expected, JsonToImageTranslator.portMapToSet(input));

    ImmutableList<Map<String, Map<String, String>>> badInputs =
        ImmutableList.of(
            ImmutableMap.of("abc", ImmutableMap.of()),
            ImmutableMap.of("1000-2000", ImmutableMap.of()),
            ImmutableMap.of("/udp", ImmutableMap.of()),
            ImmutableMap.of("123/xxx", ImmutableMap.of()));
    for (Map<String, Map<String, String>> badInput : badInputs) {
      try {
        JsonToImageTranslator.portMapToSet(badInput);
        Assert.fail();
      } catch (BadContainerConfigurationFormatException ignored) {
        // ignored
      }
    }
  }