components/openstack/neutron/files/evs/db/api.py
branchs11-update
changeset 4072 db0cec748ec0
parent 4067 4be1f488dda8
child 4094 78203277f011
equal deleted inserted replaced
4067:4be1f488dda8 4072:db0cec748ec0
     1 # vim: tabstop=4 shiftwidth=4 softtabstop=4
       
     2 
       
     3 # Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
       
     4 #
       
     5 #    Licensed under the Apache License, Version 2.0 (the "License"); you may
       
     6 #    not use this file except in compliance with the License. You may obtain
       
     7 #    a copy of the License at
       
     8 #
       
     9 #         http://www.apache.org/licenses/LICENSE-2.0
       
    10 #
       
    11 #    Unless required by applicable law or agreed to in writing, software
       
    12 #    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
       
    13 #    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
       
    14 #    License for the specific language governing permissions and limitations
       
    15 #    under the License.
       
    16 #
       
    17 # @author: Girish Moodalbail, Oracle, Inc.
       
    18 #
       
    19 
       
    20 import sqlalchemy as sa
       
    21 from sqlalchemy.ext import declarative
       
    22 from sqlalchemy import orm
       
    23 
       
    24 from oslo.config import cfg
       
    25 
       
    26 from neutron.context import ContextBase
       
    27 from neutron.db import model_base
       
    28 
       
    29 
       
    30 EVS_DB_BASE = declarative.declarative_base(cls=model_base.NeutronBaseV2)
       
    31 EVS_DB_ENGINE = None
       
    32 EVS_DB_MAKER = None
       
    33 
       
    34 
       
    35 class EVSContext(ContextBase):
       
    36     @property
       
    37     def session(self):
       
    38         return self._session
       
    39 
       
    40     @session.setter
       
    41     def session(self, session):
       
    42         self._session = session
       
    43 
       
    44 
       
    45 def configure_db():
       
    46     global EVS_DB_ENGINE
       
    47     if not EVS_DB_ENGINE:
       
    48         sql_connection = cfg.CONF.DATABASE.sql_connection
       
    49         EVS_DB_ENGINE = sa.create_engine(sql_connection, echo=False)
       
    50         EVS_DB_BASE.metadata.create_all(EVS_DB_ENGINE)
       
    51 
       
    52 
       
    53 def get_session(autocommit=True, expire_on_commit=False):
       
    54     global EVS_DB_ENGINE, EVS_DB_MAKER
       
    55     assert EVS_DB_ENGINE
       
    56     if not EVS_DB_MAKER:
       
    57         EVS_DB_MAKER = orm.sessionmaker(bind=EVS_DB_ENGINE,
       
    58                                         autocommit=autocommit,
       
    59                                         expire_on_commit=expire_on_commit)
       
    60     return EVS_DB_MAKER()
       
    61 
       
    62 
       
    63 def get_evs_context(context):
       
    64     """Overrides the Neutron DB session with EVS DB session"""
       
    65 
       
    66     evs_context = EVSContext.from_dict(context.to_dict())
       
    67     evs_context.session = get_session()
       
    68 
       
    69     return evs_context