    try:
        return unicode(obj)
    except UnicodeDecodeError:
        return obj.decode('utf-8')

# Universal Analytics for Python
# Analytics Pros offers consulting and integration services if your firm needs
# assistance in strategy, implementation, or auditing existing work.

from urllib2 import urlopen, build_opener, install_opener
from urllib2 import Request, HTTPSHandler
from urllib2 import URLError, HTTPError
from urllib import urlencode

import random
import datetime
import time
import uuid
import hashlib
import socket

def generate_uuid(basedata = None):
    """ Provides a _random_ UUID with no input, or a UUID4-format MD5 checksum of any input data provided """
    if basedata is None:
        return str(uuid.uuid4())
    elif isinstance(basedata, basestring):
        checksum = hashlib.md5(basedata).hexdigest()
        return '%8s-%4s-%4s-%4s-%12s' % (checksum[0:8], checksum[8:12], checksum[12:16], checksum[16:20], checksum[20:32])

class Time(datetime.datetime):
    """ Wrappers and convenience methods for processing various time representations """

    @classmethod
    def from_unix(cls, seconds, milliseconds = 0):
        """ Produce a full |datetime.datetime| object from a Unix timestamp """
        base = list(time.gmtime(seconds))[0:6]
        base.append(milliseconds * 1000) # microseconds
        return cls(* base)

    @classmethod
    def to_unix(cls, timestamp):
        """ Wrapper over time module to produce Unix epoch time as a float """
        if not isinstance(timestamp, datetime.datetime):
            raise TypeError, 'Time.milliseconds expects a datetime object'
        base = time.mktime(timestamp.timetuple())
        return base

    @classmethod
    def milliseconds_offset(cls, timestamp, now = None):
        """ Offset time (in milliseconds) from a |datetime.datetime| object to now """
        if isinstance(timestamp, (int, float)):
            base = timestamp
        else:
            base = cls.to_unix(timestamp)
            base = base + (timestamp.microsecond / 1000000)
        if now is None:
            now = time.time()
        return (now - base) * 1000

class HTTPRequest(object):
    """ URL Construction and request handling abstraction.
        This is not intended to be used outside this module.

        Automates mapping of persistent state (i.e. query parameters)
        onto transcient datasets for each query.
    """

    endpoint = 'http://example.com'

    @staticmethod
    def debug():
        """ Activate debugging on urllib2 """
        handler = HTTPSHandler(debuglevel = 1)
        opener = build_opener(handler)
        install_opener(opener)

    # Store properties for all requests
    def __init__(self, user_agent = None, *args, **opts):
        self.user_agent = user_agent or 'Analytics Pros - Universal Analytics (Python)'

    @classmethod
    def fixUTF8(cls, data): # Ensure proper encoding for UA's servers...
        """ Convert all strings to UTF-8 """
        for key in data:
            if isinstance(data[ key ], basestring):
                data[ key ] = data[ key ].encode('utf-8')
        return data

    # Apply stored properties to the given dataset & POST to the configured endpoint
    def send(self, data):
        request = Request(
                self.endpoint + '?' + urlencode(self.fixUTF8(data)),
                headers = {
                    'User-Agent': self.user_agent
                }
            )
        self.open(request)

    def open(self, request):
        try:
            return urlopen(request)
        except HTTPError as e:
            return False
        except URLError as e:
            self.cache_request(request)
            return False

    def cache_request(self, request):
        # TODO: implement a proper caching mechanism here for re-transmitting hits
        pass

class HTTPPost(HTTPRequest):

    # Apply stored properties to the given dataset & POST to the configured endpoint
    def send(self, data):
        request = Request(
                self.endpoint,
                data = urlencode(self.fixUTF8(data)),
                headers = {
                    'User-Agent': self.user_agent
                }
            )
        self.open(request)

class Tracker(object):
    """ Primary tracking interface for Universal Analytics """
    params = None
    parameter_alias = {}
    valid_hittypes = ('pageview', 'event', 'social', 'screenview', 'transaction', 'item', 'exception', 'timing')

    @classmethod
    def alias(cls, typemap, base, *names):
        """ Declare an alternate (humane) name for a measurement protocol parameter """
        cls.parameter_alias[ base ] = (typemap, base)
        for i in names:
            cls.parameter_alias[ i ] = (typemap, base)

    @classmethod
    def coerceParameter(cls, name, value = None):
        if isinstance(name, basestring) and name[0] == '&':
            return name[1:], str(value)
        elif name in cls.parameter_alias:
            typecast, param_name = cls.parameter_alias.get(name)
            return param_name, typecast(value)
        else:
            raise KeyError, 'Parameter "{0}" is not recognized'.format(name)

    def payload(self, data):
        for key, value in data.iteritems():
            try:
                yield self.coerceParameter(key, value)
            except KeyError:
                continue

    option_sequence = {
        'pageview': [ (basestring, 'dp') ],
        'event': [ (basestring, 'ec'), (basestring, 'ea'), (basestring, 'el'), (int, 'ev') ],
        'social': [ (basestring, 'sn'), (basestring, 'sa'), (basestring, 'st') ],
        'timing': [ (basestring, 'utc'), (basestring, 'utv'), (basestring, 'utt'), (basestring, 'utl') ]
    }

    @classmethod
    def consume_options(cls, data, hittype, args):
        """ Interpret sequential arguments related to known hittypes based on declared structures """
        opt_position = 0
        data[ 't' ] = hittype # integrate hit type parameter
        if hittype in cls.option_sequence:
            for expected_type, optname in cls.option_sequence[ hittype ]:
                if opt_position < len(args) and isinstance(args[opt_position], expected_type):
                    data[ optname ] = args[ opt_position ]
                opt_position += 1

    @classmethod
    def hittime(cls, timestamp = None, age = None, milliseconds = None):
        """ Returns an integer represeting the milliseconds offset for a given hit (relative to now) """
        if isinstance(timestamp, (int, float)):
            return int(Time.milliseconds_offset(Time.from_unix(timestamp, milliseconds = milliseconds)))
        if isinstance(timestamp, datetime.datetime):
            return int(Time.milliseconds_offset(timestamp))
        if isinstance(age, (int, float)):
            return int(age * 1000) + (milliseconds or 0)

    @property
    def account(self):
        return self.params.get('tid', None)

    def __init__(self, account, name = None, client_id = None, hash_client_id = False, user_id = None, user_agent = None, use_post = True):

        if use_post is False:
            self.http = HTTPRequest(user_agent = user_agent)
        else:
            self.http = HTTPPost(user_agent = user_agent)

        self.params = { 'v': 1, 'tid': account }

        if client_id is None:
            client_id = generate_uuid()

        self.params[ 'cid' ] = client_id

        self.hash_client_id = hash_client_id

        if user_id is not None:
            self.params[ 'uid' ] = user_id

    def set_timestamp(self, data):
        """ Interpret time-related options, apply queue-time parameter as needed """
        if 'hittime' in data: # an absolute timestamp
            data['qt'] = self.hittime(timestamp = data.pop('hittime', None))
        if 'hitage' in data: # a relative age (in seconds)
            data['qt'] = self.hittime(age = data.pop('hitage', None))

    def send(self, hittype, *args, **data):
        """ Transmit HTTP requests to Google Analytics using the measurement protocol """

        if hittype not in self.valid_hittypes:
            raise KeyError('Unsupported Universal Analytics Hit Type: {0}'.format(repr(hittype)))

        self.set_timestamp(data)
        self.consume_options(data, hittype, args)

        for item in args: # process dictionary-object arguments of transcient data
            if isinstance(item, dict):
                for key, val in self.payload(item):
                    data[ key ] = val

        for k, v in self.params.iteritems(): # update only absent parameters
            if k not in data:
                data[ k ] = v

        data = dict(self.payload(data))

        if self.hash_client_id:
            data[ 'cid' ] = generate_uuid(data[ 'cid' ])

        # Transmit the hit to Google...
        self.http.send(data)

    # Setting persistent attibutes of the session/hit/etc (inc. custom dimensions/metrics)
    def set(self, name, value = None):
        if isinstance(name, dict):
            for key, value in name.iteritems():
                try:
                    param, value = self.coerceParameter(key, value)
                    self.params[param] = value
                except KeyError:
                    pass
        elif isinstance(name, basestring):
            try:
                param, value = self.coerceParameter(name, value)
                self.params[param] = value
            except KeyError:
                pass

    def __getitem__(self, name):
        param, value = self.coerceParameter(name, None)
        return self.params.get(param, None)

    def __setitem__(self, name, value):
        param, value = self.coerceParameter(name, value)
        self.params[param] = value

    def __delitem__(self, name):
        param, value = self.coerceParameter(name, None)
        if param in self.params:
            del self.params[param]

def safe_unicode(obj):
    """ Safe convertion to the Unicode string version of the object """

# Declaring name mappings for Measurement Protocol parameters
MAX_CUSTOM_DEFINITIONS = 200
MAX_EC_LISTS = 11  # 1-based index
MAX_EC_PRODUCTS = 11  # 1-based index
MAX_EC_PROMOTIONS = 11 # 1-based index

Tracker.alias(int, 'v', 'protocol-version')
Tracker.alias(safe_unicode, 'cid', 'client-id', 'clientId', 'clientid')
Tracker.alias(safe_unicode, 'tid', 'trackingId', 'account')
Tracker.alias(safe_unicode, 'uid', 'user-id', 'userId', 'userid')
Tracker.alias(safe_unicode, 'uip', 'user-ip', 'userIp', 'ipaddr')
Tracker.alias(safe_unicode, 'ua', 'userAgent', 'userAgentOverride', 'user-agent')
Tracker.alias(safe_unicode, 'dp', 'page', 'path')
Tracker.alias(safe_unicode, 'dt', 'title', 'pagetitle', 'pageTitle' 'page-title')
Tracker.alias(safe_unicode, 'dl', 'location')
Tracker.alias(safe_unicode, 'dh', 'hostname')
Tracker.alias(safe_unicode, 'sc', 'sessioncontrol', 'session-control', 'sessionControl')
Tracker.alias(safe_unicode, 'dr', 'referrer', 'referer')
Tracker.alias(int, 'qt', 'queueTime', 'queue-time')
Tracker.alias(safe_unicode, 't', 'hitType', 'hittype')
Tracker.alias(int, 'aip', 'anonymizeIp', 'anonIp', 'anonymize-ip')

# Campaign attribution
Tracker.alias(safe_unicode, 'cn', 'campaign', 'campaignName', 'campaign-name')
Tracker.alias(safe_unicode, 'cs', 'source', 'campaignSource', 'campaign-source')
Tracker.alias(safe_unicode, 'cm', 'medium', 'campaignMedium', 'campaign-medium')
Tracker.alias(safe_unicode, 'ck', 'keyword', 'campaignKeyword', 'campaign-keyword')
Tracker.alias(safe_unicode, 'cc', 'content', 'campaignContent', 'campaign-content')
Tracker.alias(safe_unicode, 'ci', 'campaignId', 'campaignID', 'campaign-id')

# Technical specs
Tracker.alias(safe_unicode, 'sr', 'screenResolution', 'screen-resolution', 'resolution')
Tracker.alias(safe_unicode, 'vp', 'viewport', 'viewportSize', 'viewport-size')
Tracker.alias(safe_unicode, 'de', 'encoding', 'documentEncoding', 'document-encoding')
Tracker.alias(int, 'sd', 'colors', 'screenColors', 'screen-colors')
Tracker.alias(safe_unicode, 'ul', 'language', 'user-language', 'userLanguage')

# Mobile app
Tracker.alias(safe_unicode, 'an', 'appName', 'app-name', 'app')
Tracker.alias(safe_unicode, 'cd', 'contentDescription', 'screenName', 'screen-name', 'content-description')
Tracker.alias(safe_unicode, 'av', 'appVersion', 'app-version', 'version')
Tracker.alias(safe_unicode, 'aid', 'appID', 'appId', 'application-id', 'app-id', 'applicationId')
Tracker.alias(safe_unicode, 'aiid', 'appInstallerId', 'app-installer-id')

# Ecommerce
Tracker.alias(safe_unicode, 'ta', 'affiliation', 'transactionAffiliation', 'transaction-affiliation')
Tracker.alias(safe_unicode, 'ti', 'transaction', 'transactionId', 'transaction-id')
Tracker.alias(float, 'tr', 'revenue', 'transactionRevenue', 'transaction-revenue')
Tracker.alias(float, 'ts', 'shipping', 'transactionShipping', 'transaction-shipping')
Tracker.alias(float, 'tt', 'tax', 'transactionTax', 'transaction-tax')
Tracker.alias(safe_unicode, 'cu', 'currency', 'transactionCurrency', 'transaction-currency') # Currency code, e.g. USD, EUR
Tracker.alias(safe_unicode, 'in', 'item-name', 'itemName')
Tracker.alias(float, 'ip', 'item-price', 'itemPrice')
Tracker.alias(float, 'iq', 'item-quantity', 'itemQuantity')
Tracker.alias(safe_unicode, 'ic', 'item-code', 'sku', 'itemCode')
Tracker.alias(safe_unicode, 'iv', 'item-variation', 'item-category', 'itemCategory', 'itemVariation')

# Events
Tracker.alias(safe_unicode, 'ec', 'event-category', 'eventCategory', 'category')
Tracker.alias(safe_unicode, 'ea', 'event-action', 'eventAction', 'action')
Tracker.alias(safe_unicode, 'el', 'event-label', 'eventLabel', 'label')
Tracker.alias(int, 'ev', 'event-value', 'eventValue', 'value')
Tracker.alias(int, 'ni', 'noninteractive', 'nonInteractive', 'noninteraction', 'nonInteraction')

# Social
Tracker.alias(safe_unicode, 'sa', 'social-action', 'socialAction')
Tracker.alias(safe_unicode, 'sn', 'social-network', 'socialNetwork')
Tracker.alias(safe_unicode, 'st', 'social-target', 'socialTarget')

# Exceptions
Tracker.alias(safe_unicode, 'exd', 'exception-description', 'exceptionDescription', 'exDescription')
Tracker.alias(int, 'exf', 'exception-fatal', 'exceptionFatal', 'exFatal')

# User Timing
Tracker.alias(safe_unicode, 'utc', 'timingCategory', 'timing-category')
Tracker.alias(safe_unicode, 'utv', 'timingVariable', 'timing-variable')
Tracker.alias(int, 'utt', 'time', 'timingTime', 'timing-time')
Tracker.alias(safe_unicode, 'utl', 'timingLabel', 'timing-label')
Tracker.alias(float, 'dns', 'timingDNS', 'timing-dns')
Tracker.alias(float, 'pdt', 'timingPageLoad', 'timing-page-load')
Tracker.alias(float, 'rrt', 'timingRedirect', 'timing-redirect')
Tracker.alias(safe_unicode, 'tcp', 'timingTCPConnect', 'timing-tcp-connect')
Tracker.alias(safe_unicode, 'srt', 'timingServerResponse', 'timing-server-response')

# Custom dimensions and metrics
for i in range(0,200):
    Tracker.alias(safe_unicode, 'cd{0}'.format(i), 'dimension{0}'.format(i))
    Tracker.alias(int, 'cm{0}'.format(i), 'metric{0}'.format(i))

# Enhanced Ecommerce
Tracker.alias(str, 'pa')  # Product action
Tracker.alias(str, 'tcc')  # Coupon code
Tracker.alias(unicode, 'pal')  # Product action list
Tracker.alias(int, 'cos')  # Checkout step
Tracker.alias(str, 'col')  # Checkout step option

Tracker.alias(str, 'promoa')  # Promotion action

for product_index in range(1, MAX_EC_PRODUCTS):
    Tracker.alias(str, 'pr{0}id'.format(product_index))  # Product SKU
    Tracker.alias(unicode, 'pr{0}nm'.format(product_index))  # Product name
    Tracker.alias(unicode, 'pr{0}br'.format(product_index))  # Product brand
    Tracker.alias(unicode, 'pr{0}ca'.format(product_index))  # Product category
    Tracker.alias(unicode, 'pr{0}va'.format(product_index))  # Product variant
    Tracker.alias(str, 'pr{0}pr'.format(product_index))  # Product price
    Tracker.alias(int, 'pr{0}qt'.format(product_index))  # Product quantity
    Tracker.alias(str, 'pr{0}cc'.format(product_index))  # Product coupon code
    Tracker.alias(int, 'pr{0}ps'.format(product_index))  # Product position

    for custom_index in range(MAX_CUSTOM_DEFINITIONS):
        Tracker.alias(str, 'pr{0}cd{1}'.format(product_index, custom_index))  # Product custom dimension
        Tracker.alias(int, 'pr{0}cm{1}'.format(product_index, custom_index))  # Product custom metric

    for list_index in range(1, MAX_EC_LISTS):
        Tracker.alias(str, 'il{0}pi{1}id'.format(list_index, product_index))  # Product impression SKU
        Tracker.alias(unicode, 'il{0}pi{1}nm'.format(list_index, product_index))  # Product impression name
        Tracker.alias(unicode, 'il{0}pi{1}br'.format(list_index, product_index))  # Product impression brand
        Tracker.alias(unicode, 'il{0}pi{1}ca'.format(list_index, product_index))  # Product impression category
        Tracker.alias(unicode, 'il{0}pi{1}va'.format(list_index, product_index))  # Product impression variant
        Tracker.alias(int, 'il{0}pi{1}ps'.format(list_index, product_index))  # Product impression position
        Tracker.alias(int, 'il{0}pi{1}pr'.format(list_index, product_index))  # Product impression price

        for custom_index in range(MAX_CUSTOM_DEFINITIONS):
            Tracker.alias(str, 'il{0}pi{1}cd{2}'.format(list_index, product_index, custom_index))  # Product impression custom dimension
            Tracker.alias(int, 'il{0}pi{1}cm{2}'.format(list_index, product_index, custom_index))  # Product impression custom metric

for list_index in range(1, MAX_EC_LISTS):
    Tracker.alias(unicode, 'il{0}nm'.format(list_index))  # Product impression list name

for promotion_index in range(1, MAX_EC_PROMOTIONS):
    Tracker.alias(str, 'promo{0}id'.format(promotion_index))  # Promotion ID
    Tracker.alias(unicode, 'promo{0}nm'.format(promotion_index))  # Promotion name
    Tracker.alias(str, 'promo{0}cr'.format(promotion_index))  # Promotion creative
    Tracker.alias(str, 'promo{0}ps'.format(promotion_index))  # Promotion position

# Shortcut for creating trackers
def create(account, *args, **kwargs):
    return Tracker(account, *args, **kwargs)

# vim: set nowrap tabstop=4 shiftwidth=4 softtabstop=0 expandtab textwidth=0 filetype=python foldmethod=indent foldcolumn=4


  if model_name.startswith('efficientnet-lite'):
    return efficientnet_lite_builder
  elif model_name.startswith('efficientnet-b'):
    return efficientnet_builder
  else:
    raise ValueError('Unknown model name {}'.format(model_name))

"""Backbone network factory."""

from . import efficientnet_builder
from . import efficientnet_lite_builder

def get_model_builder(model_name):
  """Get the model_builder module for a given model name."""


        for l in letters:
            if l > target:
                return l

        return letters[0]

# Given a list of sorted characters letters containing only lowercase letters, and given a target letter target,
# find the smallest element in the list that is larger than the given target.
# Letters also wrap around. For example, if the target is target = 'z' and letters = ['a', 'b'], the answer is 'a'.
# letters has a length in range [2, 10000].
# letters consists of lowercase letters, and contains at least 2 unique letters.
# target is a lowercase letter.

# Status: Accepted
class Solution(object):
    def nextGreatestLetter(self, letters, target):
        """
        :type letters: List[str]
        :type target: str
        :rtype: str
        """

# Status: Accepted
import bisect
class Solution(object):
    def nextGreatestLetter(self, letters, target):
        """
        :type letters: List[str]
        :type target: str
        :rtype: str
        """
        pos = bisect.bisect_right(letters, target)
        return letters[0] if pos == len(letters) else letters[pos]

if __name__ == '__main__':
    print(Solution().nextGreatestLetter(
        ["c", "f", "j"], 'k'
    ))


    if value is None:
      raise validation.MissingAttribute('schedule must be specified')
    if not isinstance(value, basestring):
      raise TypeError('schedule must be a string, not \'%r\''%type(value))
    try:
      groctimespecification.GrocTimeSpecification(value)
    except groc.GrocException, e:
      raise validation.ValidationError('schedule \'%s\' failed to parse: %s'%(
          value, e.args[0]))
    return value

"""CronInfo tools.

A library for working with CronInfo records, describing cron entries for an
application. Supports loading the records from yaml.
"""

import logging
import os
import sys
import traceback

try:
  import pytz
except ImportError:
  pytz = None

from google.appengine.cron import groc
from google.appengine.cron import groctimespecification

if os.environ.get('APPENGINE_RUNTIME') == 'python27':
  from google.appengine.api import appinfo
  from google.appengine.api import validation
  from google.appengine.api import yaml_builder
  from google.appengine.api import yaml_listener
  from google.appengine.api import yaml_object
else:
  from google.appengine.api import appinfo
  from google.appengine.api import validation
  from google.appengine.api import yaml_builder
  from google.appengine.api import yaml_listener
  from google.appengine.api import yaml_object

_URL_REGEX = r'^/.*$'
_TIMEZONE_REGEX = r'^.{0,100}$'
_DESCRIPTION_REGEX = ur'^.{0,499}$'

SERVER_ID_RE_STRING = r'(?!-)[a-z\d\-]{1,63}'

SERVER_VERSION_RE_STRING = r'(?!-)[a-z\d\-]{1,100}'

_VERSION_REGEX = r'^(?:(?:(%s):)?)(%s)$' % (SERVER_ID_RE_STRING,
                                            SERVER_VERSION_RE_STRING)

class GrocValidator(validation.Validator):
  """Checks that a schedule is in valid groc format."""

  def Validate(self, value, key=None):
    """Validates a schedule."""

class TimezoneValidator(validation.Validator):
  """Checks that a timezone can be correctly parsed and is known."""

  def Validate(self, value, key=None):
    """Validates a timezone."""
    if value is None:

      return
    if not isinstance(value, basestring):
      raise TypeError('timezone must be a string, not \'%r\'' % type(value))
    if pytz is None:

      return value
    try:
      pytz.timezone(value)
    except pytz.UnknownTimeZoneError:
      raise validation.ValidationError('timezone \'%s\' is unknown' % value)
    except IOError:

      return value
    except:

      unused_e, v, t = sys.exc_info()
      logging.warning('pytz raised an unexpected error: %s.\n' % (v) +
                      'Traceback:\n' + '\n'.join(traceback.format_tb(t)))
      raise
    return value

CRON = 'cron'

URL = 'url'
SCHEDULE = 'schedule'
TIMEZONE = 'timezone'
DESCRIPTION = 'description'
TARGET = 'target'

RETRY_PARAMETERS = 'retry_parameters'
JOB_RETRY_LIMIT = 'job_retry_limit'
JOB_AGE_LIMIT = 'job_age_limit'
MIN_BACKOFF_SECONDS = 'min_backoff_seconds'
MAX_BACKOFF_SECONDS = 'max_backoff_seconds'
MAX_DOUBLINGS = 'max_doublings'

class MalformedCronfigurationFile(Exception):
  """Configuration file for Cron is malformed."""
  pass

class RetryParameters(validation.Validated):
  """Retry parameters for a single cron job."""
  ATTRIBUTES = {
      JOB_RETRY_LIMIT: validation.Optional(
          validation.Range(0, None, range_type=int)),
      JOB_AGE_LIMIT: validation.Optional(validation.TimeValue()),
      MIN_BACKOFF_SECONDS: validation.Optional(
          validation.Range(0.0, None, range_type=float)),
      MAX_BACKOFF_SECONDS: validation.Optional(
          validation.Range(0.0, None, range_type=float)),
      MAX_DOUBLINGS: validation.Optional(
          validation.Range(0, None, range_type=int)),
  }

class CronEntry(validation.Validated):
  """A cron entry describes a single cron job."""
  ATTRIBUTES = {
      URL: _URL_REGEX,
      SCHEDULE: GrocValidator(),
      TIMEZONE: TimezoneValidator(),
      DESCRIPTION: validation.Optional(_DESCRIPTION_REGEX),
      RETRY_PARAMETERS: validation.Optional(RetryParameters),
      TARGET: validation.Optional(_VERSION_REGEX),
  }

class CronInfoExternal(validation.Validated):
  """CronInfoExternal describes all cron entries for an application."""
  ATTRIBUTES = {
      appinfo.APPLICATION: validation.Optional(appinfo.APPLICATION_RE_STRING),
      CRON: validation.Optional(validation.Repeated(CronEntry))
  }

def LoadSingleCron(cron_info, open_fn=None):
  """Load a cron.yaml file or string and return a CronInfoExternal object."""
  builder = yaml_object.ObjectBuilder(CronInfoExternal)
  handler = yaml_builder.BuilderHandler(builder)
  listener = yaml_listener.EventListener(handler)
  listener.Parse(cron_info)

  cron_info_result = handler.GetResults()
  if len(cron_info_result) < 1:
    raise MalformedCronfigurationFile('Empty cron configuration.')
  if len(cron_info_result) > 1:
    raise MalformedCronfigurationFile('Multiple cron sections '
                                      'in configuration.')
  return cron_info_result[0]


while True:
            match = re.search(HEX_ENCODED_CHAR_REGEX, retVal)
            if match:
                retVal = retVal.replace(match.group("result"), (unichr if isinstance(value, unicode) else chr)(ord(binascii.unhexlify(match.group("result").lstrip("\\x")))))
            else:
                break

"""
safe2bin.py - Simple safe(hex) to binary format converter

"""

import binascii
import re
import string
import os
import sys

from optparse import OptionError
from optparse import OptionParser

# Regex used for recognition of hex encoded characters
HEX_ENCODED_CHAR_REGEX = r"(?P<result>\\x[0-9A-Fa-f]{2})"

# Raw chars that will be safe encoded to their slash (\) representations (e.g. newline to \n)
SAFE_ENCODE_SLASH_REPLACEMENTS = "\t\n\r\x0b\x0c"

# Characters that don't need to be safe encoded
SAFE_CHARS = "".join(filter(lambda _: _ not in SAFE_ENCODE_SLASH_REPLACEMENTS, string.printable.replace('\\', '')))

# Prefix used for hex encoded values
HEX_ENCODED_PREFIX = r"\x"

# Strings used for temporary marking of hex encoded prefixes (to prevent double encoding)
HEX_ENCODED_PREFIX_MARKER = "__HEX_ENCODED_PREFIX__"

# String used for temporary marking of slash characters
SLASH_MARKER = "__SLASH__"

def safecharencode(value):
    """
    Returns safe representation of a given basestring value

    >>> safecharencode(u'test123')
    u'test123'
    >>> safecharencode(u'test\x01\x02\xff')
    u'test\\01\\02\\03\\ff'
    """

    retVal = value

    if isinstance(value, basestring):
        if any([_ not in SAFE_CHARS for _ in value]):
            retVal = retVal.replace(HEX_ENCODED_PREFIX, HEX_ENCODED_PREFIX_MARKER)
            retVal = retVal.replace('\\', SLASH_MARKER)

            for char in SAFE_ENCODE_SLASH_REPLACEMENTS:
                retVal = retVal.replace(char, repr(char).strip('\''))

            retVal = reduce(lambda x, y: x + (y if (y in string.printable or isinstance(value, unicode) and ord(y) >= 160) else '\\x%02x' % ord(y)), retVal, (unicode if isinstance(value, unicode) else str)())

            retVal = retVal.replace(SLASH_MARKER, "\\\\")
            retVal = retVal.replace(HEX_ENCODED_PREFIX_MARKER, HEX_ENCODED_PREFIX)
    elif isinstance(value, list):
        for i in xrange(len(value)):
            retVal[i] = safecharencode(value[i])

    return retVal

def safechardecode(value, binary=False):
    """
    Reverse function to safecharencode
    """

    retVal = value
    if isinstance(value, basestring):
        retVal = retVal.replace('\\\\', SLASH_MARKER)

        for char in SAFE_ENCODE_SLASH_REPLACEMENTS[::-1]:
            retVal = retVal.replace(repr(char).strip('\''), char)

        retVal = retVal.replace(SLASH_MARKER, '\\')

        if binary:
            if isinstance(retVal, unicode):
                retVal = retVal.encode("utf8")

    elif isinstance(value, (list, tuple)):
        for i in xrange(len(value)):
            retVal[i] = safechardecode(value[i])

    return retVal

def main():
    usage = '%s -i <input file> [-o <output file>]' % sys.argv[0]
    parser = OptionParser(usage=usage, version='0.1')

    try:
        parser.add_option('-i', dest='inputFile', help='Input file')
        parser.add_option('-o', dest='outputFile', help='Output file')

        (args, _) = parser.parse_args()

        if not args.inputFile:
            parser.error('Missing the input file, -h for help')

    except (OptionError, TypeError), e:
        parser.error(e)

    if not os.path.isfile(args.inputFile):
        print 'ERROR: the provided input file \'%s\' is not a regular file' % args.inputFile
        sys.exit(1)

    f = open(args.inputFile, 'r')
    data = f.read()
    f.close()

    if not args.outputFile:
        args.outputFile = args.inputFile + '.bin'

    f = open(args.outputFile, 'wb')
    f.write(safechardecode(data))
    f.close()

if __name__ == '__main__':
    main()


  hparams = registry.hparams(hparams_set)
  if hparams_path and tf.gfile.Exists(hparams_path):
    hparams = create_hparams_from_json(hparams_path, hparams)
  if data_dir:
    hparams.add_hparam("data_dir", data_dir)
  if hparams_overrides_str:
    tf.logging.info("Overriding hparams in %s with %s", hparams_set,
                    hparams_overrides_str)
    hparams = hparams.parse(hparams_overrides_str)
  if problem_name:
    add_problem_hparams(hparams, problem_name)
  return hparams

"""T2T HParams handling."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import json

from tensor2tensor.data_generators import problem as problem_lib
from tensor2tensor.utils import hparam
from tensor2tensor.utils import registry

import tensorflow.compat.v1 as tf

def copy_hparams(hparams):
  hp_vals = hparams.values()
  new_hparams = hparam.HParams(**hp_vals)
  other_attrs = ["problem", "problem_hparams"]
  for attr in other_attrs:
    attr_val = getattr(hparams, attr, None)
    if attr_val is not None:
      setattr(new_hparams, attr, attr_val)
  return new_hparams

def create_hparams(hparams_set,
                   hparams_overrides_str="",
                   data_dir=None,
                   problem_name=None,
                   hparams_path=None):
  """Create HParams with data_dir and problem hparams, if kwargs provided."""

def create_hparams_from_json(json_path, hparams=None):
  """Loading hparams from json; can also start from hparams if specified."""
  tf.logging.info("Loading hparams from existing json %s" % json_path)
  with tf.gfile.Open(json_path, "r") as f:
    hparams_values = json.load(f)
    # Prevent certain keys from overwriting the passed-in hparams.
    # TODO(trandustin): Remove this hack after registries are available to avoid
    # saving them as functions.
    if hparams:
      hparams_values.pop("bottom", None)
      hparams_values.pop("loss", None)
      hparams_values.pop("name", None)
      hparams_values.pop("top", None)
      hparams_values.pop("weights_fn", None)
    new_hparams = hparam.HParams(**hparams_values)
    # Some keys are in new_hparams but not hparams, so we need to be more
    #   careful than simply using parse_json() from HParams
    if hparams:  # hparams specified, so update values from json
      for key in sorted(new_hparams.values().keys()):
        if hasattr(hparams, key):  # Overlapped keys
          value = getattr(hparams, key)
          new_value = getattr(new_hparams, key)
          if value != new_value:  # Different values
            tf.logging.info("Overwrite key %s: %s -> %s" % (
                key, value, new_value))
            setattr(hparams, key, new_value)
    else:
      hparams = new_hparams

  return hparams

def add_problem_hparams(hparams, problem_name_or_instance):
  """Add problem hparams for the problems."""
  if isinstance(problem_name_or_instance, problem_lib.Problem):
    problem = problem_name_or_instance
  else:
    problem = registry.problem(problem_name_or_instance)
  p_hparams = problem.get_hparams(hparams)
  hparams.problem = problem
  hparams.problem_hparams = p_hparams


    if theta is None:
        theta = Parameter("ϴ")

    circ = QuantumCircuit(2)
    circ.cx(0, 1)
    circ.ry(theta, 1)
    circ.cx(0, 1)
    circ.ry(-1 * theta, 1)
    circ.rz(-np.pi / 2, 1)
    circ.rx(theta, 1)
    circ.rzx(-1 * theta, 0, 1)
    circ.rz(np.pi / 2, 1)

    return circ

# This code is part of Qiskit.
# Any modifications or derivative works of this code must retain this
# copyright notice, and modified files need to carry a notice indicating
# that they have been altered from the originals.

"""
RZX based template for CX - RYGate - CX
.. parsed-literal::
                                                       ┌──────────┐
q_0: ──■─────────────■─────────────────────────────────┤0         ├───────────
     ┌─┴─┐┌───────┐┌─┴─┐┌────────┐┌──────────┐┌───────┐│  RZX(-ϴ) │┌─────────┐
q_1: ┤ X ├┤ RY(ϴ) ├┤ X ├┤ RY(-ϴ) ├┤ RZ(-π/2) ├┤ RX(ϴ) ├┤1         ├┤ RZ(π/2) ├
     └───┘└───────┘└───┘└────────┘└──────────┘└───────┘└──────────┘└─────────┘
"""

import numpy as np
from qiskit.circuit import Parameter, QuantumCircuit

def rzx_cy(theta: float = None):
    """Template for CX - RYGate - CX."""


    outdir = os.path.join(cfg.OUTPUT_DIR, 'detection_output',
                          imdb.name)
    if weights_filename is None:
        weights_filename = 'default'
    outdir = os.path.join(outdir, weights_filename)
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    return outdir

"""Transform a roidb into a trainable roidb by adding a bunch of metadata."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import libs.datasets as datasets
import numpy as np
from libs.datasets.factory import get_imdb
from libs.utils.path_config import cfg
from PIL import Image
import cv2
import torch
import pdb

def prepare_roidb(imdb):
    """Enrich the imdb's roidb by adding some derived quantities that
    are useful for training. This function precomputes the maximum
    overlap, taken over ground-truth boxes, between each ROI and
    each ground-truth box. The class with maximum overlap is also
    recorded.
    """

    roidb = imdb.roidb
    if not (imdb.name.startswith('coco')):
        sizes = [Image.open(imdb.image_path_at(i)).size
                 for i in range(imdb.num_images)]

    for i in range(len(imdb.image_index)):
        roidb[i]['img_id'] = imdb.image_id_at(i)
        roidb[i]['image'] = imdb.image_path_at(i)
        if not (imdb.name.startswith('coco')):
            roidb[i]['width'] = sizes[i][0]
            roidb[i]['height'] = sizes[i][1]
        # need gt_overlaps as a dense array for argmax
        gt_overlaps = roidb[i]['gt_overlaps'].toarray()
        # max overlap with gt over classes (columns)
        max_overlaps = gt_overlaps.max(axis=1)
        # gt class that had the max overlap
        max_classes = gt_overlaps.argmax(axis=1)
        roidb[i]['max_classes'] = max_classes
        roidb[i]['max_overlaps'] = max_overlaps
        # sanity checks
        # max overlap of 0 => class should be zero (background)
        zero_inds = np.where(max_overlaps == 0)[0]
        assert all(max_classes[zero_inds] == 0)
        # max overlap > 0 => class should not be zero (must be a fg class)
        nonzero_inds = np.where(max_overlaps > 0)[0]
        assert all(max_classes[nonzero_inds] != 0)

def filter_roidb(roidb):
    # filter the image without bounding box.
    print('before filtering, there are %d images...' % (len(roidb)))
    i = 0
    while i < len(roidb):
        if len(roidb[i]['boxes']) == 0:
            del roidb[i]
            i -= 1
        i += 1

    print('after filtering, there are %d images...' % (len(roidb)))
    return roidb

def combined_roidb(imdb_names, training=True):
    """
    Combine multiple roidbs
    """

    def get_training_roidb(imdb):
        """Returns a roidb (Region of Interest database) for use in training."""
        print('Preparing training data...')

        prepare_roidb(imdb)
        print('done')

        return imdb.roidb

    def get_roidb(imdb_name):
        imdb = get_imdb(imdb_name)
        print('Loaded dataset `{:s}` for training'.format(imdb.name))
        roidb = get_training_roidb(imdb)
        return roidb

    roidbs = [get_roidb(s) for s in imdb_names.split('+')]
    roidb = roidbs[0]

    if len(roidbs) > 1:
        for r in roidbs[1:]:
            roidb.extend(r)
        tmp = get_imdb(imdb_names.split('+')[1])
        imdb = datasets.imdb.imdb(imdb_names, tmp.classes)
    else:
        imdb = get_imdb(imdb_names)

    if training:
        roidb = filter_roidb(roidb)

    return imdb, roidb

def get_output_dir(imdb, weights_filename):
    """Return the directory where experimental artifacts are placed.
    If the directory does not exist, it is created.

    A canonical path is built using the name from an imdb and a network
    (if not None).
    """


while i < len(roidb):
        if len(roidb[i]['boxes']) == 0:
            del roidb[i]
            i -= 1
        i += 1

"""Transform a roidb into a trainable roidb by adding a bunch of metadata."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import os
import libs.datasets as datasets
import numpy as np
from libs.datasets.factory import get_imdb
from libs.utils.path_config import cfg
from PIL import Image
import cv2
import torch
import pdb

def prepare_roidb(imdb):
    """Enrich the imdb's roidb by adding some derived quantities that
    are useful for training. This function precomputes the maximum
    overlap, taken over ground-truth boxes, between each ROI and
    each ground-truth box. The class with maximum overlap is also
    recorded.
    """

    roidb = imdb.roidb
    if not (imdb.name.startswith('coco')):
        sizes = [Image.open(imdb.image_path_at(i)).size
                 for i in range(imdb.num_images)]

    for i in range(len(imdb.image_index)):
        roidb[i]['img_id'] = imdb.image_id_at(i)
        roidb[i]['image'] = imdb.image_path_at(i)
        if not (imdb.name.startswith('coco')):
            roidb[i]['width'] = sizes[i][0]
            roidb[i]['height'] = sizes[i][1]
        # need gt_overlaps as a dense array for argmax
        gt_overlaps = roidb[i]['gt_overlaps'].toarray()
        # max overlap with gt over classes (columns)
        max_overlaps = gt_overlaps.max(axis=1)
        # gt class that had the max overlap
        max_classes = gt_overlaps.argmax(axis=1)
        roidb[i]['max_classes'] = max_classes
        roidb[i]['max_overlaps'] = max_overlaps
        # sanity checks
        # max overlap of 0 => class should be zero (background)
        zero_inds = np.where(max_overlaps == 0)[0]
        assert all(max_classes[zero_inds] == 0)
        # max overlap > 0 => class should not be zero (must be a fg class)
        nonzero_inds = np.where(max_overlaps > 0)[0]
        assert all(max_classes[nonzero_inds] != 0)

def filter_roidb(roidb):
    # filter the image without bounding box.
    print('before filtering, there are %d images...' % (len(roidb)))
    i = 0

    print('after filtering, there are %d images...' % (len(roidb)))
    return roidb

def combined_roidb(imdb_names, training=True):
    """
    Combine multiple roidbs
    """

    def get_training_roidb(imdb):
        """Returns a roidb (Region of Interest database) for use in training."""
        print('Preparing training data...')

        prepare_roidb(imdb)
        print('done')

        return imdb.roidb

    def get_roidb(imdb_name):
        imdb = get_imdb(imdb_name)
        print('Loaded dataset `{:s}` for training'.format(imdb.name))
        roidb = get_training_roidb(imdb)
        return roidb

    roidbs = [get_roidb(s) for s in imdb_names.split('+')]
    roidb = roidbs[0]

    if len(roidbs) > 1:
        for r in roidbs[1:]:
            roidb.extend(r)
        tmp = get_imdb(imdb_names.split('+')[1])
        imdb = datasets.imdb.imdb(imdb_names, tmp.classes)
    else:
        imdb = get_imdb(imdb_names)

    if training:
        roidb = filter_roidb(roidb)

    return imdb, roidb

def get_output_dir(imdb, weights_filename):
    """Return the directory where experimental artifacts are placed.
    If the directory does not exist, it is created.

    A canonical path is built using the name from an imdb and a network
    (if not None).
    """
    outdir = os.path.join(cfg.OUTPUT_DIR, 'detection_output',
                          imdb.name)
    if weights_filename is None:
        weights_filename = 'default'
    outdir = os.path.join(outdir, weights_filename)
    if not os.path.exists(outdir):
        os.makedirs(outdir)
    return outdir


    for value in values:
        try:
            connection.addValueToBundle(bundle, value)
        except YouTrackException, e:
            if e.response.status == 409:
                print "Value with name [ %s ] already exists in bundle [ %s ]" % \
                      (value.name.encode('utf-8'), bundle.name.encode('utf-8'))
            else:
                raise e

from youtrack import YouTrackException

def _create_custom_field_prototype(connection, cf_type, cf_name, auto_attached=False, additional_params=dict([])):
    field = _get_custom_field(connection, cf_name)
    if field is not None:
        if field.type != cf_type:
            msg = "Custom field with name [ %s ] already exists. It has type [ %s ] instead of [ %s ]" %\
                  (cf_name.encode('utf-8'), field.type, cf_type)
            raise LogicException(msg)
    else:
        connection.createCustomFieldDetailed(cf_name, cf_type, False, True, auto_attached, additional_params)

def _get_custom_field(connection, cf_name):
    existing_fields = [item for item in connection.getCustomFields() if item.name.encode('utf-8').lower() ==
                                                                        cf_name.encode('utf-8').lower()]
    if len(existing_fields):
        return existing_fields[0]
    return None

def create_custom_field(connection, cf_type, cf_name, auto_attached, value_names=None, bundle_policy="0"):
    """
    Creates custom field prototype(if not exist) and sets default values bundle if needed

     Args:
        connection: An opened Connection instance.
        cf_type: Type of custom field to be created
        cf_name: Name of custom field that should be created (if not exists)
        auto_attached: If this field should be auto attached or not.
        value_names: Values, that should be attached with this cf by default. If None, no bundle is created to this field
            if empty, empty bundle is created.
    Raises:
        LogicException: If custom field already exists, but has wrong type.
        YouTrackException: If something is wrong with queries.
    """
    if (value_names is None) and (not auto_attached or "[" not in cf_type):
        _create_custom_field_prototype(connection, cf_type, cf_name, auto_attached)
        return
    if value_names is None:
        value_names = set([])
    else:
        value_names = set(value_names)
    field = _get_custom_field(connection, cf_name)
    bundle = None
    if field is not None:
        if hasattr(field, "defaultBundle"):
            bundle = connection.getBundle(field.type, field.defaultBundle)
        elif field.autoAttached:
            return
        else:
            bundle = create_bundle_safe(connection, cf_name + "_bundle", cf_type)
    else:
        bundle = create_bundle_safe(connection, cf_name + "_bundle", cf_type)
        _create_custom_field_prototype(connection, cf_type, cf_name, auto_attached,
                {"defaultBundle": bundle.name,
                 "attachBundlePolicy": bundle_policy})
    for value_name in value_names:
        try:
            connection.addValueToBundle(bundle, value_name)
        except YouTrackException:
            pass

def process_custom_field(connection, project_id, cf_type, cf_name, value_names=None):
    """
    Creates custom field and attaches it to the project. If custom field already exists and has type
    cf_type it is attached to the project. If it has another type, LogicException is raised. If project field already
    exists, uses it and bundle from it. If not, creates project field and bundle with name <cf_name>_bundle_<project_id> for it.
    Adds value_names to bundle.
    Args:
        connection: An opened Connection instance.
        project_id: Id of the project to attach CF to.
        cf_type: Type of cf to be created.
        cf_name: Name of cf that should be created (if not exists) and attached to the project (if not yet attached)
        value_names: Values, that cf must have. If None, does not create any bundle for the field. If empty list, creates bundle, but
            does not create any value_names in it. If bundle already contains some value_names, only value_names that do not already exist are added.

    Raises:
        LogicException: If custom field already exists, but has wrong type.
        YouTrackException: If something is wrong with queries.

    """
    _create_custom_field_prototype(connection, cf_type, cf_name)
    if cf_type[0:-3] not in connection.bundle_types:
        value_names = None
    elif value_names is None:
        value_names = []

    existing_project_fields = [item for item in connection.getProjectCustomFields(project_id) if
                               item.name.encode('utf-8') == cf_name]
    values_to_add = []
    bundle = None
    if len(existing_project_fields):
        if value_names is None:
            return
        bundle = connection.getBundle(cf_type, existing_project_fields[0].bundle)
        values_to_add = calculate_missing_value_names(bundle, value_names)
    else:
        if value_names is None:
            connection.createProjectCustomFieldDetailed(project_id, cf_name, "No " + cf_name)
            return
        bundle = create_bundle_safe(connection, cf_name + "_bundle_" + project_id, cf_type)
        values_to_add = calculate_missing_value_names(bundle, value_names)
        connection.createProjectCustomFieldDetailed(project_id, cf_name, "No " + cf_name,
                                                    params={"bundle": bundle.name})
    [connection.addValueToBundle(bundle, bundle.createElement(name)) for name in values_to_add]

def add_values_to_bundle_safe(connection, bundle, values):
    """
    Adds values to specified bundle. Checks, whether each value already contains in bundle. If yes, it is not added.

    Args:
        connection: An opened Connection instance.
        bundle: Bundle instance to add values in.
        values: Values, that should be added in bundle.

    Raises:
        YouTrackException: if something is wrong with queries.
    """

def create_bundle_safe(connection, bundle_name, bundle_type):
    bundle = connection.bundle_types[bundle_type[0:-3]](None, None)
    bundle.name = bundle_name
    try:
        connection.createBundle(bundle)
    except YouTrackException, e:
        if e.response.status == 409:
            print "Bundle with name [ %s ] already exists" % bundle_name
        else:
            raise e
    return connection.getBundle(bundle_type, bundle_name)

def calculate_missing_value_names(bundle, value_names):
    bundle_elements_names = [elem.name.lower() for elem in bundle.values]
    return [value for value in value_names if value.lower() not in bundle_elements_names]

class LogicException(Exception):
    def __init__(self, msg):
        Exception.__init__(self, msg)


        updated_values = self.delete_values()
        self.update(updated_values)
        self.save(session=session)
        return updated_values

# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.

"""
SQLAlchemy models for cinder data.
"""

from oslo_config import cfg
from oslo_db.sqlalchemy import models
from oslo_utils import timeutils
import sqlalchemy as sa
# imports needed for cinderlib
from sqlalchemy import Column, String, Text  # noqa: F401
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import func
from sqlalchemy import schema
from sqlalchemy.orm import backref, column_property, relationship, validates

CONF = cfg.CONF
BASE = declarative_base()

class CinderBase(models.TimestampMixin,
                 models.ModelBase):
    """Base class for Cinder Models."""

    __table_args__ = {'mysql_engine': 'InnoDB'}

    # TODO(rpodolyaka): reuse models.SoftDeleteMixin in the next stage
    #                   of implementing of BP db-cleanup
    deleted_at = sa.Column(sa.DateTime)
    deleted = sa.Column(sa.Boolean, default=False)
    metadata = None

    @staticmethod
    def delete_values():
        return {'deleted': True,
                'deleted_at': timeutils.utcnow()}

    def delete(self, session):
        """Delete this object."""

class Service(BASE, CinderBase):
    """Represents a running service on a host."""

    __tablename__ = 'services'
    id = sa.Column(sa.Integer, primary_key=True)
    uuid = sa.Column(sa.String(36), nullable=True, index=True)
    cluster_name = sa.Column(sa.String(255), nullable=True)
    host = sa.Column(sa.String(255))  # , sa.ForeignKey('hosts.id'))
    binary = sa.Column(sa.String(255))
    # We want to overwrite default updated_at definition so we timestamp at
    # creation as well, so we only need to check updated_at for the heartbeat
    updated_at = sa.Column(
        sa.DateTime,
        default=timeutils.utcnow,
        onupdate=timeutils.utcnow)
    topic = sa.Column(sa.String(255))
    report_count = sa.Column(sa.Integer, nullable=False, default=0)
    disabled = sa.Column(sa.Boolean, default=False)
    availability_zone = sa.Column(sa.String(255), default='cinder')
    disabled_reason = sa.Column(sa.String(255))
    # adding column modified_at to contain timestamp
    # for manual enable/disable of cinder services
    # updated_at column will now contain timestamps for
    # periodic updates
    modified_at = sa.Column(sa.DateTime)

    # Version columns to support rolling upgrade. These report the max RPC API
    # and objects versions that the manager of the service is able to support.
    rpc_current_version = sa.Column(sa.String(36))
    object_current_version = sa.Column(sa.String(36))

    # replication_status can be: enabled, disabled, not-capable, error,
    replication_status = sa.Column(sa.String(36), default="not-capable")
    active_backend_id = sa.Column(sa.String(255))
    frozen = sa.Column(sa.Boolean, nullable=False, default=False)

    cluster = relationship('Cluster',
                           backref='services',
                           foreign_keys=cluster_name,
                           primaryjoin='and_('
                                       'Service.cluster_name == Cluster.name,'
                                       'Service.deleted == False)')

class Cluster(BASE, CinderBase):
    """Represents a cluster of hosts."""
    __tablename__ = 'clusters'
    # To remove potential races on creation we have a constraint set on name
    # and race_preventer fields, and we set value on creation to 0, so 2
    # clusters with the same name will fail this constraint.  On deletion we
    # change this field to the same value as the id which will be unique and
    # will not conflict with the creation of another cluster with the same
    __table_args__ = (sa.UniqueConstraint('name', 'binary', 'race_preventer'),
                      CinderBase.__table_args__)

    id = sa.Column(sa.Integer, primary_key=True)
    # NOTE(geguileo): Name is constructed in the same way that Server.host but
    # using cluster configuration option instead of host.
    name = sa.Column(sa.String(255), nullable=False)
    binary = sa.Column(sa.String(255), nullable=False)
    disabled = sa.Column(sa.Boolean, default=False)
    disabled_reason = sa.Column(sa.String(255))
    race_preventer = sa.Column(sa.Integer, nullable=False, default=0)

    replication_status = sa.Column(sa.String(36), default="not-capable")
    active_backend_id = sa.Column(sa.String(255))
    frozen = sa.Column(sa.Boolean, nullable=False, default=False)

    # Last heartbeat reported by any of the services of this cluster.  This is
    # not deferred since we always want to load this field.
    last_heartbeat = column_property(
        sa.select([func.max(Service.updated_at)]).
        where(sa.and_(Service.cluster_name == name, ~Service.deleted)).
        correlate_except(Service), deferred=False)

    # Number of existing services for this cluster
    num_hosts = column_property(
        sa.select([func.count(Service.id)]).
        where(sa.and_(Service.cluster_name == name, ~Service.deleted)).
        correlate_except(Service),
        group='services_summary', deferred=True)

    # Number of services that are down for this cluster
    num_down_hosts = column_property(
        sa.select([func.count(Service.id)]).
        where(sa.and_(Service.cluster_name == name,
                      ~Service.deleted,
                      Service.updated_at < sa.bindparam('expired'))).
        correlate_except(Service),
        group='services_summary', deferred=True)

    @staticmethod
    def delete_values():
        return {'race_preventer': Cluster.id,
                'deleted': True,
                'deleted_at': timeutils.utcnow()}

class ConsistencyGroup(BASE, CinderBase):
    """Represents a consistencygroup."""
    __tablename__ = 'consistencygroups'
    id = sa.Column(sa.String(36), primary_key=True)

    user_id = sa.Column(sa.String(255), nullable=False)
    project_id = sa.Column(sa.String(255), nullable=False)

    cluster_name = sa.Column(sa.String(255), nullable=True)
    host = sa.Column(sa.String(255))
    availability_zone = sa.Column(sa.String(255))
    name = sa.Column(sa.String(255))
    description = sa.Column(sa.String(255))
    volume_type_id = sa.Column(sa.String(255))
    status = sa.Column(sa.String(255))
    cgsnapshot_id = sa.Column(sa.String(36))
    source_cgid = sa.Column(sa.String(36))

class Group(BASE, CinderBase):
    """Represents a generic volume group."""
    __tablename__ = 'groups'
    id = sa.Column(sa.String(36), primary_key=True)

    user_id = sa.Column(sa.String(255), nullable=False)
    project_id = sa.Column(sa.String(255), nullable=False)

    cluster_name = sa.Column(sa.String(255))
    host = sa.Column(sa.String(255))
    availability_zone = sa.Column(sa.String(255))
    name = sa.Column(sa.String(255))
    description = sa.Column(sa.String(255))
    status = sa.Column(sa.String(255))
    group_type_id = sa.Column(sa.String(36))
    group_snapshot_id = sa.Column(sa.String(36))
    source_group_id = sa.Column(sa.String(36))

    replication_status = sa.Column(sa.String(255))

class CGSnapshot(BASE, CinderBase):
    """Represents a cgsnapshot."""
    __tablename__ = 'cgsnapshots'
    id = sa.Column(sa.String(36), primary_key=True)

    consistencygroup_id = sa.Column(sa.String(36), index=True)
    user_id = sa.Column(sa.String(255), nullable=False)
    project_id = sa.Column(sa.String(255), nullable=False)

    name = sa.Column(sa.String(255))
    description = sa.Column(sa.String(255))
    status = sa.Column(sa.String(255))

    consistencygroup = relationship(
        ConsistencyGroup,
        backref="cgsnapshots",
        foreign_keys=consistencygroup_id,
        primaryjoin='CGSnapshot.consistencygroup_id == ConsistencyGroup.id')

class GroupSnapshot(BASE, CinderBase):
    """Represents a group snapshot."""
    __tablename__ = 'group_snapshots'
    id = sa.Column(sa.String(36), primary_key=True)

    group_id = sa.Column(sa.String(36), nullable=False, index=True)
    user_id = sa.Column(sa.String(255))
    project_id = sa.Column(sa.String(255))

    name = sa.Column(sa.String(255))
    description = sa.Column(sa.String(255))
    status = sa.Column(sa.String(255))
    group_type_id = sa.Column(sa.String(36))

    group = relationship(
        Group,
        backref="group_snapshots",
        foreign_keys=group_id,
        primaryjoin='GroupSnapshot.group_id == Group.id')

class Volume(BASE, CinderBase):
    """Represents a block storage device that can be attached to a vm."""
    __tablename__ = 'volumes'
    __table_args__ = (
        sa.Index('volumes_service_uuid_idx', 'deleted', 'service_uuid'),
        CinderBase.__table_args__,
    )

    id = sa.Column(sa.String(36), primary_key=True)
    _name_id = sa.Column(sa.String(36))  # Don't access/modify this directly!
    # TODO: (Y release) Change nullable to False
    use_quota = Column(sa.Boolean, nullable=True, default=True,
                       doc='Ignore volume in quota usage')

    @property
    def name_id(self):
        return self.id if not self._name_id else self._name_id

    @name_id.setter
    def name_id(self, value):
        self._name_id = value

    @property
    def name(self):
        return CONF.volume_name_template % self.name_id

    ec2_id = sa.Column(sa.Integer)
    user_id = sa.Column(sa.String(255))
    project_id = sa.Column(sa.String(255))

    snapshot_id = sa.Column(sa.String(36))

    cluster_name = sa.Column(sa.String(255), nullable=True)
    host = sa.Column(sa.String(255))  # , sa.ForeignKey('hosts.id'))
    size = sa.Column(sa.Integer)
    availability_zone = sa.Column(sa.String(255))  # TODO(vish): foreign key?
    status = sa.Column(sa.String(255))  # TODO(vish): enum?
    attach_status = sa.Column(sa.String(255))  # TODO(vish): enum
    migration_status = sa.Column(sa.String(255))

    scheduled_at = sa.Column(sa.DateTime)
    launched_at = sa.Column(sa.DateTime)
    terminated_at = sa.Column(sa.DateTime)

    display_name = sa.Column(sa.String(255))
    display_description = sa.Column(sa.String(255))

    provider_location = sa.Column(sa.String(255))
    provider_auth = sa.Column(sa.String(255))
    provider_geometry = sa.Column(sa.String(255))
    provider_id = sa.Column(sa.String(255))

    volume_type_id = sa.Column(sa.String(36))
    source_volid = sa.Column(sa.String(36))
    encryption_key_id = sa.Column(sa.String(36))

    consistencygroup_id = sa.Column(sa.String(36), index=True)
    group_id = sa.Column(sa.String(36), index=True)

    bootable = sa.Column(sa.Boolean, default=False)
    multiattach = sa.Column(sa.Boolean, default=False)

    replication_status = sa.Column(sa.String(255))
    replication_extended_status = sa.Column(sa.String(255))
    replication_driver_data = sa.Column(sa.String(255))

    previous_status = sa.Column(sa.String(255))

    consistencygroup = relationship(
        ConsistencyGroup,
        backref="volumes",
        foreign_keys=consistencygroup_id,
        primaryjoin='Volume.consistencygroup_id == ConsistencyGroup.id')

    group = relationship(
        Group,
        backref="volumes",
        foreign_keys=group_id,
        primaryjoin='Volume.group_id == Group.id')

    service_uuid = sa.Column(sa.String(36), index=True)
    service = relationship(Service,
                           backref="volumes",
                           foreign_keys=service_uuid,
                           primaryjoin='Volume.service_uuid == Service.uuid')
    # make an FK of service?
    shared_targets = sa.Column(sa.Boolean, default=True)

class VolumeMetadata(BASE, CinderBase):
    """Represents a metadata key/value pair for a volume."""
    __tablename__ = 'volume_metadata'
    id = sa.Column(sa.Integer, primary_key=True)
    key = sa.Column(sa.String(255))
    value = sa.Column(sa.String(255))
    volume_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('volumes.id'),
        nullable=False,
        index=True)
    volume = relationship(Volume, backref="volume_metadata",
                          foreign_keys=volume_id,
                          primaryjoin='and_('
                          'VolumeMetadata.volume_id == Volume.id,'
                          'VolumeMetadata.deleted == False)')

class VolumeAdminMetadata(BASE, CinderBase):
    """Represents an administrator metadata key/value pair for a volume."""
    __tablename__ = 'volume_admin_metadata'
    id = sa.Column(sa.Integer, primary_key=True)
    key = sa.Column(sa.String(255))
    value = sa.Column(sa.String(255))
    volume_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('volumes.id'),
        nullable=False,
        index=True)
    volume = relationship(Volume, backref="volume_admin_metadata",
                          foreign_keys=volume_id,
                          primaryjoin='and_('
                          'VolumeAdminMetadata.volume_id == Volume.id,'
                          'VolumeAdminMetadata.deleted == False)')

class VolumeAttachment(BASE, CinderBase):
    """Represents a volume attachment for a vm."""
    __tablename__ = 'volume_attachment'
    id = sa.Column(sa.String(36), primary_key=True)

    volume_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('volumes.id'),
        nullable=False,
        index=True)
    volume = relationship(Volume, backref="volume_attachment",
                          foreign_keys=volume_id,
                          primaryjoin='and_('
                          'VolumeAttachment.volume_id == Volume.id,'
                          'VolumeAttachment.deleted == False)')
    instance_uuid = sa.Column(sa.String(36))
    attached_host = sa.Column(sa.String(255))
    mountpoint = sa.Column(sa.String(255))
    attach_time = sa.Column(sa.DateTime)
    detach_time = sa.Column(sa.DateTime)
    attach_status = sa.Column(sa.String(255))
    attach_mode = sa.Column(sa.String(255))
    connection_info = sa.Column(sa.Text)
    # Stores a serialized json dict of host connector information from brick.
    connector = sa.Column(sa.Text)

class VolumeType(BASE, CinderBase):
    """Represent possible volume_types of volumes offered."""
    __tablename__ = "volume_types"
    id = sa.Column(sa.String(36), primary_key=True)
    name = sa.Column(sa.String(255))
    description = sa.Column(sa.String(255))
    # A reference to qos_specs entity
    qos_specs_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('quality_of_service_specs.id'),
        index=True)
    is_public = sa.Column(sa.Boolean, default=True)
    volumes = relationship(Volume,
                           backref=backref('volume_type', uselist=False),
                           foreign_keys=id,
                           primaryjoin='and_('
                           'Volume.volume_type_id == VolumeType.id, '
                           'VolumeType.deleted == False)')

class GroupType(BASE, CinderBase):
    """Represent possible group_types of groups offered."""
    __tablename__ = "group_types"
    id = sa.Column(sa.String(36), primary_key=True)
    name = sa.Column(sa.String(255))
    description = sa.Column(sa.String(255))
    is_public = sa.Column(sa.Boolean, default=True)
    groups = relationship(Group,
                          backref=backref('group_type', uselist=False),
                          foreign_keys=id,
                          primaryjoin='and_('
                          'Group.group_type_id == GroupType.id, '
                          'GroupType.deleted == False)')

class GroupVolumeTypeMapping(BASE, CinderBase):
    """Represent mapping between groups and volume_types."""
    __tablename__ = "group_volume_type_mapping"
    id = sa.Column(sa.Integer, primary_key=True, nullable=False)
    volume_type_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('volume_types.id'),
        nullable=False,
        index=True)
    group_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('groups.id'),
        nullable=False,
        index=True)

    group = relationship(
        Group,
        backref="volume_types",
        foreign_keys=group_id,
        primaryjoin='and_('
        'GroupVolumeTypeMapping.group_id == Group.id,'
        'GroupVolumeTypeMapping.deleted == False)'
    )

class VolumeTypeProjects(BASE, CinderBase):
    """Represent projects associated volume_types."""
    __tablename__ = "volume_type_projects"
    __table_args__ = (schema.UniqueConstraint(
        "volume_type_id", "project_id", "deleted",
        name="uniq_volume_type_projects0volume_type_id0project_id0deleted"),
        CinderBase.__table_args__)
    id = sa.Column(sa.Integer, primary_key=True)
    volume_type_id = sa.Column(
        sa.String,
        sa.ForeignKey('volume_types.id'),
        nullable=False)
    project_id = sa.Column(sa.String(255))
    deleted = sa.Column(sa.Integer, default=0)

    volume_type = relationship(
        VolumeType,
        backref="projects",
        foreign_keys=volume_type_id,
        primaryjoin='and_('
        'VolumeTypeProjects.volume_type_id == VolumeType.id,'
        'VolumeTypeProjects.deleted == 0)')

class GroupTypeProjects(BASE, CinderBase):
    """Represent projects associated group_types."""
    __tablename__ = "group_type_projects"
    __table_args__ = (schema.UniqueConstraint(
        "group_type_id", "project_id", "deleted",
        name="uniq_group_type_projects0group_type_id0project_id0deleted"),
        CinderBase.__table_args__)
    id = sa.Column(sa.Integer, primary_key=True)
    group_type_id = sa.Column(
        sa.String,
        sa.ForeignKey('group_types.id'),
        nullable=False)
    project_id = sa.Column(sa.String(255))

    group_type = relationship(
        GroupType,
        backref="projects",
        foreign_keys=group_type_id,
        primaryjoin='and_('
        'GroupTypeProjects.group_type_id == GroupType.id,'
        'GroupTypeProjects.deleted == False)')

class VolumeTypeExtraSpecs(BASE, CinderBase):
    """Represents additional specs as key/value pairs for a volume_type."""
    __tablename__ = 'volume_type_extra_specs'
    id = sa.Column(sa.Integer, primary_key=True)
    key = sa.Column(sa.String(255))
    value = sa.Column(sa.String(255))
    volume_type_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('volume_types.id'),
        nullable=False,
        index=True)
    volume_type = relationship(
        VolumeType,
        backref="extra_specs",
        foreign_keys=volume_type_id,
        primaryjoin='and_('
        'VolumeTypeExtraSpecs.volume_type_id == VolumeType.id,'
        'VolumeTypeExtraSpecs.deleted == False)'
    )

class GroupTypeSpecs(BASE, CinderBase):
    """Represents additional specs as key/value pairs for a group_type."""
    __tablename__ = 'group_type_specs'
    id = sa.Column(sa.Integer, primary_key=True)
    key = sa.Column(sa.String(255))
    value = sa.Column(sa.String(255))
    group_type_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('group_types.id'),
        nullable=False,
        index=True)
    group_type = relationship(
        GroupType,
        backref="group_specs",
        foreign_keys=group_type_id,
        primaryjoin='and_('
        'GroupTypeSpecs.group_type_id == GroupType.id,'
        'GroupTypeSpecs.deleted == False)'
    )

class DefaultVolumeTypes(BASE, CinderBase):
    """Represent projects associated volume_types."""
    __tablename__ = "default_volume_types"
    volume_type_id = sa.Column(
        sa.String,
        sa.ForeignKey('volume_types.id'),
        nullable=False,
        index=True)
    project_id = sa.Column(sa.String(255), primary_key=True)
    volume_type = relationship(
        VolumeType,
        foreign_keys=volume_type_id,
        primaryjoin='DefaultVolumeTypes.volume_type_id == VolumeType.id')

class QualityOfServiceSpecs(BASE, CinderBase):
    """Represents QoS specs as key/value pairs.

    QoS specs is standalone entity that can be associated/disassociated
    with volume types (one to many relation).  Adjacency list relationship
    pattern is used in this model in order to represent following hierarchical
    data with in flat table, e.g, following structure:

    .. code-block:: none

      qos-specs-1  'Rate-Limit'
           |
           +------>  consumer = 'front-end'
           +------>  total_bytes_sec = 1048576
           +------>  total_iops_sec = 500

      qos-specs-2  'QoS_Level1'
           |
           +------>  consumer = 'back-end'
           +------>  max-iops =  1000
           +------>  min-iops = 200

      is represented by:

        id       specs_id       key                  value
      ------     --------   -------------            -----
      UUID-1     NULL       QoSSpec_Name           Rate-Limit
      UUID-2     UUID-1       consumer             front-end
      UUID-3     UUID-1     total_bytes_sec        1048576
      UUID-4     UUID-1     total_iops_sec           500
      UUID-5     NULL       QoSSpec_Name           QoS_Level1
      UUID-6     UUID-5       consumer             back-end
      UUID-7     UUID-5       max-iops               1000
      UUID-8     UUID-5       min-iops               200
    """
    __tablename__ = 'quality_of_service_specs'
    id = sa.Column(sa.String(36), primary_key=True)
    specs_id = sa.Column(sa.String(36), sa.ForeignKey(id), index=True)
    key = sa.Column(sa.String(255))
    value = sa.Column(sa.String(255))

    specs = relationship(
        "QualityOfServiceSpecs",
        cascade="all, delete-orphan",
        backref=backref("qos_spec", remote_side=id),
    )

    vol_types = relationship(
        VolumeType,
        backref=backref('qos_specs'),
        foreign_keys=id,
        primaryjoin='and_('
                    'or_(VolumeType.qos_specs_id == '
                    'QualityOfServiceSpecs.id,'
                    'VolumeType.qos_specs_id == '
                    'QualityOfServiceSpecs.specs_id),'
                    'QualityOfServiceSpecs.deleted == False)')

class VolumeGlanceMetadata(BASE, CinderBase):
    """Glance metadata for a bootable volume."""
    __tablename__ = 'volume_glance_metadata'
    id = sa.Column(sa.Integer, primary_key=True, nullable=False)
    volume_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('volumes.id'),
        index=True)
    snapshot_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('snapshots.id'),
        index=True)
    key = sa.Column(sa.String(255))
    value = sa.Column(sa.Text)
    volume = relationship(Volume, backref="volume_glance_metadata",
                          foreign_keys=volume_id,
                          primaryjoin='and_('
                          'VolumeGlanceMetadata.volume_id == Volume.id,'
                          'VolumeGlanceMetadata.deleted == False)')

class Quota(BASE, CinderBase):
    """Represents a single quota override for a project.

    If there is no row for a given project id and resource, then the
    default for the quota class is used.  If there is no row for a
    given quota class and resource, then the default for the
    deployment is used. If the row is present but the hard limit is
    Null, then the resource is unlimited.
    """

    __tablename__ = 'quotas'
    id = sa.Column(sa.Integer, primary_key=True)

    project_id = sa.Column(sa.String(255), index=True)

    resource = sa.Column(sa.String(255))
    hard_limit = sa.Column(sa.Integer, nullable=True)
    # TODO: (X release): Remove allocated, belonged to nested quotas
    allocated = sa.Column(sa.Integer, default=0)

class QuotaClass(BASE, CinderBase):
    """Represents a single quota override for a quota class.

    If there is no row for a given quota class and resource, then the
    default for the deployment is used.  If the row is present but the
    hard limit is Null, then the resource is unlimited.
    """

    __tablename__ = 'quota_classes'
    id = sa.Column(sa.Integer, primary_key=True)

    class_name = sa.Column(sa.String(255), index=True)

    resource = sa.Column(sa.String(255))
    hard_limit = sa.Column(sa.Integer, nullable=True)

class QuotaUsage(BASE, CinderBase):
    """Represents the current usage for a given resource."""

    __tablename__ = 'quota_usages'
    # NOTE: project_id and resource are not enough as unique constraint since
    # we do soft deletes and there could be duplicated entries, so we add the
    # race_preventer field.
    __table_args__ = (
        sa.UniqueConstraint('project_id', 'resource', 'race_preventer'),
        CinderBase.__table_args__)

    id = sa.Column(sa.Integer, primary_key=True)

    project_id = sa.Column(sa.String(255), index=True)
    resource = sa.Column(sa.String(300), index=True)

    in_use = sa.Column(sa.Integer)
    reserved = sa.Column(sa.Integer)

    @property
    def total(self):
        return self.in_use + self.reserved

    until_refresh = sa.Column(sa.Integer, nullable=True)

    # To prevent races during creation on quota_reserve method
    race_preventer = sa.Column(sa.Boolean, nullable=True, default=True)

    @staticmethod
    def delete_values():
        res = CinderBase.delete_values()
        res['race_preventer'] = None
        return res

class Reservation(BASE, CinderBase):
    """Represents a resource reservation for quotas."""

    __tablename__ = 'reservations'
    __table_args__ = (
        sa.Index('reservations_deleted_expire_idx', 'deleted', 'expire'),
        sa.Index('reservations_deleted_uuid_idx', 'deleted', 'uuid'),
        CinderBase.__table_args__,
    )

    id = sa.Column(sa.Integer, primary_key=True)
    uuid = sa.Column(sa.String(36), nullable=False)

    usage_id = sa.Column(
        sa.Integer,
        sa.ForeignKey('quota_usages.id'),
        nullable=True,
        index=True)
    # TODO: (X release): Remove allocated_id, belonged to nested quotas
    allocated_id = sa.Column(
        sa.Integer,
        sa.ForeignKey('quotas.id'),
        nullable=True,
        index=True)

    project_id = sa.Column(sa.String(255), index=True)
    resource = sa.Column(sa.String(255))

    delta = sa.Column(sa.Integer)
    expire = sa.Column(sa.DateTime, nullable=False)

    usage = relationship(
        "QuotaUsage",
        foreign_keys=usage_id,
        primaryjoin='and_(Reservation.usage_id == QuotaUsage.id,'
                    'QuotaUsage.deleted == False)')
    # TODO: (X release): Remove allocated_id, belonged to nested quotas
    quota = relationship(
        "Quota",
        foreign_keys=allocated_id,
        primaryjoin='and_(Reservation.allocated_id == Quota.id)')

class Snapshot(BASE, CinderBase):
    """Represents a snapshot of volume."""
    __tablename__ = 'snapshots'
    id = sa.Column(sa.String(36), primary_key=True)
    # TODO: (Y release) Change nullable to False
    use_quota = Column(sa.Boolean, nullable=True, default=True,
                       doc='Ignore volume in quota usage')

    @property
    def name(self):
        return CONF.snapshot_name_template % self.id

    @property
    def volume_name(self):
        return self.volume.name  # pylint: disable=E1101

    user_id = sa.Column(sa.String(255))
    project_id = sa.Column(sa.String(255))

    volume_id = sa.Column(sa.String(36), index=True)
    cgsnapshot_id = sa.Column(sa.String(36), index=True)
    group_snapshot_id = sa.Column(sa.String(36), index=True)
    status = sa.Column(sa.String(255))
    progress = sa.Column(sa.String(255))
    volume_size = sa.Column(sa.Integer)

    display_name = sa.Column(sa.String(255))
    display_description = sa.Column(sa.String(255))

    encryption_key_id = sa.Column(sa.String(36))
    volume_type_id = sa.Column(sa.String(36))

    provider_location = sa.Column(sa.String(255))
    provider_id = sa.Column(sa.String(255))
    provider_auth = sa.Column(sa.String(255))

    volume = relationship(Volume, backref="snapshots",
                          foreign_keys=volume_id,
                          primaryjoin='Snapshot.volume_id == Volume.id')

    cgsnapshot = relationship(
        CGSnapshot,
        backref="snapshots",
        foreign_keys=cgsnapshot_id,
        primaryjoin='Snapshot.cgsnapshot_id == CGSnapshot.id')

    group_snapshot = relationship(
        GroupSnapshot,
        backref="snapshots",
        foreign_keys=group_snapshot_id,
        primaryjoin='Snapshot.group_snapshot_id == GroupSnapshot.id')

class SnapshotMetadata(BASE, CinderBase):
    """Represents a metadata key/value pair for a snapshot."""
    __tablename__ = 'snapshot_metadata'
    id = sa.Column(sa.Integer, primary_key=True)
    key = sa.Column(sa.String(255))
    value = sa.Column(sa.String(255))
    snapshot_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('snapshots.id'),
        nullable=False,
        index=True)
    snapshot = relationship(Snapshot, backref="snapshot_metadata",
                            foreign_keys=snapshot_id,
                            primaryjoin='and_('
                            'SnapshotMetadata.snapshot_id == Snapshot.id,'
                            'SnapshotMetadata.deleted == False)')

class Backup(BASE, CinderBase):
    """Represents a backup of a volume to Swift."""
    __tablename__ = 'backups'
    id = sa.Column(sa.String(36), primary_key=True)
    # Backups don't have use_quota field since we don't have temporary backups

    @property
    def name(self):
        return CONF.backup_name_template % self.id

    user_id = sa.Column(sa.String(255), nullable=False)
    project_id = sa.Column(sa.String(255), nullable=False)

    volume_id = sa.Column(sa.String(36), nullable=False)
    host = sa.Column(sa.String(255))
    availability_zone = sa.Column(sa.String(255))
    display_name = sa.Column(sa.String(255))
    display_description = sa.Column(sa.String(255))
    container = sa.Column(sa.String(255))
    parent_id = sa.Column(sa.String(36))
    status = sa.Column(sa.String(255))
    fail_reason = sa.Column(sa.String(255))
    service_metadata = sa.Column(sa.String(255))
    service = sa.Column(sa.String(255))
    size = sa.Column(sa.Integer)
    object_count = sa.Column(sa.Integer)
    temp_volume_id = sa.Column(sa.String(36))
    temp_snapshot_id = sa.Column(sa.String(36))
    num_dependent_backups = sa.Column(sa.Integer)
    snapshot_id = sa.Column(sa.String(36))
    data_timestamp = sa.Column(sa.DateTime)
    restore_volume_id = sa.Column(sa.String(36))
    encryption_key_id = sa.Column(sa.String(36))

    @validates('fail_reason')
    def validate_fail_reason(self, key, fail_reason):
        return fail_reason and fail_reason[:255] or ''

class BackupMetadata(BASE, CinderBase):
    """Represents a metadata key/value pair for a backup."""
    __tablename__ = 'backup_metadata'
    id = sa.Column(sa.Integer, primary_key=True)
    key = sa.Column(sa.String(255))
    value = sa.Column(sa.String(255))
    backup_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('backups.id'),
        nullable=False,
        index=True)
    backup = relationship(Backup, backref="backup_metadata",
                          foreign_keys=backup_id,
                          primaryjoin='and_('
                          'BackupMetadata.backup_id == Backup.id,'
                          'BackupMetadata.deleted == False)')

class Encryption(BASE, CinderBase):
    """Represents encryption requirement for a volume type.

    Encryption here is a set of performance characteristics describing
    cipher, provider, and key_size for a certain volume type.
    """

    __tablename__ = 'encryption'
    encryption_id = sa.Column(sa.String(36), primary_key=True)
    cipher = sa.Column(sa.String(255))
    key_size = sa.Column(sa.Integer)
    provider = sa.Column(sa.String(255))
    control_location = sa.Column(sa.String(255))
    volume_type_id = sa.Column(sa.String(36), sa.ForeignKey('volume_types.id'))
    volume_type = relationship(
        VolumeType,
        backref="encryption",
        foreign_keys=volume_type_id,
        primaryjoin='and_('
        'Encryption.volume_type_id == VolumeType.id,'
        'Encryption.deleted == False)'
    )

class Transfer(BASE, CinderBase):
    """Represents a volume transfer request."""
    __tablename__ = 'transfers'
    id = sa.Column(sa.String(36), primary_key=True)
    volume_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('volumes.id'),
        index=True)
    display_name = sa.Column(sa.String(255))
    salt = sa.Column(sa.String(255))
    crypt_hash = sa.Column(sa.String(255))
    expires_at = sa.Column(sa.DateTime)
    no_snapshots = sa.Column(sa.Boolean, default=False)
    source_project_id = sa.Column(sa.String(255), nullable=True)
    destination_project_id = sa.Column(sa.String(255), nullable=True)
    accepted = sa.Column(sa.Boolean, default=False)
    volume = relationship(Volume, backref="transfer",
                          foreign_keys=volume_id,
                          primaryjoin='and_('
                          'Transfer.volume_id == Volume.id,'
                          'Transfer.deleted == False)')

class DriverInitiatorData(BASE, models.TimestampMixin, models.ModelBase):
    """Represents private key-value pair specific an initiator for drivers"""
    __tablename__ = 'driver_initiator_data'
    __table_args__ = (
        schema.UniqueConstraint("initiator", "namespace", "key"),
        CinderBase.__table_args__)

    id = sa.Column(sa.Integer, primary_key=True, nullable=False)
    initiator = sa.Column(sa.String(255), index=True, nullable=False)
    namespace = sa.Column(sa.String(255), nullable=False)
    key = sa.Column(sa.String(255), nullable=False)
    value = sa.Column(sa.String(255))

class Message(BASE, CinderBase):
    """Represents a message"""
    __tablename__ = 'messages'
    id = sa.Column(sa.String(36), primary_key=True, nullable=False)
    project_id = sa.Column(sa.String(255), nullable=False)
    message_level = sa.Column(sa.String(255), nullable=False)
    request_id = sa.Column(sa.String(255), nullable=True)
    resource_type = sa.Column(sa.String(255))
    # The UUID of the related resource.
    resource_uuid = sa.Column(sa.String(36), nullable=True)
    # Operation specific event ID.
    event_id = sa.Column(sa.String(255), nullable=False)
    # Message detail ID.
    detail_id = sa.Column(sa.String(10), nullable=True)
    # Operation specific action.
    action_id = sa.Column(sa.String(10), nullable=True)
    # After this time the message may no longer exist
    expires_at = sa.Column(sa.DateTime, nullable=True, index=True)

class ImageVolumeCacheEntry(BASE, models.ModelBase):
    """Represents an image volume cache entry"""
    __tablename__ = 'image_volume_cache_entries'

    id = sa.Column(sa.Integer, primary_key=True, nullable=False)
    host = sa.Column(sa.String(255), index=True, nullable=False)
    cluster_name = sa.Column(sa.String(255), nullable=True)
    image_id = sa.Column(sa.String(36), index=True, nullable=False)
    image_updated_at = sa.Column(sa.DateTime, nullable=False)
    volume_id = sa.Column(sa.String(36), nullable=False)
    size = sa.Column(sa.Integer, nullable=False)
    last_used = sa.Column(sa.DateTime, default=lambda: timeutils.utcnow())

class Worker(BASE, CinderBase):
    """Represents all resources that are being worked on by a node."""
    __tablename__ = 'workers'
    __table_args__ = (schema.UniqueConstraint('resource_type', 'resource_id'),
                      CinderBase.__table_args__)

    # We want to overwrite default updated_at definition so we timestamp at
    # creation as well
    updated_at = sa.Column(
        sa.DateTime,
        default=timeutils.utcnow,
        onupdate=timeutils.utcnow)

    # Id added for convenience and speed on some operations
    id = sa.Column(sa.Integer, primary_key=True, autoincrement=True)

    # Type of the resource we are working on (Volume, Snapshot, Backup) it must
    # match the Versioned Object class name.
    resource_type = sa.Column(sa.String(40), primary_key=True, nullable=False)
    # UUID of the resource we are working on
    resource_id = sa.Column(sa.String(36), primary_key=True, nullable=False)

    # Status that should be cleaned on service failure
    status = sa.Column(sa.String(255), nullable=False)

    # Service that is currently processing the operation
    service_id = sa.Column(sa.Integer, nullable=True, index=True)

    # To prevent claiming and updating races
    race_preventer = sa.Column(sa.Integer, nullable=False, default=0)

    # This is a flag we don't need to store in the DB as it is only used when
    # we are doing the cleanup to let decorators know
    cleaning = False

    service = relationship(
        'Service',
        backref="workers",
        foreign_keys=service_id,
        primaryjoin='Worker.service_id == Service.id')

class AttachmentSpecs(BASE, CinderBase):
    """Represents attachment specs as k/v pairs for a volume_attachment.

    DO NOT USE - NOTHING SHOULD WRITE NEW DATA TO THIS TABLE

    The volume_attachment.connector column should be used instead.
    """

    __tablename__ = 'attachment_specs'
    id = sa.Column(sa.Integer, primary_key=True)
    key = sa.Column(sa.String(255))
    value = sa.Column(sa.String(255))
    attachment_id = sa.Column(
        sa.String(36),
        sa.ForeignKey('volume_attachment.id'),
        nullable=False,
        index=True)
    volume_attachment = relationship(
        VolumeAttachment,
        backref="attachment_specs",
        foreign_keys=attachment_id,
        primaryjoin='and_('
        'AttachmentSpecs.attachment_id == VolumeAttachment.id,'
        'AttachmentSpecs.deleted == False)'
    )