| 1 |
# Copyright (c) 2017 Juniper networks Inc. |
| 2 |
# 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 logging |
| 18 |
|
| 19 |
from osc_lib.cli import format_columns |
| 20 |
from osc_lib.cli import parseractions |
| 21 |
from osc_lib.utils import columns as column_util |
| 22 |
|
| 23 |
from neutronclient._i18n import _ |
| 24 |
from neutronclient.osc.v2.networking_bgpvpn import constants |
| 25 |
from neutronclient.osc.v2.networking_bgpvpn import resource_association |
| 26 |
|
| 27 |
LOG = logging.getLogger(__name__) |
| 28 |
|
| 29 |
|
| 30 |
class BgpvpnPortAssoc(object): |
| 31 |
_assoc_res_name = constants.PORT_RESOURCE_NAME |
| 32 |
_resource = constants.PORT_ASSOC |
| 33 |
_resource_plural = constants.PORT_ASSOCS |
| 34 |
|
| 35 |
_attr_map = ( |
| 36 |
('id', 'ID', column_util.LIST_BOTH), |
| 37 |
('tenant_id', 'Project', column_util.LIST_LONG_ONLY), |
| 38 |
('%s_id' % _assoc_res_name, '%s ID' % _assoc_res_name.capitalize(), |
| 39 |
column_util.LIST_BOTH), |
| 40 |
('prefix_routes', 'Prefix Routes (BGP LOCAL_PREF)', |
| 41 |
column_util.LIST_LONG_ONLY), |
| 42 |
('bgpvpn_routes', 'BGP VPN Routes (BGP LOCAL_PREF)', |
| 43 |
column_util.LIST_LONG_ONLY), |
| 44 |
('advertise_fixed_ips', "Advertise Port's Fixed IPs", |
| 45 |
column_util.LIST_LONG_ONLY), |
| 46 |
) |
| 47 |
_formatters = { |
| 48 |
'prefix_routes': format_columns.ListColumn, |
| 49 |
'bgpvpn_routes': format_columns.ListColumn, |
| 50 |
} |
| 51 |
|
| 52 |
def _transform_resource(self, data): |
| 53 |
"""Transforms BGP VPN port association routes property |
| 54 |
|
| 55 |
That permits to easily format the command output with ListColumn |
| 56 |
formater and separate the two route types. |
| 57 |
|
| 58 |
{'routes': |
| 59 |
[ |
| 60 |
{ |
| 61 |
'type': 'prefix', |
| 62 |
'local_pref': 100, |
| 63 |
'prefix': '8.8.8.0/27', |
| 64 |
}, |
| 65 |
{ |
| 66 |
'type': 'prefix', |
| 67 |
'local_pref': 42, |
| 68 |
'prefix': '80.50.30.0/28', |
| 69 |
}, |
| 70 |
{ |
| 71 |
'type': 'bgpvpn', |
| 72 |
'local_pref': 50, |
| 73 |
'bgpvpn': '157d72a9-9968-48e7-8087-6c9a9bc7a181', |
| 74 |
}, |
| 75 |
{ |
| 76 |
'type': 'bgpvpn', |
| 77 |
'bgpvpn': 'd5c7aaab-c7e8-48b3-85ca-a115c00d3603', |
| 78 |
}, |
| 79 |
], |
| 80 |
} |
| 81 |
|
| 82 |
to |
| 83 |
|
| 84 |
{ |
| 85 |
'prefix_routes': [ |
| 86 |
'8.8.8.0/27 (100)', |
| 87 |
'80.50.30.0/28 (42)', |
| 88 |
], |
| 89 |
'bgpvpn_routes': [ |
| 90 |
'157d72a9-9968-48e7-8087-6c9a9bc7a181 (50)', |
| 91 |
'd5c7aaab-c7e8-48b3-85ca-a115c00d3603', |
| 92 |
], |
| 93 |
} |
| 94 |
""" |
| 95 |
for route in data.get('routes', []): |
| 96 |
local_pref = '' |
| 97 |
if route.get('local_pref'): |
| 98 |
local_pref = ' (%d)' % route.get('local_pref') |
| 99 |
if route['type'] == 'prefix': |
| 100 |
data.setdefault('prefix_routes', []).append( |
| 101 |
'%s%s' % (route['prefix'], local_pref) |
| 102 |
) |
| 103 |
elif route['type'] == 'bgpvpn': |
| 104 |
data.setdefault('bgpvpn_routes', []).append( |
| 105 |
'%s%s' % (route['bgpvpn_id'], local_pref) |
| 106 |
) |
| 107 |
else: |
| 108 |
LOG.warning("Unknown route type %s (%s).", route['type'], |
| 109 |
route) |
| 110 |
data.pop('routes', None) |
| 111 |
return data |
| 112 |
|
| 113 |
def _get_common_parser(self, parser): |
| 114 |
"""Adds to parser arguments common to create, set and unset commands. |
| 115 |
|
| 116 |
:params ArgumentParser parser: argparse object contains all command's |
| 117 |
arguments |
| 118 |
""" |
| 119 |
ADVERTISE_ROUTE = _("Fixed IPs of the port will be advertised to the " |
| 120 |
"BGP VPN%s") % ( |
| 121 |
_(' (default)') if self._action == 'create' |
| 122 |
else "") |
| 123 |
NOT_ADVERTISE_ROUTE = _("Fixed IPs of the port will not be advertised " |
| 124 |
"to the BGP VPN") |
| 125 |
|
| 126 |
LOCAL_PREF_VALUE = _(". Optionally, can control the value of the BGP " |
| 127 |
"LOCAL_PREF of the routes that will be " |
| 128 |
"advertised") |
| 129 |
|
| 130 |
ADD_PREFIX_ROUTE = _("Add prefix route in CIDR notation%s") %\ |
| 131 |
LOCAL_PREF_VALUE |
| 132 |
REMOVE_PREFIX_ROUTE = _("Remove prefix route in CIDR notation") |
| 133 |
REPEAT_PREFIX_ROUTE = _("repeat option for multiple prefix routes") |
| 134 |
|
| 135 |
ADD_BGVPVPN_ROUTE = _("Add BGP VPN route for route leaking%s") %\ |
| 136 |
LOCAL_PREF_VALUE |
| 137 |
REMOVE_BGPVPN_ROUTE = _("Remove BGP VPN route") |
| 138 |
REPEAT_BGPVPN_ROUTE = _("repeat option for multiple BGP VPN routes") |
| 139 |
|
| 140 |
group_advertise_fixed_ips = parser.add_mutually_exclusive_group() |
| 141 |
group_advertise_fixed_ips.add_argument( |
| 142 |
'--advertise-fixed-ips', |
| 143 |
action='store_true', |
| 144 |
help=NOT_ADVERTISE_ROUTE if self._action == 'unset' |
| 145 |
else ADVERTISE_ROUTE, |
| 146 |
) |
| 147 |
group_advertise_fixed_ips.add_argument( |
| 148 |
'--no-advertise-fixed-ips', |
| 149 |
action='store_true', |
| 150 |
help=ADVERTISE_ROUTE if self._action == 'unset' |
| 151 |
else NOT_ADVERTISE_ROUTE, |
| 152 |
) |
| 153 |
|
| 154 |
if self._action in ['create', 'set']: |
| 155 |
parser.add_argument( |
| 156 |
'--prefix-route', |
| 157 |
metavar="prefix=<cidr>[,local_pref=<integer>]", |
| 158 |
dest='prefix_routes', |
| 159 |
action=parseractions.MultiKeyValueAction, |
| 160 |
required_keys=['prefix'], |
| 161 |
optional_keys=['local_pref'], |
| 162 |
help="%s (%s)" % (ADD_PREFIX_ROUTE, REPEAT_PREFIX_ROUTE), |
| 163 |
) |
| 164 |
parser.add_argument( |
| 165 |
'--bgpvpn-route', |
| 166 |
metavar="bgpvpn=<BGP VPN ID or name>[,local_pref=<integer>]", |
| 167 |
dest='bgpvpn_routes', |
| 168 |
action=parseractions.MultiKeyValueAction, |
| 169 |
required_keys=['bgpvpn'], |
| 170 |
optional_keys=['local_pref'], |
| 171 |
help="%s (%s)" % (ADD_BGVPVPN_ROUTE, REPEAT_BGPVPN_ROUTE), |
| 172 |
) |
| 173 |
else: |
| 174 |
parser.add_argument( |
| 175 |
'--prefix-route', |
| 176 |
metavar="<cidr>", |
| 177 |
dest='prefix_routes', |
| 178 |
action='append', |
| 179 |
help="%s (%s)" % (REMOVE_PREFIX_ROUTE, REPEAT_PREFIX_ROUTE), |
| 180 |
) |
| 181 |
parser.add_argument( |
| 182 |
'--bgpvpn-route', |
| 183 |
metavar="<BGP VPN ID or name>", |
| 184 |
dest='bgpvpn_routes', |
| 185 |
action='append', |
| 186 |
help="%s (%s)" % (REMOVE_BGPVPN_ROUTE, REPEAT_BGPVPN_ROUTE), |
| 187 |
) |
| 188 |
if self._action != 'create': |
| 189 |
parser.add_argument( |
| 190 |
'--no-prefix-route' if self._action == 'set' else |
| 191 |
'--all-prefix-routes', |
| 192 |
dest='purge_prefix_route', |
| 193 |
action='store_true', |
| 194 |
help=_('Empty prefix route list'), |
| 195 |
) |
| 196 |
parser.add_argument( |
| 197 |
'--no-bgpvpn-route' if self._action == 'set' else |
| 198 |
'--all-bgpvpn-routes', |
| 199 |
dest='purge_bgpvpn_route', |
| 200 |
action='store_true', |
| 201 |
help=_('Empty BGP VPN route list'), |
| 202 |
) |
| 203 |
|
| 204 |
def _args2body(self, bgpvpn_id, args): |
| 205 |
client = self.app.client_manager.network |
| 206 |
attrs = {} |
| 207 |
|
| 208 |
if self._action != 'create': |
| 209 |
assoc = client.find_bgpvpn_port_association( |
| 210 |
args.resource_association_id, |
| 211 |
bgpvpn_id=bgpvpn_id) |
| 212 |
else: |
| 213 |
assoc = {'routes': []} |
| 214 |
|
| 215 |
if args.advertise_fixed_ips: |
| 216 |
attrs['advertise_fixed_ips'] = self._action != 'unset' |
| 217 |
elif args.no_advertise_fixed_ips: |
| 218 |
attrs['advertise_fixed_ips'] = self._action == 'unset' |
| 219 |
|
| 220 |
prefix_routes = None |
| 221 |
if 'purge_prefix_route' in args and args.purge_prefix_route: |
| 222 |
prefix_routes = [] |
| 223 |
else: |
| 224 |
prefix_routes = {r['prefix']: r.get('local_pref') |
| 225 |
for r in assoc['routes'] |
| 226 |
if r['type'] == 'prefix'} |
| 227 |
if args.prefix_routes: |
| 228 |
if self._action in ['create', 'set']: |
| 229 |
prefix_routes.update({r['prefix']: r.get('local_pref') |
| 230 |
for r in args.prefix_routes}) |
| 231 |
elif self._action == 'unset': |
| 232 |
for prefix in args.prefix_routes: |
| 233 |
prefix_routes.pop(prefix, None) |
| 234 |
|
| 235 |
bgpvpn_routes = None |
| 236 |
if 'purge_bgpvpn_route' in args and args.purge_bgpvpn_route: |
| 237 |
bgpvpn_routes = [] |
| 238 |
else: |
| 239 |
bgpvpn_routes = {r['bgpvpn_id']: r.get('local_pref') |
| 240 |
for r in assoc['routes'] |
| 241 |
if r['type'] == 'bgpvpn'} |
| 242 |
if args.bgpvpn_routes: |
| 243 |
if self._action == 'unset': |
| 244 |
routes = [ |
| 245 |
{'bgpvpn': bgpvpn} for bgpvpn in args.bgpvpn_routes |
| 246 |
] |
| 247 |
else: |
| 248 |
routes = args.bgpvpn_routes |
| 249 |
args_bgpvpn_routes = { |
| 250 |
client.find_bgpvpn(r['bgpvpn']).id: |
| 251 |
r.get('local_pref') |
| 252 |
for r in routes |
| 253 |
} |
| 254 |
if self._action in ['create', 'set']: |
| 255 |
bgpvpn_routes.update(args_bgpvpn_routes) |
| 256 |
elif self._action == 'unset': |
| 257 |
for bgpvpn_id in args_bgpvpn_routes: |
| 258 |
bgpvpn_routes.pop(bgpvpn_id, None) |
| 259 |
|
| 260 |
if prefix_routes is not None and not prefix_routes: |
| 261 |
attrs.setdefault('routes', []) |
| 262 |
elif prefix_routes is not None: |
| 263 |
for prefix, local_pref in prefix_routes.items(): |
| 264 |
route = { |
| 265 |
'type': 'prefix', |
| 266 |
'prefix': prefix, |
| 267 |
} |
| 268 |
if local_pref: |
| 269 |
route['local_pref'] = int(local_pref) |
| 270 |
attrs.setdefault('routes', []).append(route) |
| 271 |
if bgpvpn_routes is not None and not bgpvpn_routes: |
| 272 |
attrs.setdefault('routes', []) |
| 273 |
elif bgpvpn_routes is not None: |
| 274 |
for bgpvpn_id, local_pref in bgpvpn_routes.items(): |
| 275 |
route = { |
| 276 |
'type': 'bgpvpn', |
| 277 |
'bgpvpn_id': bgpvpn_id, |
| 278 |
} |
| 279 |
if local_pref: |
| 280 |
route['local_pref'] = int(local_pref) |
| 281 |
attrs.setdefault('routes', []).append(route) |
| 282 |
|
| 283 |
return attrs |
| 284 |
|
| 285 |
|
| 286 |
class CreateBgpvpnPortAssoc(BgpvpnPortAssoc, |
| 287 |
resource_association.CreateBgpvpnResAssoc): |
| 288 |
_description = _("Create a BGP VPN port association") |
| 289 |
|
| 290 |
|
| 291 |
class SetBgpvpnPortAssoc(BgpvpnPortAssoc, |
| 292 |
resource_association.SetBgpvpnResAssoc): |
| 293 |
_description = _("Set BGP VPN port association properties") |
| 294 |
|
| 295 |
|
| 296 |
class UnsetBgpvpnPortAssoc(BgpvpnPortAssoc, |
| 297 |
resource_association.UnsetBgpvpnResAssoc): |
| 298 |
_description = _("Unset BGP VPN port association properties") |
| 299 |
|
| 300 |
|
| 301 |
class DeleteBgpvpnPortAssoc(BgpvpnPortAssoc, |
| 302 |
resource_association.DeleteBgpvpnResAssoc): |
| 303 |
_description = _("Delete a BGP VPN port association(s) for a given BGP " |
| 304 |
"VPN") |
| 305 |
|
| 306 |
|
| 307 |
class ListBgpvpnPortAssoc(BgpvpnPortAssoc, |
| 308 |
resource_association.ListBgpvpnResAssoc): |
| 309 |
_description = _("List BGP VPN port associations for a given BGP VPN") |
| 310 |
|
| 311 |
|
| 312 |
class ShowBgpvpnPortAssoc(BgpvpnPortAssoc, |
| 313 |
resource_association.ShowBgpvpnResAssoc): |
| 314 |
_description = _("Show information of a given BGP VPN port association") |