|
1 # |
|
2 # Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 # |
|
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may |
|
5 # not use this file except in compliance with the License. You may obtain |
|
6 # a copy of the License at |
|
7 # |
|
8 # http://www.apache.org/licenses/LICENSE-2.0 |
|
9 # |
|
10 # Unless required by applicable law or agreed to in writing, software |
|
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
|
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
|
13 # License for the specific language governing permissions and limitations |
|
14 # under the License. |
|
15 # |
|
16 |
|
17 import ConfigParser |
|
18 |
|
19 from sqlalchemy.ext import declarative |
|
20 from sqlalchemy import orm |
|
21 import sqlalchemy as sa |
|
22 |
|
23 from neutron.context import ContextBase |
|
24 from neutron.db import model_base |
|
25 from neutron.openstack.common import uuidutils |
|
26 |
|
27 EVS_DB_BASE = declarative.declarative_base(cls=model_base.NeutronBaseV2) |
|
28 EVS_DB_ENGINE = None |
|
29 EVS_DB_MAKER = None |
|
30 |
|
31 |
|
32 class EVSContext(ContextBase): |
|
33 @property |
|
34 def session(self): |
|
35 return self._session |
|
36 |
|
37 @session.setter |
|
38 def session(self, session): |
|
39 self._session = session |
|
40 |
|
41 |
|
42 def configure_db(): |
|
43 global EVS_DB_ENGINE |
|
44 if not EVS_DB_ENGINE: |
|
45 config = ConfigParser.RawConfigParser() |
|
46 config.readfp(open("/etc/neutron/neutron.conf")) |
|
47 if config.has_option("database", 'connection'): |
|
48 sql_connection = config.get("database", 'connection') |
|
49 else: |
|
50 sql_connection = 'sqlite:////var/lib/neutron/neutron.sqlite' |
|
51 EVS_DB_ENGINE = sa.create_engine(sql_connection, echo=False) |
|
52 EVS_DB_BASE.metadata.create_all(EVS_DB_ENGINE) |
|
53 |
|
54 |
|
55 def get_session(autocommit=True, expire_on_commit=False): |
|
56 global EVS_DB_ENGINE, EVS_DB_MAKER |
|
57 assert EVS_DB_ENGINE |
|
58 if not EVS_DB_MAKER: |
|
59 EVS_DB_MAKER = orm.sessionmaker(bind=EVS_DB_ENGINE, |
|
60 autocommit=autocommit, |
|
61 expire_on_commit=expire_on_commit) |
|
62 return EVS_DB_MAKER() |
|
63 |
|
64 |
|
65 def get_evs_context(context): |
|
66 """Overrides the Neutron DB session with EVS DB session""" |
|
67 |
|
68 evs_context = EVSContext.from_dict(context.to_dict()) |
|
69 evs_context.session = get_session() |
|
70 |
|
71 return evs_context |
|
72 |
|
73 |
|
74 class Router(EVS_DB_BASE): |
|
75 """Represents a v2 neutron router.""" |
|
76 |
|
77 id = sa.Column(sa.String(36), primary_key=True, |
|
78 default=uuidutils.generate_uuid) |
|
79 name = sa.Column(sa.String(255)) |
|
80 status = sa.Column(sa.String(16)) |
|
81 admin_state_up = sa.Column(sa.Boolean) |
|
82 tenant_id = sa.Column(sa.String(255)) |
|
83 gw_port_id = sa.Column(sa.String(36)) |
|
84 gw_port_network_id = sa.Column(sa.String(36)) |
|
85 |
|
86 |
|
87 class FloatingIP(EVS_DB_BASE): |
|
88 """Represents a floating IP address. |
|
89 |
|
90 This IP address may or may not be allocated to a tenant, and may or |
|
91 may not be associated with an internal port/ip address/router. |
|
92 """ |
|
93 |
|
94 id = sa.Column(sa.String(36), primary_key=True, |
|
95 default=uuidutils.generate_uuid) |
|
96 floating_ip_address = sa.Column(sa.String(64), nullable=False) |
|
97 floating_network_id = sa.Column(sa.String(36), nullable=False) |
|
98 floating_port_id = sa.Column(sa.String(36), nullable=False) |
|
99 fixed_port_id = sa.Column(sa.String(36)) |
|
100 fixed_ip_address = sa.Column(sa.String(64)) |
|
101 router_id = sa.Column(sa.String(36), sa.ForeignKey('routers.id')) |
|
102 tenant_id = sa.Column(sa.String(255)) |