| 1 |
# (c) Copyright 2013 Hewlett-Packard Development Company, L.P. |
| 2 |
# (c) Copyright 2015 Cisco Systems Inc. |
| 3 |
# 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 |
from neutron.db import models_v2 |
| 18 |
from neutron_lib.callbacks import events |
| 19 |
from neutron_lib.callbacks import registry |
| 20 |
from neutron_lib.callbacks import resources |
| 21 |
from neutron_lib import constants as lib_constants |
| 22 |
from neutron_lib.db import api as db_api |
| 23 |
from neutron_lib.db import model_query |
| 24 |
from neutron_lib.db import utils as db_utils |
| 25 |
from neutron_lib.exceptions import l3 as l3_exception |
| 26 |
from neutron_lib.exceptions import vpn as vpn_exception |
| 27 |
from neutron_lib.plugins import constants as p_constants |
| 28 |
from neutron_lib.plugins import directory |
| 29 |
from neutron_lib.plugins import utils |
| 30 |
from oslo_log import log as logging |
| 31 |
from oslo_utils import excutils |
| 32 |
from oslo_utils import uuidutils |
| 33 |
import sqlalchemy as sa |
| 34 |
from sqlalchemy.orm import exc |
| 35 |
|
| 36 |
|
| 37 |
from neutron_vpnaas.db.vpn import vpn_models |
| 38 |
from neutron_vpnaas.db.vpn import vpn_validator |
| 39 |
from neutron_vpnaas.extensions import vpn_endpoint_groups |
| 40 |
from neutron_vpnaas.extensions import vpnaas |
| 41 |
from neutron_vpnaas.services.vpn.common import constants as v_constants |
| 42 |
|
| 43 |
LOG = logging.getLogger(__name__) |
| 44 |
|
| 45 |
|
| 46 |
class VPNPluginDb(vpnaas.VPNPluginBase, |
| 47 |
vpn_endpoint_groups.VPNEndpointGroupsPluginBase): |
| 48 |
"""VPN plugin database class using SQLAlchemy models.""" |
| 49 |
|
| 50 |
def _get_validator(self): |
| 51 |
"""Obtain validator to use for attribute validation. |
| 52 |
|
| 53 |
Subclasses may override this with a different validator, as needed. |
| 54 |
Note: some UTs will directly create a VPNPluginDb object and then |
| 55 |
call its methods, instead of creating a VPNDriverPlugin, which |
| 56 |
will have a service driver associated that will provide a |
| 57 |
validator object. As a result, we use the reference validator here. |
| 58 |
""" |
| 59 |
return vpn_validator.VpnReferenceValidator() |
| 60 |
|
| 61 |
def update_status(self, context, model, v_id, status): |
| 62 |
with db_api.CONTEXT_WRITER.using(context): |
| 63 |
v_db = self._get_resource(context, model, v_id) |
| 64 |
v_db.update({'status': status}) |
| 65 |
|
| 66 |
def _get_resource(self, context, model, v_id): |
| 67 |
try: |
| 68 |
r = model_query.get_by_id(context, model, v_id) |
| 69 |
except exc.NoResultFound: |
| 70 |
with excutils.save_and_reraise_exception(reraise=False) as ctx: |
| 71 |
if issubclass(model, vpn_models.IPsecSiteConnection): |
| 72 |
raise vpn_exception.IPsecSiteConnectionNotFound( |
| 73 |
ipsec_site_conn_id=v_id |
| 74 |
) |
| 75 |
if issubclass(model, vpn_models.IKEPolicy): |
| 76 |
raise vpn_exception.IKEPolicyNotFound(ikepolicy_id=v_id) |
| 77 |
if issubclass(model, vpn_models.IPsecPolicy): |
| 78 |
raise vpn_exception.IPsecPolicyNotFound( |
| 79 |
ipsecpolicy_id=v_id) |
| 80 |
if issubclass(model, vpn_models.VPNService): |
| 81 |
raise vpn_exception.VPNServiceNotFound(vpnservice_id=v_id) |
| 82 |
if issubclass(model, vpn_models.VPNEndpointGroup): |
| 83 |
raise vpn_exception.VPNEndpointGroupNotFound( |
| 84 |
endpoint_group_id=v_id) |
| 85 |
ctx.reraise = True |
| 86 |
return r |
| 87 |
|
| 88 |
def assert_update_allowed(self, obj): |
| 89 |
status = getattr(obj, 'status', None) |
| 90 |
_id = getattr(obj, 'id', None) |
| 91 |
if utils.in_pending_status(status): |
| 92 |
raise vpn_exception.VPNStateInvalidToUpdate(id=_id, state=status) |
| 93 |
|
| 94 |
def _make_ipsec_site_connection_dict(self, ipsec_site_conn, fields=None): |
| 95 |
|
| 96 |
res = {'id': ipsec_site_conn['id'], |
| 97 |
'tenant_id': ipsec_site_conn['tenant_id'], |
| 98 |
'name': ipsec_site_conn['name'], |
| 99 |
'description': ipsec_site_conn['description'], |
| 100 |
'peer_address': ipsec_site_conn['peer_address'], |
| 101 |
'peer_id': ipsec_site_conn['peer_id'], |
| 102 |
'local_id': ipsec_site_conn['local_id'], |
| 103 |
'route_mode': ipsec_site_conn['route_mode'], |
| 104 |
'mtu': ipsec_site_conn['mtu'], |
| 105 |
'auth_mode': ipsec_site_conn['auth_mode'], |
| 106 |
'psk': ipsec_site_conn['psk'], |
| 107 |
'initiator': ipsec_site_conn['initiator'], |
| 108 |
'dpd': { |
| 109 |
'action': ipsec_site_conn['dpd_action'], |
| 110 |
'interval': ipsec_site_conn['dpd_interval'], |
| 111 |
'timeout': ipsec_site_conn['dpd_timeout'] |
| 112 |
}, |
| 113 |
'admin_state_up': ipsec_site_conn['admin_state_up'], |
| 114 |
'status': ipsec_site_conn['status'], |
| 115 |
'vpnservice_id': ipsec_site_conn['vpnservice_id'], |
| 116 |
'ikepolicy_id': ipsec_site_conn['ikepolicy_id'], |
| 117 |
'ipsecpolicy_id': ipsec_site_conn['ipsecpolicy_id'], |
| 118 |
'peer_cidrs': [pcidr['cidr'] |
| 119 |
for pcidr in ipsec_site_conn['peer_cidrs']], |
| 120 |
'local_ep_group_id': ipsec_site_conn['local_ep_group_id'], |
| 121 |
'peer_ep_group_id': ipsec_site_conn['peer_ep_group_id'], |
| 122 |
} |
| 123 |
|
| 124 |
return db_utils.resource_fields(res, fields) |
| 125 |
|
| 126 |
def get_endpoint_info(self, context, ipsec_sitecon): |
| 127 |
"""Obtain all endpoint info, and store in connection for validation.""" |
| 128 |
ipsec_sitecon['local_epg_subnets'] = self.get_endpoint_group( |
| 129 |
context, ipsec_sitecon['local_ep_group_id']) |
| 130 |
ipsec_sitecon['peer_epg_cidrs'] = self.get_endpoint_group( |
| 131 |
context, ipsec_sitecon['peer_ep_group_id']) |
| 132 |
|
| 133 |
def validate_connection_info(self, context, validator, ipsec_sitecon, |
| 134 |
vpnservice): |
| 135 |
"""Collect info and validate connection. |
| 136 |
|
| 137 |
If endpoint groups used (default), collect the group info and |
| 138 |
do not specify the IP version (as it will come from endpoints). |
| 139 |
Otherwise, get the IP version from the (legacy) subnet for |
| 140 |
validation purposes. |
| 141 |
|
| 142 |
NOTE: Once the deprecated subnet is removed, the caller can just |
| 143 |
call get_endpoint_info() and validate_ipsec_site_connection(). |
| 144 |
""" |
| 145 |
if ipsec_sitecon['local_ep_group_id']: |
| 146 |
self.get_endpoint_info(context, ipsec_sitecon) |
| 147 |
ip_version = None |
| 148 |
else: |
| 149 |
ip_version = vpnservice.subnet.ip_version |
| 150 |
validator.validate_ipsec_site_connection(context, ipsec_sitecon, |
| 151 |
ip_version, vpnservice) |
| 152 |
|
| 153 |
def create_ipsec_site_connection(self, context, ipsec_site_connection): |
| 154 |
ipsec_sitecon = ipsec_site_connection['ipsec_site_connection'] |
| 155 |
validator = self._get_validator() |
| 156 |
validator.assign_sensible_ipsec_sitecon_defaults(ipsec_sitecon) |
| 157 |
with db_api.CONTEXT_WRITER.using(context): |
| 158 |
#Check permissions |
| 159 |
vpnservice_id = ipsec_sitecon['vpnservice_id'] |
| 160 |
self._get_resource(context, vpn_models.VPNService, vpnservice_id) |
| 161 |
self._get_resource(context, vpn_models.IKEPolicy, |
| 162 |
ipsec_sitecon['ikepolicy_id']) |
| 163 |
self._get_resource(context, vpn_models.IPsecPolicy, |
| 164 |
ipsec_sitecon['ipsecpolicy_id']) |
| 165 |
vpnservice = self._get_vpnservice(context, vpnservice_id) |
| 166 |
validator.validate_ipsec_conn_optional_args(ipsec_sitecon, |
| 167 |
vpnservice.subnet) |
| 168 |
self.validate_connection_info(context, validator, ipsec_sitecon, |
| 169 |
vpnservice) |
| 170 |
validator.resolve_peer_address(ipsec_sitecon, vpnservice.router) |
| 171 |
|
| 172 |
ipsec_site_conn_db = vpn_models.IPsecSiteConnection( |
| 173 |
id=uuidutils.generate_uuid(), |
| 174 |
tenant_id=ipsec_sitecon['tenant_id'], |
| 175 |
name=ipsec_sitecon['name'], |
| 176 |
description=ipsec_sitecon['description'], |
| 177 |
peer_address=ipsec_sitecon['peer_address'], |
| 178 |
peer_id=ipsec_sitecon['peer_id'], |
| 179 |
local_id=ipsec_sitecon['local_id'], |
| 180 |
route_mode='static', |
| 181 |
mtu=ipsec_sitecon['mtu'], |
| 182 |
auth_mode='psk', |
| 183 |
psk=ipsec_sitecon['psk'], |
| 184 |
initiator=ipsec_sitecon['initiator'], |
| 185 |
dpd_action=ipsec_sitecon['dpd_action'], |
| 186 |
dpd_interval=ipsec_sitecon['dpd_interval'], |
| 187 |
dpd_timeout=ipsec_sitecon['dpd_timeout'], |
| 188 |
admin_state_up=ipsec_sitecon['admin_state_up'], |
| 189 |
status=lib_constants.PENDING_CREATE, |
| 190 |
vpnservice_id=vpnservice_id, |
| 191 |
ikepolicy_id=ipsec_sitecon['ikepolicy_id'], |
| 192 |
ipsecpolicy_id=ipsec_sitecon['ipsecpolicy_id'], |
| 193 |
local_ep_group_id=ipsec_sitecon['local_ep_group_id'], |
| 194 |
peer_ep_group_id=ipsec_sitecon['peer_ep_group_id'] |
| 195 |
) |
| 196 |
context.session.add(ipsec_site_conn_db) |
| 197 |
for cidr in ipsec_sitecon['peer_cidrs']: |
| 198 |
peer_cidr_db = vpn_models.IPsecPeerCidr( |
| 199 |
cidr=cidr, |
| 200 |
ipsec_site_connection_id=ipsec_site_conn_db.id |
| 201 |
) |
| 202 |
context.session.add(peer_cidr_db) |
| 203 |
return self._make_ipsec_site_connection_dict(ipsec_site_conn_db) |
| 204 |
|
| 205 |
def update_ipsec_site_connection( |
| 206 |
self, context, |
| 207 |
ipsec_site_conn_id, ipsec_site_connection): |
| 208 |
ipsec_sitecon = ipsec_site_connection['ipsec_site_connection'] |
| 209 |
changed_peer_cidrs = False |
| 210 |
validator = self._get_validator() |
| 211 |
with db_api.CONTEXT_WRITER.using(context): |
| 212 |
ipsec_site_conn_db = self._get_resource( |
| 213 |
context, vpn_models.IPsecSiteConnection, ipsec_site_conn_id) |
| 214 |
vpnservice_id = ipsec_site_conn_db['vpnservice_id'] |
| 215 |
vpnservice = self._get_vpnservice(context, vpnservice_id) |
| 216 |
|
| 217 |
validator.assign_sensible_ipsec_sitecon_defaults( |
| 218 |
ipsec_sitecon, ipsec_site_conn_db) |
| 219 |
validator.validate_ipsec_conn_optional_args(ipsec_sitecon, |
| 220 |
vpnservice.subnet) |
| 221 |
self.validate_connection_info(context, validator, ipsec_sitecon, |
| 222 |
vpnservice) |
| 223 |
if 'peer_address' in ipsec_sitecon: |
| 224 |
validator.resolve_peer_address(ipsec_sitecon, |
| 225 |
vpnservice.router) |
| 226 |
self.assert_update_allowed(ipsec_site_conn_db) |
| 227 |
|
| 228 |
if "peer_cidrs" in ipsec_sitecon: |
| 229 |
changed_peer_cidrs = True |
| 230 |
old_peer_cidr_list = ipsec_site_conn_db['peer_cidrs'] |
| 231 |
old_peer_cidr_dict = { |
| 232 |
peer_cidr['cidr']: peer_cidr |
| 233 |
for peer_cidr in old_peer_cidr_list} |
| 234 |
new_peer_cidr_set = set(ipsec_sitecon["peer_cidrs"]) |
| 235 |
old_peer_cidr_set = set(old_peer_cidr_dict) |
| 236 |
|
| 237 |
new_peer_cidrs = list(new_peer_cidr_set) |
| 238 |
for peer_cidr in old_peer_cidr_set - new_peer_cidr_set: |
| 239 |
context.session.delete(old_peer_cidr_dict[peer_cidr]) |
| 240 |
for peer_cidr in new_peer_cidr_set - old_peer_cidr_set: |
| 241 |
pcidr = vpn_models.IPsecPeerCidr( |
| 242 |
cidr=peer_cidr, |
| 243 |
ipsec_site_connection_id=ipsec_site_conn_id) |
| 244 |
context.session.add(pcidr) |
| 245 |
# Note: Unconditionally remove peer_cidrs, as they will be set to |
| 246 |
# previous, if unchanged (to be able to validate above). |
| 247 |
del ipsec_sitecon["peer_cidrs"] |
| 248 |
if ipsec_sitecon: |
| 249 |
ipsec_site_conn_db.update(ipsec_sitecon) |
| 250 |
result = self._make_ipsec_site_connection_dict(ipsec_site_conn_db) |
| 251 |
if changed_peer_cidrs: |
| 252 |
result['peer_cidrs'] = new_peer_cidrs |
| 253 |
return result |
| 254 |
|
| 255 |
def delete_ipsec_site_connection(self, context, ipsec_site_conn_id): |
| 256 |
with db_api.CONTEXT_WRITER.using(context): |
| 257 |
ipsec_site_conn_db = self._get_resource( |
| 258 |
context, vpn_models.IPsecSiteConnection, ipsec_site_conn_id) |
| 259 |
context.session.delete(ipsec_site_conn_db) |
| 260 |
|
| 261 |
def _get_ipsec_site_connection( |
| 262 |
self, context, ipsec_site_conn_id): |
| 263 |
return self._get_resource( |
| 264 |
context, vpn_models.IPsecSiteConnection, ipsec_site_conn_id) |
| 265 |
|
| 266 |
@db_api.CONTEXT_READER |
| 267 |
def get_ipsec_site_connection(self, context, |
| 268 |
ipsec_site_conn_id, fields=None): |
| 269 |
ipsec_site_conn_db = self._get_ipsec_site_connection( |
| 270 |
context, ipsec_site_conn_id) |
| 271 |
return self._make_ipsec_site_connection_dict( |
| 272 |
ipsec_site_conn_db, fields) |
| 273 |
|
| 274 |
@db_api.CONTEXT_READER |
| 275 |
def get_ipsec_site_connections(self, context, filters=None, fields=None): |
| 276 |
return model_query.get_collection( |
| 277 |
context, vpn_models.IPsecSiteConnection, |
| 278 |
self._make_ipsec_site_connection_dict, |
| 279 |
filters=filters, fields=fields) |
| 280 |
|
| 281 |
def update_ipsec_site_conn_status(self, context, conn_id, new_status): |
| 282 |
with db_api.CONTEXT_WRITER.using(context): |
| 283 |
self._update_connection_status(context, conn_id, new_status, True) |
| 284 |
|
| 285 |
def _update_connection_status(self, context, conn_id, new_status, |
| 286 |
updated_pending): |
| 287 |
"""Update the connection status, if changed. |
| 288 |
|
| 289 |
If the connection is not in a pending state, unconditionally update |
| 290 |
the status. Likewise, if in a pending state, and have an indication |
| 291 |
that the status has changed, then update the database. |
| 292 |
""" |
| 293 |
try: |
| 294 |
conn_db = self._get_ipsec_site_connection(context, conn_id) |
| 295 |
except vpn_exception.IPsecSiteConnectionNotFound: |
| 296 |
return |
| 297 |
if not utils.in_pending_status(conn_db.status) or updated_pending: |
| 298 |
conn_db.status = new_status |
| 299 |
|
| 300 |
def _make_ikepolicy_dict(self, ikepolicy, fields=None): |
| 301 |
res = {'id': ikepolicy['id'], |
| 302 |
'tenant_id': ikepolicy['tenant_id'], |
| 303 |
'name': ikepolicy['name'], |
| 304 |
'description': ikepolicy['description'], |
| 305 |
'auth_algorithm': ikepolicy['auth_algorithm'], |
| 306 |
'encryption_algorithm': ikepolicy['encryption_algorithm'], |
| 307 |
'phase1_negotiation_mode': ikepolicy['phase1_negotiation_mode'], |
| 308 |
'lifetime': { |
| 309 |
'units': ikepolicy['lifetime_units'], |
| 310 |
'value': ikepolicy['lifetime_value'], |
| 311 |
}, |
| 312 |
'ike_version': ikepolicy['ike_version'], |
| 313 |
'pfs': ikepolicy['pfs'] |
| 314 |
} |
| 315 |
|
| 316 |
return db_utils.resource_fields(res, fields) |
| 317 |
|
| 318 |
def create_ikepolicy(self, context, ikepolicy): |
| 319 |
ike = ikepolicy['ikepolicy'] |
| 320 |
validator = self._get_validator() |
| 321 |
lifetime_info = ike['lifetime'] |
| 322 |
lifetime_units = lifetime_info.get('units', 'seconds') |
| 323 |
lifetime_value = lifetime_info.get('value', 3600) |
| 324 |
|
| 325 |
with db_api.CONTEXT_WRITER.using(context): |
| 326 |
validator.validate_ike_policy(context, ike) |
| 327 |
ike_db = vpn_models.IKEPolicy( |
| 328 |
id=uuidutils.generate_uuid(), |
| 329 |
tenant_id=ike['tenant_id'], |
| 330 |
name=ike['name'], |
| 331 |
description=ike['description'], |
| 332 |
auth_algorithm=ike['auth_algorithm'], |
| 333 |
encryption_algorithm=ike['encryption_algorithm'], |
| 334 |
phase1_negotiation_mode=ike['phase1_negotiation_mode'], |
| 335 |
lifetime_units=lifetime_units, |
| 336 |
lifetime_value=lifetime_value, |
| 337 |
ike_version=ike['ike_version'], |
| 338 |
pfs=ike['pfs'] |
| 339 |
) |
| 340 |
|
| 341 |
context.session.add(ike_db) |
| 342 |
return self._make_ikepolicy_dict(ike_db) |
| 343 |
|
| 344 |
def update_ikepolicy(self, context, ikepolicy_id, ikepolicy): |
| 345 |
ike = ikepolicy['ikepolicy'] |
| 346 |
validator = self._get_validator() |
| 347 |
with db_api.CONTEXT_WRITER.using(context): |
| 348 |
validator.validate_ike_policy(context, ike) |
| 349 |
if context.session.query(vpn_models.IPsecSiteConnection).filter_by( |
| 350 |
ikepolicy_id=ikepolicy_id).first(): |
| 351 |
raise vpn_exception.IKEPolicyInUse(ikepolicy_id=ikepolicy_id) |
| 352 |
ike_db = self._get_resource( |
| 353 |
context, vpn_models.IKEPolicy, ikepolicy_id) |
| 354 |
if ike: |
| 355 |
lifetime_info = ike.get('lifetime') |
| 356 |
if lifetime_info: |
| 357 |
if lifetime_info.get('units'): |
| 358 |
ike['lifetime_units'] = lifetime_info['units'] |
| 359 |
if lifetime_info.get('value'): |
| 360 |
ike['lifetime_value'] = lifetime_info['value'] |
| 361 |
ike_db.update(ike) |
| 362 |
return self._make_ikepolicy_dict(ike_db) |
| 363 |
|
| 364 |
def delete_ikepolicy(self, context, ikepolicy_id): |
| 365 |
with db_api.CONTEXT_WRITER.using(context): |
| 366 |
if context.session.query(vpn_models.IPsecSiteConnection).filter_by( |
| 367 |
ikepolicy_id=ikepolicy_id).first(): |
| 368 |
raise vpn_exception.IKEPolicyInUse(ikepolicy_id=ikepolicy_id) |
| 369 |
ike_db = self._get_resource( |
| 370 |
context, vpn_models.IKEPolicy, ikepolicy_id) |
| 371 |
context.session.delete(ike_db) |
| 372 |
|
| 373 |
@db_api.CONTEXT_READER |
| 374 |
def get_ikepolicy(self, context, ikepolicy_id, fields=None): |
| 375 |
ike_db = self._get_resource( |
| 376 |
context, vpn_models.IKEPolicy, ikepolicy_id) |
| 377 |
return self._make_ikepolicy_dict(ike_db, fields) |
| 378 |
|
| 379 |
@db_api.CONTEXT_READER |
| 380 |
def get_ikepolicies(self, context, filters=None, fields=None): |
| 381 |
return model_query.get_collection(context, vpn_models.IKEPolicy, |
| 382 |
self._make_ikepolicy_dict, |
| 383 |
filters=filters, fields=fields) |
| 384 |
|
| 385 |
def _make_ipsecpolicy_dict(self, ipsecpolicy, fields=None): |
| 386 |
|
| 387 |
res = {'id': ipsecpolicy['id'], |
| 388 |
'tenant_id': ipsecpolicy['tenant_id'], |
| 389 |
'name': ipsecpolicy['name'], |
| 390 |
'description': ipsecpolicy['description'], |
| 391 |
'transform_protocol': ipsecpolicy['transform_protocol'], |
| 392 |
'auth_algorithm': ipsecpolicy['auth_algorithm'], |
| 393 |
'encryption_algorithm': ipsecpolicy['encryption_algorithm'], |
| 394 |
'encapsulation_mode': ipsecpolicy['encapsulation_mode'], |
| 395 |
'lifetime': { |
| 396 |
'units': ipsecpolicy['lifetime_units'], |
| 397 |
'value': ipsecpolicy['lifetime_value'], |
| 398 |
}, |
| 399 |
'pfs': ipsecpolicy['pfs'] |
| 400 |
} |
| 401 |
|
| 402 |
return db_utils.resource_fields(res, fields) |
| 403 |
|
| 404 |
def create_ipsecpolicy(self, context, ipsecpolicy): |
| 405 |
ipsecp = ipsecpolicy['ipsecpolicy'] |
| 406 |
validator = self._get_validator() |
| 407 |
lifetime_info = ipsecp['lifetime'] |
| 408 |
lifetime_units = lifetime_info.get('units', 'seconds') |
| 409 |
lifetime_value = lifetime_info.get('value', 3600) |
| 410 |
|
| 411 |
with db_api.CONTEXT_WRITER.using(context): |
| 412 |
validator.validate_ipsec_policy(context, ipsecp) |
| 413 |
ipsecp_db = vpn_models.IPsecPolicy( |
| 414 |
id=uuidutils.generate_uuid(), |
| 415 |
tenant_id=ipsecp['tenant_id'], |
| 416 |
name=ipsecp['name'], |
| 417 |
description=ipsecp['description'], |
| 418 |
transform_protocol=ipsecp['transform_protocol'], |
| 419 |
auth_algorithm=ipsecp['auth_algorithm'], |
| 420 |
encryption_algorithm=ipsecp['encryption_algorithm'], |
| 421 |
encapsulation_mode=ipsecp['encapsulation_mode'], |
| 422 |
lifetime_units=lifetime_units, |
| 423 |
lifetime_value=lifetime_value, |
| 424 |
pfs=ipsecp['pfs']) |
| 425 |
context.session.add(ipsecp_db) |
| 426 |
return self._make_ipsecpolicy_dict(ipsecp_db) |
| 427 |
|
| 428 |
def update_ipsecpolicy(self, context, ipsecpolicy_id, ipsecpolicy): |
| 429 |
ipsecp = ipsecpolicy['ipsecpolicy'] |
| 430 |
validator = self._get_validator() |
| 431 |
with db_api.CONTEXT_WRITER.using(context): |
| 432 |
validator.validate_ipsec_policy(context, ipsecp) |
| 433 |
if context.session.query(vpn_models.IPsecSiteConnection).filter_by( |
| 434 |
ipsecpolicy_id=ipsecpolicy_id).first(): |
| 435 |
raise vpn_exception.IPsecPolicyInUse( |
| 436 |
ipsecpolicy_id=ipsecpolicy_id) |
| 437 |
ipsecp_db = self._get_resource( |
| 438 |
context, vpn_models.IPsecPolicy, ipsecpolicy_id) |
| 439 |
if ipsecp: |
| 440 |
lifetime_info = ipsecp.get('lifetime') |
| 441 |
if lifetime_info: |
| 442 |
if lifetime_info.get('units'): |
| 443 |
ipsecp['lifetime_units'] = lifetime_info['units'] |
| 444 |
if lifetime_info.get('value'): |
| 445 |
ipsecp['lifetime_value'] = lifetime_info['value'] |
| 446 |
ipsecp_db.update(ipsecp) |
| 447 |
return self._make_ipsecpolicy_dict(ipsecp_db) |
| 448 |
|
| 449 |
def delete_ipsecpolicy(self, context, ipsecpolicy_id): |
| 450 |
with db_api.CONTEXT_WRITER.using(context): |
| 451 |
if context.session.query(vpn_models.IPsecSiteConnection).filter_by( |
| 452 |
ipsecpolicy_id=ipsecpolicy_id).first(): |
| 453 |
raise vpn_exception.IPsecPolicyInUse( |
| 454 |
ipsecpolicy_id=ipsecpolicy_id) |
| 455 |
ipsec_db = self._get_resource( |
| 456 |
context, vpn_models.IPsecPolicy, ipsecpolicy_id) |
| 457 |
context.session.delete(ipsec_db) |
| 458 |
|
| 459 |
@db_api.CONTEXT_READER |
| 460 |
def get_ipsecpolicy(self, context, ipsecpolicy_id, fields=None): |
| 461 |
ipsec_db = self._get_resource( |
| 462 |
context, vpn_models.IPsecPolicy, ipsecpolicy_id) |
| 463 |
return self._make_ipsecpolicy_dict(ipsec_db, fields) |
| 464 |
|
| 465 |
@db_api.CONTEXT_READER |
| 466 |
def get_ipsecpolicies(self, context, filters=None, fields=None): |
| 467 |
return model_query.get_collection(context, vpn_models.IPsecPolicy, |
| 468 |
self._make_ipsecpolicy_dict, |
| 469 |
filters=filters, fields=fields) |
| 470 |
|
| 471 |
def _make_vpnservice_dict(self, vpnservice, fields=None): |
| 472 |
res = {'id': vpnservice['id'], |
| 473 |
'name': vpnservice['name'], |
| 474 |
'description': vpnservice['description'], |
| 475 |
'tenant_id': vpnservice['tenant_id'], |
| 476 |
'subnet_id': vpnservice['subnet_id'], |
| 477 |
'router_id': vpnservice['router_id'], |
| 478 |
'flavor_id': vpnservice['flavor_id'], |
| 479 |
'admin_state_up': vpnservice['admin_state_up'], |
| 480 |
'external_v4_ip': vpnservice['external_v4_ip'], |
| 481 |
'external_v6_ip': vpnservice['external_v6_ip'], |
| 482 |
'status': vpnservice['status']} |
| 483 |
return db_utils.resource_fields(res, fields) |
| 484 |
|
| 485 |
def create_vpnservice(self, context, vpnservice): |
| 486 |
vpns = vpnservice['vpnservice'] |
| 487 |
flavor_id = vpns.get('flavor_id', None) |
| 488 |
validator = self._get_validator() |
| 489 |
with db_api.CONTEXT_WRITER.using(context): |
| 490 |
validator.validate_vpnservice(context, vpns) |
| 491 |
vpnservice_db = vpn_models.VPNService( |
| 492 |
id=uuidutils.generate_uuid(), |
| 493 |
tenant_id=vpns['tenant_id'], |
| 494 |
name=vpns['name'], |
| 495 |
description=vpns['description'], |
| 496 |
subnet_id=vpns['subnet_id'], |
| 497 |
router_id=vpns['router_id'], |
| 498 |
flavor_id=flavor_id, |
| 499 |
admin_state_up=vpns['admin_state_up'], |
| 500 |
status=lib_constants.PENDING_CREATE) |
| 501 |
context.session.add(vpnservice_db) |
| 502 |
return self._make_vpnservice_dict(vpnservice_db) |
| 503 |
|
| 504 |
def set_external_tunnel_ips(self, context, vpnservice_id, v4_ip=None, |
| 505 |
v6_ip=None): |
| 506 |
"""Update the external tunnel IP(s) for service.""" |
| 507 |
vpns = {'external_v4_ip': v4_ip, 'external_v6_ip': v6_ip} |
| 508 |
with db_api.CONTEXT_WRITER.using(context): |
| 509 |
vpns_db = self._get_resource(context, vpn_models.VPNService, |
| 510 |
vpnservice_id) |
| 511 |
vpns_db.update(vpns) |
| 512 |
return self._make_vpnservice_dict(vpns_db) |
| 513 |
|
| 514 |
def set_vpnservice_status(self, context, vpnservice_id, status, |
| 515 |
updated_pending_status=False): |
| 516 |
vpns = {'status': status} |
| 517 |
with db_api.CONTEXT_WRITER.using(context): |
| 518 |
vpns_db = self._get_resource(context, vpn_models.VPNService, |
| 519 |
vpnservice_id) |
| 520 |
if (utils.in_pending_status(vpns_db.status) and |
| 521 |
not updated_pending_status): |
| 522 |
raise vpnaas.VPNStateInvalidToUpdate( |
| 523 |
id=vpnservice_id, state=vpns_db.status) |
| 524 |
vpns_db.update(vpns) |
| 525 |
return self._make_vpnservice_dict(vpns_db) |
| 526 |
|
| 527 |
def update_vpnservice(self, context, vpnservice_id, vpnservice): |
| 528 |
vpns = vpnservice['vpnservice'] |
| 529 |
with db_api.CONTEXT_WRITER.using(context): |
| 530 |
vpns_db = self._get_resource(context, vpn_models.VPNService, |
| 531 |
vpnservice_id) |
| 532 |
self.assert_update_allowed(vpns_db) |
| 533 |
if vpns: |
| 534 |
vpns_db.update(vpns) |
| 535 |
return self._make_vpnservice_dict(vpns_db) |
| 536 |
|
| 537 |
def delete_vpnservice(self, context, vpnservice_id): |
| 538 |
with db_api.CONTEXT_WRITER.using(context): |
| 539 |
if context.session.query(vpn_models.IPsecSiteConnection).filter_by( |
| 540 |
vpnservice_id=vpnservice_id |
| 541 |
).first(): |
| 542 |
raise vpn_exception.VPNServiceInUse( |
| 543 |
vpnservice_id=vpnservice_id) |
| 544 |
vpns_db = self._get_resource(context, vpn_models.VPNService, |
| 545 |
vpnservice_id) |
| 546 |
context.session.delete(vpns_db) |
| 547 |
|
| 548 |
@db_api.CONTEXT_READER |
| 549 |
def _get_vpnservice(self, context, vpnservice_id): |
| 550 |
return self._get_resource(context, vpn_models.VPNService, |
| 551 |
vpnservice_id) |
| 552 |
|
| 553 |
@db_api.CONTEXT_READER |
| 554 |
def get_vpnservice(self, context, vpnservice_id, fields=None): |
| 555 |
vpns_db = self._get_resource(context, vpn_models.VPNService, |
| 556 |
vpnservice_id) |
| 557 |
return self._make_vpnservice_dict(vpns_db, fields) |
| 558 |
|
| 559 |
@db_api.CONTEXT_READER |
| 560 |
def get_vpnservices(self, context, filters=None, fields=None): |
| 561 |
return model_query.get_collection(context, vpn_models.VPNService, |
| 562 |
self._make_vpnservice_dict, |
| 563 |
filters=filters, fields=fields) |
| 564 |
|
| 565 |
def check_router_in_use(self, context, router_id): |
| 566 |
vpnservices = self.get_vpnservices( |
| 567 |
context, filters={'router_id': [router_id]}) |
| 568 |
if vpnservices: |
| 569 |
plural = "s" if len(vpnservices) > 1 else "" |
| 570 |
services = ",".join([v['id'] for v in vpnservices]) |
| 571 |
raise l3_exception.RouterInUse( |
| 572 |
router_id=router_id, |
| 573 |
reason="is currently used by VPN service%(plural)s " |
| 574 |
"(%(services)s)" % {'plural': plural, |
| 575 |
'services': services}) |
| 576 |
|
| 577 |
def check_subnet_in_use(self, context, subnet_id, router_id): |
| 578 |
with db_api.CONTEXT_READER.using(context): |
| 579 |
vpnservices = context.session.query( |
| 580 |
vpn_models.VPNService).filter_by( |
| 581 |
subnet_id=subnet_id, router_id=router_id).first() |
| 582 |
if vpnservices: |
| 583 |
raise vpn_exception.SubnetInUseByVPNService( |
| 584 |
subnet_id=subnet_id, |
| 585 |
vpnservice_id=vpnservices['id']) |
| 586 |
|
| 587 |
query = context.session.query(vpn_models.IPsecSiteConnection) |
| 588 |
query = query.join( |
| 589 |
vpn_models.VPNEndpointGroup, |
| 590 |
vpn_models.VPNEndpointGroup.id == |
| 591 |
vpn_models.IPsecSiteConnection.local_ep_group_id).filter( |
| 592 |
vpn_models.VPNEndpointGroup.endpoint_type == |
| 593 |
v_constants.SUBNET_ENDPOINT) |
| 594 |
query = query.join( |
| 595 |
vpn_models.VPNEndpoint, |
| 596 |
vpn_models.VPNEndpoint.endpoint_group_id == |
| 597 |
vpn_models.IPsecSiteConnection.local_ep_group_id).filter( |
| 598 |
vpn_models.VPNEndpoint.endpoint == subnet_id) |
| 599 |
query = query.join( |
| 600 |
vpn_models.VPNService, |
| 601 |
vpn_models.VPNService.id == |
| 602 |
vpn_models.IPsecSiteConnection.vpnservice_id).filter( |
| 603 |
vpn_models.VPNService.router_id == router_id) |
| 604 |
connection = query.first() |
| 605 |
if connection: |
| 606 |
raise vpn_exception.SubnetInUseByIPsecSiteConnection( |
| 607 |
subnet_id=subnet_id, |
| 608 |
ipsec_site_connection_id=connection['id']) |
| 609 |
|
| 610 |
def check_subnet_in_use_by_endpoint_group(self, context, subnet_id): |
| 611 |
with db_api.CONTEXT_READER.using(context): |
| 612 |
query = context.session.query(vpn_models.VPNEndpointGroup) |
| 613 |
query = query.filter(vpn_models.VPNEndpointGroup.endpoint_type == |
| 614 |
v_constants.SUBNET_ENDPOINT) |
| 615 |
query = query.join( |
| 616 |
vpn_models.VPNEndpoint, |
| 617 |
sa.and_(vpn_models.VPNEndpoint.endpoint_group_id == |
| 618 |
vpn_models.VPNEndpointGroup.id, |
| 619 |
vpn_models.VPNEndpoint.endpoint == subnet_id)) |
| 620 |
group = query.first() |
| 621 |
if group: |
| 622 |
raise vpn_exception.SubnetInUseByEndpointGroup( |
| 623 |
subnet_id=subnet_id, group_id=group['id']) |
| 624 |
|
| 625 |
def _make_endpoint_group_dict(self, endpoint_group, fields=None): |
| 626 |
res = {'id': endpoint_group['id'], |
| 627 |
'tenant_id': endpoint_group['tenant_id'], |
| 628 |
'name': endpoint_group['name'], |
| 629 |
'description': endpoint_group['description'], |
| 630 |
'type': endpoint_group['endpoint_type'], |
| 631 |
'endpoints': [ep['endpoint'] |
| 632 |
for ep in endpoint_group['endpoints']]} |
| 633 |
return db_utils.resource_fields(res, fields) |
| 634 |
|
| 635 |
def create_endpoint_group(self, context, endpoint_group): |
| 636 |
group = endpoint_group['endpoint_group'] |
| 637 |
validator = self._get_validator() |
| 638 |
with db_api.CONTEXT_WRITER.using(context): |
| 639 |
validator.validate_endpoint_group(context, group) |
| 640 |
endpoint_group_db = vpn_models.VPNEndpointGroup( |
| 641 |
id=uuidutils.generate_uuid(), |
| 642 |
tenant_id=group['tenant_id'], |
| 643 |
name=group['name'], |
| 644 |
description=group['description'], |
| 645 |
endpoint_type=group['type']) |
| 646 |
context.session.add(endpoint_group_db) |
| 647 |
for endpoint in group['endpoints']: |
| 648 |
endpoint_db = vpn_models.VPNEndpoint( |
| 649 |
endpoint=endpoint, |
| 650 |
endpoint_group_id=endpoint_group_db.id |
| 651 |
) |
| 652 |
context.session.add(endpoint_db) |
| 653 |
return self._make_endpoint_group_dict(endpoint_group_db) |
| 654 |
|
| 655 |
def update_endpoint_group(self, context, endpoint_group_id, |
| 656 |
endpoint_group): |
| 657 |
group_changes = endpoint_group['endpoint_group'] |
| 658 |
# Note: Endpoints cannot be changed, so will not do validation |
| 659 |
with db_api.CONTEXT_WRITER.using(context): |
| 660 |
endpoint_group_db = self._get_resource(context, |
| 661 |
vpn_models.VPNEndpointGroup, |
| 662 |
endpoint_group_id) |
| 663 |
endpoint_group_db.update(group_changes) |
| 664 |
return self._make_endpoint_group_dict(endpoint_group_db) |
| 665 |
|
| 666 |
def delete_endpoint_group(self, context, endpoint_group_id): |
| 667 |
with db_api.CONTEXT_WRITER.using(context): |
| 668 |
self.check_endpoint_group_not_in_use(context, endpoint_group_id) |
| 669 |
endpoint_group_db = self._get_resource( |
| 670 |
context, vpn_models.VPNEndpointGroup, endpoint_group_id) |
| 671 |
context.session.delete(endpoint_group_db) |
| 672 |
|
| 673 |
@db_api.CONTEXT_READER |
| 674 |
def get_endpoint_group(self, context, endpoint_group_id, fields=None): |
| 675 |
endpoint_group_db = self._get_resource( |
| 676 |
context, vpn_models.VPNEndpointGroup, endpoint_group_id) |
| 677 |
return self._make_endpoint_group_dict(endpoint_group_db, fields) |
| 678 |
|
| 679 |
@db_api.CONTEXT_READER |
| 680 |
def get_endpoint_groups(self, context, filters=None, fields=None): |
| 681 |
return model_query.get_collection(context, vpn_models.VPNEndpointGroup, |
| 682 |
self._make_endpoint_group_dict, |
| 683 |
filters=filters, fields=fields) |
| 684 |
|
| 685 |
def check_endpoint_group_not_in_use(self, context, group_id): |
| 686 |
query = context.session.query(vpn_models.IPsecSiteConnection) |
| 687 |
query = query.filter( |
| 688 |
sa.or_( |
| 689 |
vpn_models.IPsecSiteConnection.local_ep_group_id == group_id, |
| 690 |
vpn_models.IPsecSiteConnection.peer_ep_group_id == group_id) |
| 691 |
) |
| 692 |
if query.first(): |
| 693 |
raise vpn_exception.EndpointGroupInUse(group_id=group_id) |
| 694 |
|
| 695 |
def get_vpnservice_router_id(self, context, vpnservice_id): |
| 696 |
with db_api.CONTEXT_READER.using(context): |
| 697 |
vpnservice = self._get_vpnservice(context, vpnservice_id) |
| 698 |
return vpnservice['router_id'] |
| 699 |
|
| 700 |
@db_api.CONTEXT_READER |
| 701 |
def get_peer_cidrs_for_router(self, context, router_id): |
| 702 |
filters = {'router_id': [router_id]} |
| 703 |
vpnservices = model_query.get_collection_query( |
| 704 |
context, vpn_models.VPNService, filters=filters).all() |
| 705 |
cidrs = [] |
| 706 |
for vpnservice in vpnservices: |
| 707 |
for ipsec_site_connection in vpnservice.ipsec_site_connections: |
| 708 |
if ipsec_site_connection.peer_cidrs: |
| 709 |
for peer_cidr in ipsec_site_connection.peer_cidrs: |
| 710 |
cidrs.append(peer_cidr.cidr) |
| 711 |
if ipsec_site_connection.peer_ep_group is not None: |
| 712 |
for ep in ipsec_site_connection.peer_ep_group.endpoints: |
| 713 |
cidrs.append(ep.endpoint) |
| 714 |
return cidrs |
| 715 |
|
| 716 |
|
| 717 |
class VPNPluginRpcDbMixin: |
| 718 |
def _build_local_subnet_cidr_map(self, context): |
| 719 |
"""Build a dict of all local endpoint subnets, with list of CIDRs.""" |
| 720 |
query = context.session.query(models_v2.Subnet.id, |
| 721 |
models_v2.Subnet.cidr) |
| 722 |
query = query.join(vpn_models.VPNEndpoint, |
| 723 |
vpn_models.VPNEndpoint.endpoint == |
| 724 |
models_v2.Subnet.id) |
| 725 |
query = query.join(vpn_models.VPNEndpointGroup, |
| 726 |
vpn_models.VPNEndpointGroup.id == |
| 727 |
vpn_models.VPNEndpoint.endpoint_group_id) |
| 728 |
query = query.join(vpn_models.IPsecSiteConnection, |
| 729 |
vpn_models.IPsecSiteConnection.local_ep_group_id == |
| 730 |
vpn_models.VPNEndpointGroup.id) |
| 731 |
return {sn.id: sn.cidr for sn in query.all()} |
| 732 |
|
| 733 |
def update_status_by_agent(self, context, service_status_info_list): |
| 734 |
"""Updating vpnservice and vpnconnection status. |
| 735 |
|
| 736 |
:param context: context variable |
| 737 |
:param service_status_info_list: list of status |
| 738 |
The structure is |
| 739 |
[{id: vpnservice_id, |
| 740 |
status: ACTIVE|DOWN|ERROR, |
| 741 |
updated_pending_status: True|False |
| 742 |
ipsec_site_connections: { |
| 743 |
ipsec_site_connection_id: { |
| 744 |
status: ACTIVE|DOWN|ERROR, |
| 745 |
updated_pending_status: True|False |
| 746 |
} |
| 747 |
}] |
| 748 |
The agent will set updated_pending_status as True, |
| 749 |
when agent updates any pending status. |
| 750 |
""" |
| 751 |
with db_api.CONTEXT_WRITER.using(context): |
| 752 |
for vpnservice in service_status_info_list: |
| 753 |
try: |
| 754 |
vpnservice_db = self._get_vpnservice( |
| 755 |
context, vpnservice['id']) |
| 756 |
except vpn_exception.VPNServiceNotFound: |
| 757 |
LOG.warning('vpnservice %s in db is already deleted', |
| 758 |
vpnservice['id']) |
| 759 |
continue |
| 760 |
|
| 761 |
if (not utils.in_pending_status(vpnservice_db.status) or |
| 762 |
vpnservice['updated_pending_status']): |
| 763 |
vpnservice_db.status = vpnservice['status'] |
| 764 |
for conn_id, conn in vpnservice[ |
| 765 |
'ipsec_site_connections'].items(): |
| 766 |
self._update_connection_status( |
| 767 |
context, conn_id, conn['status'], |
| 768 |
conn['updated_pending_status']) |
| 769 |
|
| 770 |
|
| 771 |
def vpn_router_gateway_callback(resource, event, trigger, payload=None): |
| 772 |
# the event payload objects |
| 773 |
vpn_plugin = directory.get_plugin(p_constants.VPN) |
| 774 |
if vpn_plugin: |
| 775 |
context = payload.context |
| 776 |
router_id = payload.resource_id |
| 777 |
if resource == resources.ROUTER_GATEWAY: |
| 778 |
vpn_plugin.check_router_in_use(context, router_id) |
| 779 |
elif resource == resources.ROUTER_INTERFACE: |
| 780 |
subnet_id = payload.metadata.get('subnet_id') |
| 781 |
vpn_plugin.check_subnet_in_use(context, subnet_id, router_id) |
| 782 |
|
| 783 |
|
| 784 |
def migration_callback(resource, event, trigger, payload): |
| 785 |
context = payload.context |
| 786 |
router = payload.latest_state |
| 787 |
vpn_plugin = directory.get_plugin(p_constants.VPN) |
| 788 |
if vpn_plugin: |
| 789 |
vpn_plugin.check_router_in_use(context, router['id']) |
| 790 |
return True |
| 791 |
|
| 792 |
|
| 793 |
def subnet_callback(resource, event, trigger, payload=None): |
| 794 |
"""Respond to subnet based notifications - see if subnet in use.""" |
| 795 |
context = payload.context |
| 796 |
subnet_id = payload.resource_id |
| 797 |
vpn_plugin = directory.get_plugin(p_constants.VPN) |
| 798 |
if vpn_plugin: |
| 799 |
vpn_plugin.check_subnet_in_use_by_endpoint_group(context, subnet_id) |
| 800 |
|
| 801 |
|
| 802 |
def subscribe(): |
| 803 |
registry.subscribe( |
| 804 |
vpn_router_gateway_callback, resources.ROUTER_GATEWAY, |
| 805 |
events.BEFORE_DELETE) |
| 806 |
registry.subscribe( |
| 807 |
vpn_router_gateway_callback, resources.ROUTER_INTERFACE, |
| 808 |
events.BEFORE_DELETE) |
| 809 |
registry.subscribe( |
| 810 |
migration_callback, resources.ROUTER, events.BEFORE_UPDATE) |
| 811 |
registry.subscribe( |
| 812 |
subnet_callback, resources.SUBNET, events.BEFORE_DELETE) |
| 813 |
|
| 814 |
|
| 815 |
# NOTE(armax): multiple VPN service plugins (potentially out of tree) may |
| 816 |
# inherit from vpn_db and may need the callbacks to be processed. Having an |
| 817 |
# implicit subscription (through the module import) preserves the existing |
| 818 |
# behavior, and at the same time it avoids fixing it manually in each and |
| 819 |
# every vpn plugin outta there. That said, The subscription is also made |
| 820 |
# explicitly in the reference vpn plugin. The subscription operation is |
| 821 |
# idempotent so there is no harm in registering the same callback multiple |
| 822 |
# times. |
| 823 |
subscribe() |