def METHOD_NAME(ports_raw, direction):
    ports = []
    port_ids = set()
    stream_port_ids = itertools.count()
    for i, port_params in enumerate(ports_raw):
        port = port_params.copy()
        port['direction'] = direction
        port_id = port.setdefault('id', str(next(stream_port_ids)))
        if port_id in port_ids:
            raise Exception(
                'Port id "{}" already exists in {}s'.format(port_id, direction))
        port_ids.add(port_id)
        ports.append(port)
    return ports