'Generate Openshift route templates or playbook tasks. Each port on a service definition found in container.yml
represents an externally exposed port.'

def get_route_templates(self):

    def _get_published_ports(service_config):
        result = []
        for port in service_config.get('ports', []):
            protocol = 'TCP'
            if (isinstance(port, string_types) and ('/' in port)):
                (port, protocol) = port.split('/')
            if (isinstance(port, string_types) and (':' in port)):
                (host, container) = port.split(':')
            else:
                host = port
            result.append({'port': host, 'protocol': protocol.lower()})
        return result
    templates = []
    for (name, service_config) in self._services.items():
        state = service_config.get(self.CONFIG_KEY, {}).get('state', 'present')
        force = service_config.get(self.CONFIG_KEY, {}).get('force', False)
        published_ports = _get_published_ports(service_config)
        if (state != 'present'):
            continue
        for port in published_ports:
            route_name = ('%s-%s' % (name, port['port']))
            labels = dict(app=self._namespace_name, service=name)
            template = CommentedMap()
            template['apiVersion'] = self.DEFAULT_API_VERSION
            template['kind'] = 'Route'
            template['force'] = force
            template['metadata'] = CommentedMap([('name', route_name), ('namespace', self._namespace_name), ('labels', labels.copy())])
            template['spec'] = CommentedMap([('to', CommentedMap([('kind', 'Service'), ('name', name)])), ('port', CommentedMap([('targetPort', 'port-{}-{}'.format(port['port'], port['protocol']))]))])
            if service_config.get(self.CONFIG_KEY, {}).get('routes'):
                for route in service_config[self.CONFIG_KEY]['routes']:
                    if (str(route.get('port')) == str(port['port'])):
                        for (key, value) in route.items():
                            if (key not in ('force', 'port')):
                                self.copy_attribute(template['spec'], key, value)
            templates.append(template)
    return templates
