| 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.neutron import v2_0 as neutronV20 |
| 19 |
from neutronclient.neutron.v2_0.bgp import speaker as bgp_speaker |
| 20 |
|
| 21 |
|
| 22 |
def add_common_args(parser): |
| 23 |
parser.add_argument('dragent_id', |
| 24 |
metavar='BGP_DRAGENT_ID', |
| 25 |
help=_('ID of the Dynamic Routing agent.')) |
| 26 |
parser.add_argument('bgp_speaker', |
| 27 |
metavar='BGP_SPEAKER', |
| 28 |
help=_('ID or name of the BGP speaker.')) |
| 29 |
|
| 30 |
|
| 31 |
class AddBGPSpeakerToDRAgent(neutronV20.NeutronCommand): |
| 32 |
"""Add a BGP speaker to a Dynamic Routing agent.""" |
| 33 |
|
| 34 |
def get_parser(self, prog_name): |
| 35 |
parser = super(AddBGPSpeakerToDRAgent, self).get_parser(prog_name) |
| 36 |
add_common_args(parser) |
| 37 |
return parser |
| 38 |
|
| 39 |
def take_action(self, parsed_args): |
| 40 |
neutron_client = self.get_client() |
| 41 |
_speaker_id = bgp_speaker.get_bgp_speaker_id(neutron_client, |
| 42 |
parsed_args.bgp_speaker) |
| 43 |
neutron_client.add_bgp_speaker_to_dragent( |
| 44 |
parsed_args.dragent_id, {'bgp_speaker_id': _speaker_id}) |
| 45 |
print(_('Associated BGP speaker %s to the Dynamic Routing agent.') |
| 46 |
% parsed_args.bgp_speaker, file=self.app.stdout) |
| 47 |
|
| 48 |
|
| 49 |
class RemoveBGPSpeakerFromDRAgent(neutronV20.NeutronCommand): |
| 50 |
"""Removes a BGP speaker from a Dynamic Routing agent.""" |
| 51 |
|
| 52 |
def get_parser(self, prog_name): |
| 53 |
parser = super(RemoveBGPSpeakerFromDRAgent, self).get_parser( |
| 54 |
prog_name) |
| 55 |
add_common_args(parser) |
| 56 |
return parser |
| 57 |
|
| 58 |
def take_action(self, parsed_args): |
| 59 |
neutron_client = self.get_client() |
| 60 |
_speaker_id = bgp_speaker.get_bgp_speaker_id(neutron_client, |
| 61 |
parsed_args.bgp_speaker) |
| 62 |
neutron_client.remove_bgp_speaker_from_dragent(parsed_args.dragent_id, |
| 63 |
_speaker_id) |
| 64 |
print(_('Disassociated BGP speaker %s from the ' |
| 65 |
'Dynamic Routing agent.') |
| 66 |
% parsed_args.bgp_speaker, file=self.app.stdout) |
| 67 |
|
| 68 |
|
| 69 |
class ListBGPSpeakersOnDRAgent(neutronV20.ListCommand): |
| 70 |
"""List BGP speakers hosted by a Dynamic Routing agent.""" |
| 71 |
|
| 72 |
list_columns = ['id', 'name', 'local_as', 'ip_version'] |
| 73 |
resource = 'bgp_speaker' |
| 74 |
|
| 75 |
def get_parser(self, prog_name): |
| 76 |
parser = super(ListBGPSpeakersOnDRAgent, |
| 77 |
self).get_parser(prog_name) |
| 78 |
parser.add_argument( |
| 79 |
'dragent_id', |
| 80 |
metavar='BGP_DRAGENT_ID', |
| 81 |
help=_('ID of the Dynamic Routing agent.')) |
| 82 |
return parser |
| 83 |
|
| 84 |
def call_server(self, neutron_client, search_opts, parsed_args): |
| 85 |
data = neutron_client.list_bgp_speaker_on_dragent( |
| 86 |
parsed_args.dragent_id, **search_opts) |
| 87 |
return data |
| 88 |
|
| 89 |
|
| 90 |
class ListDRAgentsHostingBGPSpeaker(neutronV20.ListCommand): |
| 91 |
"""List Dynamic Routing agents hosting a BGP speaker.""" |
| 92 |
|
| 93 |
resource = 'agent' |
| 94 |
_formatters = {} |
| 95 |
list_columns = ['id', 'host', 'admin_state_up', 'alive'] |
| 96 |
unknown_parts_flag = False |
| 97 |
|
| 98 |
def get_parser(self, prog_name): |
| 99 |
parser = super(ListDRAgentsHostingBGPSpeaker, |
| 100 |
self).get_parser(prog_name) |
| 101 |
parser.add_argument('bgp_speaker', |
| 102 |
metavar='BGP_SPEAKER', |
| 103 |
help=_('ID or name of the BGP speaker.')) |
| 104 |
return parser |
| 105 |
|
| 106 |
def extend_list(self, data, parsed_args): |
| 107 |
for agent in data: |
| 108 |
agent['alive'] = ":-)" if agent['alive'] else 'xxx' |
| 109 |
|
| 110 |
def call_server(self, neutron_client, search_opts, parsed_args): |
| 111 |
_speaker_id = bgp_speaker.get_bgp_speaker_id(neutron_client, |
| 112 |
parsed_args.bgp_speaker) |
| 113 |
search_opts['bgp_speaker'] = _speaker_id |
| 114 |
data = neutron_client.list_dragents_hosting_bgp_speaker(**search_opts) |
| 115 |
return data |