| 1 |
# Copyright 2016 Huawei Technologies India Pvt. Ltd. |
| 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 |
from neutronclient._i18n import _ |
| 18 |
from neutronclient.common import utils |
| 19 |
from neutronclient.common import validators |
| 20 |
from neutronclient.neutron import v2_0 as neutronv20 |
| 21 |
from neutronclient.neutron.v2_0.bgp import peer as bgp_peer |
| 22 |
|
| 23 |
# Allowed BGP Autonomous number range |
| 24 |
MIN_AS_NUM = 1 |
| 25 |
MAX_AS_NUM = 4294967295 |
| 26 |
|
| 27 |
|
| 28 |
def get_network_id(client, id_or_name): |
| 29 |
return neutronv20.find_resourceid_by_name_or_id(client, |
| 30 |
'network', |
| 31 |
id_or_name) |
| 32 |
|
| 33 |
|
| 34 |
def get_bgp_speaker_id(client, id_or_name): |
| 35 |
return neutronv20.find_resourceid_by_name_or_id(client, |
| 36 |
'bgp_speaker', |
| 37 |
id_or_name) |
| 38 |
|
| 39 |
|
| 40 |
def validate_speaker_attributes(parsed_args): |
| 41 |
# Validate AS number |
| 42 |
validators.validate_int_range(parsed_args, 'local_as', |
| 43 |
MIN_AS_NUM, MAX_AS_NUM) |
| 44 |
|
| 45 |
|
| 46 |
def add_common_arguments(parser): |
| 47 |
utils.add_boolean_argument( |
| 48 |
parser, '--advertise-floating-ip-host-routes', |
| 49 |
help=_('Whether to enable or disable the advertisement ' |
| 50 |
'of floating-ip host routes by the BGP speaker. ' |
| 51 |
'By default floating ip host routes will be ' |
| 52 |
'advertised by the BGP speaker.')) |
| 53 |
utils.add_boolean_argument( |
| 54 |
parser, '--advertise-tenant-networks', |
| 55 |
help=_('Whether to enable or disable the advertisement ' |
| 56 |
'of tenant network routes by the BGP speaker. ' |
| 57 |
'By default tenant network routes will be ' |
| 58 |
'advertised by the BGP speaker.')) |
| 59 |
|
| 60 |
|
| 61 |
def args2body_common_arguments(body, parsed_args): |
| 62 |
neutronv20.update_dict(parsed_args, body, |
| 63 |
['name', |
| 64 |
'advertise_floating_ip_host_routes', |
| 65 |
'advertise_tenant_networks']) |
| 66 |
|
| 67 |
|
| 68 |
class ListSpeakers(neutronv20.ListCommand): |
| 69 |
"""List BGP speakers.""" |
| 70 |
|
| 71 |
resource = 'bgp_speaker' |
| 72 |
list_columns = ['id', 'name', 'local_as', 'ip_version'] |
| 73 |
pagination_support = True |
| 74 |
sorting_support = True |
| 75 |
|
| 76 |
|
| 77 |
class ShowSpeaker(neutronv20.ShowCommand): |
| 78 |
"""Show information of a given BGP speaker.""" |
| 79 |
|
| 80 |
resource = 'bgp_speaker' |
| 81 |
|
| 82 |
|
| 83 |
class CreateSpeaker(neutronv20.CreateCommand): |
| 84 |
"""Create a BGP Speaker.""" |
| 85 |
|
| 86 |
resource = 'bgp_speaker' |
| 87 |
|
| 88 |
def add_known_arguments(self, parser): |
| 89 |
parser.add_argument( |
| 90 |
'name', |
| 91 |
metavar='NAME', |
| 92 |
help=_('Name of the BGP speaker to create.')) |
| 93 |
parser.add_argument( |
| 94 |
'--local-as', |
| 95 |
metavar='LOCAL_AS', |
| 96 |
required=True, |
| 97 |
help=_('Local AS number. (Integer in [%(min_val)s, %(max_val)s] ' |
| 98 |
'is allowed.)') % {'min_val': MIN_AS_NUM, |
| 99 |
'max_val': MAX_AS_NUM}) |
| 100 |
parser.add_argument( |
| 101 |
'--ip-version', |
| 102 |
type=int, choices=[4, 6], |
| 103 |
default=4, |
| 104 |
help=_('IP version for the BGP speaker (default is 4).')) |
| 105 |
add_common_arguments(parser) |
| 106 |
|
| 107 |
def args2body(self, parsed_args): |
| 108 |
body = {} |
| 109 |
validate_speaker_attributes(parsed_args) |
| 110 |
body['local_as'] = parsed_args.local_as |
| 111 |
body['ip_version'] = parsed_args.ip_version |
| 112 |
args2body_common_arguments(body, parsed_args) |
| 113 |
return {self.resource: body} |
| 114 |
|
| 115 |
|
| 116 |
class UpdateSpeaker(neutronv20.UpdateCommand): |
| 117 |
"""Update BGP Speaker's information.""" |
| 118 |
|
| 119 |
resource = 'bgp_speaker' |
| 120 |
|
| 121 |
def add_known_arguments(self, parser): |
| 122 |
parser.add_argument( |
| 123 |
'--name', |
| 124 |
help=_('Name of the BGP speaker to update.')) |
| 125 |
add_common_arguments(parser) |
| 126 |
|
| 127 |
def args2body(self, parsed_args): |
| 128 |
body = {} |
| 129 |
args2body_common_arguments(body, parsed_args) |
| 130 |
return {self.resource: body} |
| 131 |
|
| 132 |
|
| 133 |
class DeleteSpeaker(neutronv20.DeleteCommand): |
| 134 |
"""Delete a BGP speaker.""" |
| 135 |
|
| 136 |
resource = 'bgp_speaker' |
| 137 |
|
| 138 |
|
| 139 |
class AddPeerToSpeaker(neutronv20.NeutronCommand): |
| 140 |
"""Add a peer to the BGP speaker.""" |
| 141 |
|
| 142 |
def get_parser(self, prog_name): |
| 143 |
parser = super(AddPeerToSpeaker, self).get_parser(prog_name) |
| 144 |
parser.add_argument( |
| 145 |
'bgp_speaker', |
| 146 |
metavar='BGP_SPEAKER', |
| 147 |
help=_('ID or name of the BGP speaker.')) |
| 148 |
parser.add_argument( |
| 149 |
'bgp_peer', |
| 150 |
metavar='BGP_PEER', |
| 151 |
help=_('ID or name of the BGP peer to add.')) |
| 152 |
return parser |
| 153 |
|
| 154 |
def take_action(self, parsed_args): |
| 155 |
neutron_client = self.get_client() |
| 156 |
_speaker_id = get_bgp_speaker_id(neutron_client, |
| 157 |
parsed_args.bgp_speaker) |
| 158 |
_peer_id = bgp_peer.get_bgp_peer_id(neutron_client, |
| 159 |
parsed_args.bgp_peer) |
| 160 |
neutron_client.add_peer_to_bgp_speaker(_speaker_id, |
| 161 |
{'bgp_peer_id': _peer_id}) |
| 162 |
print(_('Added BGP peer %(peer)s to BGP speaker %(speaker)s.') % |
| 163 |
{'peer': parsed_args.bgp_peer, |
| 164 |
'speaker': parsed_args.bgp_speaker}, |
| 165 |
file=self.app.stdout) |
| 166 |
|
| 167 |
|
| 168 |
class RemovePeerFromSpeaker(neutronv20.NeutronCommand): |
| 169 |
"""Remove a peer from the BGP speaker.""" |
| 170 |
|
| 171 |
def get_parser(self, prog_name): |
| 172 |
parser = super(RemovePeerFromSpeaker, self).get_parser(prog_name) |
| 173 |
parser.add_argument( |
| 174 |
'bgp_speaker', |
| 175 |
metavar='BGP_SPEAKER', |
| 176 |
help=_('ID or name of the BGP speaker.')) |
| 177 |
parser.add_argument( |
| 178 |
'bgp_peer', |
| 179 |
metavar='BGP_PEER', |
| 180 |
help=_('ID or name of the BGP peer to remove.')) |
| 181 |
return parser |
| 182 |
|
| 183 |
def take_action(self, parsed_args): |
| 184 |
neutron_client = self.get_client() |
| 185 |
_speaker_id = get_bgp_speaker_id(neutron_client, |
| 186 |
parsed_args.bgp_speaker) |
| 187 |
_peer_id = bgp_peer.get_bgp_peer_id(neutron_client, |
| 188 |
parsed_args.bgp_peer) |
| 189 |
neutron_client.remove_peer_from_bgp_speaker(_speaker_id, |
| 190 |
{'bgp_peer_id': _peer_id}) |
| 191 |
print(_('Removed BGP peer %(peer)s from BGP speaker %(speaker)s.') % |
| 192 |
{'peer': parsed_args.bgp_peer, |
| 193 |
'speaker': parsed_args.bgp_speaker}, |
| 194 |
file=self.app.stdout) |
| 195 |
|
| 196 |
|
| 197 |
class AddNetworkToSpeaker(neutronv20.NeutronCommand): |
| 198 |
"""Add a network to the BGP speaker.""" |
| 199 |
|
| 200 |
def get_parser(self, prog_name): |
| 201 |
parser = super(AddNetworkToSpeaker, self).get_parser(prog_name) |
| 202 |
parser.add_argument( |
| 203 |
'bgp_speaker', |
| 204 |
metavar='BGP_SPEAKER', |
| 205 |
help=_('ID or name of the BGP speaker.')) |
| 206 |
parser.add_argument( |
| 207 |
'network', |
| 208 |
metavar='NETWORK', |
| 209 |
help=_('ID or name of the network to add.')) |
| 210 |
return parser |
| 211 |
|
| 212 |
def take_action(self, parsed_args): |
| 213 |
neutron_client = self.get_client() |
| 214 |
_speaker_id = get_bgp_speaker_id(neutron_client, |
| 215 |
parsed_args.bgp_speaker) |
| 216 |
_net_id = get_network_id(neutron_client, |
| 217 |
parsed_args.network) |
| 218 |
neutron_client.add_network_to_bgp_speaker(_speaker_id, |
| 219 |
{'network_id': _net_id}) |
| 220 |
print(_('Added network %(net)s to BGP speaker %(speaker)s.') % |
| 221 |
{'net': parsed_args.network, 'speaker': parsed_args.bgp_speaker}, |
| 222 |
file=self.app.stdout) |
| 223 |
|
| 224 |
|
| 225 |
class RemoveNetworkFromSpeaker(neutronv20.NeutronCommand): |
| 226 |
"""Remove a network from the BGP speaker.""" |
| 227 |
|
| 228 |
def get_parser(self, prog_name): |
| 229 |
parser = super(RemoveNetworkFromSpeaker, self).get_parser(prog_name) |
| 230 |
parser.add_argument( |
| 231 |
'bgp_speaker', |
| 232 |
metavar='BGP_SPEAKER', |
| 233 |
help=_('ID or name of the BGP speaker.')) |
| 234 |
parser.add_argument( |
| 235 |
'network', |
| 236 |
metavar='NETWORK', |
| 237 |
help=_('ID or name of the network to remove.')) |
| 238 |
return parser |
| 239 |
|
| 240 |
def take_action(self, parsed_args): |
| 241 |
neutron_client = self.get_client() |
| 242 |
_speaker_id = get_bgp_speaker_id(neutron_client, |
| 243 |
parsed_args.bgp_speaker) |
| 244 |
_net_id = get_network_id(neutron_client, |
| 245 |
parsed_args.network) |
| 246 |
neutron_client.remove_network_from_bgp_speaker(_speaker_id, |
| 247 |
{'network_id': _net_id}) |
| 248 |
print(_('Removed network %(net)s from BGP speaker %(speaker)s.') % |
| 249 |
{'net': parsed_args.network, 'speaker': parsed_args.bgp_speaker}, |
| 250 |
file=self.app.stdout) |
| 251 |
|
| 252 |
|
| 253 |
class ListRoutesAdvertisedBySpeaker(neutronv20.ListCommand): |
| 254 |
"""List routes advertised by a given BGP speaker.""" |
| 255 |
|
| 256 |
list_columns = ['id', 'destination', 'next_hop'] |
| 257 |
resource = 'advertised_route' |
| 258 |
pagination_support = True |
| 259 |
sorting_support = True |
| 260 |
|
| 261 |
def get_parser(self, prog_name): |
| 262 |
parser = super(ListRoutesAdvertisedBySpeaker, |
| 263 |
self).get_parser(prog_name) |
| 264 |
parser.add_argument( |
| 265 |
'bgp_speaker', |
| 266 |
metavar='BGP_SPEAKER', |
| 267 |
help=_('ID or name of the BGP speaker.')) |
| 268 |
return parser |
| 269 |
|
| 270 |
def call_server(self, neutron_client, search_opts, parsed_args): |
| 271 |
_speaker_id = get_bgp_speaker_id(neutron_client, |
| 272 |
parsed_args.bgp_speaker) |
| 273 |
data = neutron_client.list_route_advertised_from_bgp_speaker( |
| 274 |
_speaker_id, **search_opts) |
| 275 |
return data |