| 1 |
# Copyright 2017 Intel Corporation. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import logging |
| 16 |
|
| 17 |
from osc_lib.command import command |
| 18 |
from osc_lib import exceptions |
| 19 |
from osc_lib import utils |
| 20 |
from osc_lib.utils import columns as column_util |
| 21 |
|
| 22 |
from neutronclient._i18n import _ |
| 23 |
|
| 24 |
LOG = logging.getLogger(__name__) |
| 25 |
|
| 26 |
resource = 'service_graph' |
| 27 |
|
| 28 |
_attr_map = ( |
| 29 |
('id', 'ID', column_util.LIST_BOTH), |
| 30 |
('name', 'Name', column_util.LIST_BOTH), |
| 31 |
('port_chains', 'Branching Points', column_util.LIST_BOTH), |
| 32 |
('description', 'Description', column_util.LIST_LONG_ONLY), |
| 33 |
('project_id', 'Project', column_util.LIST_LONG_ONLY), |
| 34 |
) |
| 35 |
|
| 36 |
_attr_map_dict = { |
| 37 |
'id': 'ID', |
| 38 |
'name': 'Name', |
| 39 |
'description': 'Description', |
| 40 |
'port_chains': 'Branching Points', |
| 41 |
'tenant_id': 'Project', |
| 42 |
'project_id': 'Project', |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
class CreateSfcServiceGraph(command.ShowOne): |
| 47 |
"""Create a service graph.""" |
| 48 |
def get_parser(self, prog_name): |
| 49 |
parser = super(CreateSfcServiceGraph, self).get_parser(prog_name) |
| 50 |
parser.add_argument( |
| 51 |
'name', |
| 52 |
metavar='<name>', |
| 53 |
help=_('Name of the service graph.')) |
| 54 |
parser.add_argument( |
| 55 |
'--description', |
| 56 |
help=_('Description for the service graph.')) |
| 57 |
parser.add_argument( |
| 58 |
'--branching-point', |
| 59 |
metavar='SRC_CHAIN:DST_CHAIN_1,DST_CHAIN_2,DST_CHAIN_N', |
| 60 |
dest='branching_points', |
| 61 |
action='append', |
| 62 |
default=[], required=True, |
| 63 |
help=_('Service graph branching point: the key is the source ' |
| 64 |
'Port Chain while the value is a list of destination ' |
| 65 |
'Port Chains. This option can be repeated.')) |
| 66 |
return parser |
| 67 |
|
| 68 |
def take_action(self, parsed_args): |
| 69 |
client = self.app.client_manager.network |
| 70 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args) |
| 71 |
try: |
| 72 |
obj = client.create_sfc_service_graph(**attrs) |
| 73 |
display_columns, columns = \ |
| 74 |
utils.get_osc_show_columns_for_sdk_resource( |
| 75 |
obj, _attr_map_dict, ['location', 'tenant_id']) |
| 76 |
data = utils.get_dict_properties(obj, columns) |
| 77 |
return display_columns, data |
| 78 |
except Exception as e: |
| 79 |
msg = (_("Failed to create service graph using '%(pcs)s': %(e)s") |
| 80 |
% {'pcs': parsed_args.branching_points, 'e': e}) |
| 81 |
raise exceptions.CommandError(msg) |
| 82 |
|
| 83 |
|
| 84 |
class SetSfcServiceGraph(command.Command): |
| 85 |
_description = _("Set service graph properties") |
| 86 |
|
| 87 |
def get_parser(self, prog_name): |
| 88 |
parser = super(SetSfcServiceGraph, self).get_parser(prog_name) |
| 89 |
parser.add_argument( |
| 90 |
'--name', |
| 91 |
metavar='<name>', |
| 92 |
help=_('Name of the service graph')) |
| 93 |
parser.add_argument( |
| 94 |
'--description', |
| 95 |
metavar='<description>', |
| 96 |
help=_('Description for the service graph')) |
| 97 |
parser.add_argument( |
| 98 |
'service_graph', |
| 99 |
metavar='<service-graph>', |
| 100 |
help=_("Service graph to modify (name or ID)") |
| 101 |
) |
| 102 |
return parser |
| 103 |
|
| 104 |
def take_action(self, parsed_args): |
| 105 |
client = self.app.client_manager.network |
| 106 |
service_graph_id = client.find_sfc_service_graph( |
| 107 |
parsed_args.service_graph, ignore_missing=False)['id'] |
| 108 |
attrs = _get_common_attrs(self.app.client_manager, parsed_args, |
| 109 |
is_create=False) |
| 110 |
try: |
| 111 |
client.update_sfc_service_graph(service_graph_id, **attrs) |
| 112 |
except Exception as e: |
| 113 |
msg = (_("Failed to update service graph " |
| 114 |
"'%(service_graph)s': %(e)s") |
| 115 |
% {'service_graph': parsed_args.service_graph, 'e': e}) |
| 116 |
raise exceptions.CommandError(msg) |
| 117 |
|
| 118 |
|
| 119 |
class DeleteSfcServiceGraph(command.Command): |
| 120 |
"""Delete a given service graph.""" |
| 121 |
|
| 122 |
def get_parser(self, prog_name): |
| 123 |
parser = super(DeleteSfcServiceGraph, self).get_parser(prog_name) |
| 124 |
parser.add_argument( |
| 125 |
'service_graph', |
| 126 |
metavar="<service-graph>", |
| 127 |
nargs='+', |
| 128 |
help=_("ID or name of the service graph(s) to delete.") |
| 129 |
) |
| 130 |
return parser |
| 131 |
|
| 132 |
def take_action(self, parsed_args): |
| 133 |
client = self.app.client_manager.network |
| 134 |
result = 0 |
| 135 |
for sg in parsed_args.service_graph: |
| 136 |
try: |
| 137 |
sg_id = client.find_sfc_service_graph( |
| 138 |
sg, ignore_missing=False)['id'] |
| 139 |
client.delete_sfc_service_graph(sg_id) |
| 140 |
except Exception as e: |
| 141 |
result += 1 |
| 142 |
LOG.error(_("Failed to delete service graph with name " |
| 143 |
"or ID '%(sg)s': %(e)s"), |
| 144 |
{'sg': sg, 'e': e}) |
| 145 |
if result > 0: |
| 146 |
total = len(parsed_args.service_graph) |
| 147 |
msg = (_("%(result)s of %(total)s service graph(s) " |
| 148 |
"failed to delete.") % {'result': result, |
| 149 |
'total': total}) |
| 150 |
raise exceptions.CommandError(msg) |
| 151 |
|
| 152 |
|
| 153 |
class ListSfcServiceGraph(command.Lister): |
| 154 |
_description = _("List service graphs") |
| 155 |
|
| 156 |
def get_parser(self, prog_name): |
| 157 |
parser = super(ListSfcServiceGraph, self).get_parser(prog_name) |
| 158 |
parser.add_argument( |
| 159 |
'--long', |
| 160 |
action='store_true', |
| 161 |
default=False, |
| 162 |
help=_("List additional fields in output") |
| 163 |
) |
| 164 |
return parser |
| 165 |
|
| 166 |
def take_action(self, parsed_args): |
| 167 |
client = self.app.client_manager.network |
| 168 |
data = client.sfc_service_graphs() |
| 169 |
headers, columns = column_util.get_column_definitions( |
| 170 |
_attr_map, long_listing=parsed_args.long) |
| 171 |
return (headers, |
| 172 |
(utils.get_dict_properties(s, columns) |
| 173 |
for s in data)) |
| 174 |
|
| 175 |
|
| 176 |
class ShowSfcServiceGraph(command.ShowOne): |
| 177 |
"""Show information of a given service graph.""" |
| 178 |
|
| 179 |
def get_parser(self, prog_name): |
| 180 |
parser = super(ShowSfcServiceGraph, self).get_parser(prog_name) |
| 181 |
parser.add_argument( |
| 182 |
'service_graph', |
| 183 |
metavar="<service-graph>", |
| 184 |
help=_("ID or name of the service graph to display.") |
| 185 |
) |
| 186 |
return parser |
| 187 |
|
| 188 |
def take_action(self, parsed_args): |
| 189 |
client = self.app.client_manager.network |
| 190 |
sg_id = client.find_sfc_service_graph(parsed_args.service_graph, |
| 191 |
ignore_missing=False)['id'] |
| 192 |
obj = client.get_sfc_service_graph(sg_id) |
| 193 |
display_columns, columns = utils.get_osc_show_columns_for_sdk_resource( |
| 194 |
obj, _attr_map_dict, ['location', 'tenant_id']) |
| 195 |
data = utils.get_dict_properties(obj, columns) |
| 196 |
return display_columns, data |
| 197 |
|
| 198 |
|
| 199 |
def _get_common_attrs(client_manager, parsed_args, is_create=True): |
| 200 |
attrs = {} |
| 201 |
if parsed_args.name is not None: |
| 202 |
attrs['name'] = str(parsed_args.name) |
| 203 |
if parsed_args.description is not None: |
| 204 |
attrs['description'] = str(parsed_args.description) |
| 205 |
if is_create: |
| 206 |
_get_attrs_for_create(client_manager, attrs, parsed_args) |
| 207 |
return attrs |
| 208 |
|
| 209 |
|
| 210 |
def _validate_destination_chains(comma_split, attrs, client, sc_): |
| 211 |
for e in comma_split: |
| 212 |
if e != "": |
| 213 |
dc_ = client.find_sfc_port_chain(e, ignore_missing=False)['id'] |
| 214 |
attrs['port_chains'][sc_].append(dc_) |
| 215 |
if _check_cycle(attrs['port_chains'], sc_, dc_): |
| 216 |
raise exceptions.CommandError( |
| 217 |
"Error: Service graph contains a cycle") |
| 218 |
else: |
| 219 |
raise exceptions.CommandError( |
| 220 |
"Error: you must specify at least one " |
| 221 |
"destination chain for each source chain") |
| 222 |
return attrs |
| 223 |
|
| 224 |
|
| 225 |
def _check_cycle(graph, new_src, new_dest): |
| 226 |
for src in graph: |
| 227 |
if src == new_dest: |
| 228 |
if _visit(graph, src, new_dest, new_src): |
| 229 |
return True |
| 230 |
return False |
| 231 |
|
| 232 |
|
| 233 |
def _visit(graph, src, new_dest, new_src): |
| 234 |
if src in graph: |
| 235 |
found_cycle = False |
| 236 |
for dest in graph[src]: |
| 237 |
if new_src == dest or found_cycle: |
| 238 |
return True |
| 239 |
else: |
| 240 |
found_cycle = _visit(graph, dest, new_dest, new_src) |
| 241 |
return False |
| 242 |
|
| 243 |
|
| 244 |
def _get_attrs_for_create(client_manager, attrs, parsed_args): |
| 245 |
client = client_manager.network |
| 246 |
if parsed_args.branching_points: |
| 247 |
attrs['port_chains'] = {} |
| 248 |
src_chain = None |
| 249 |
for c in parsed_args.branching_points: |
| 250 |
if ':' not in c: |
| 251 |
raise exceptions.CommandError( |
| 252 |
"Error: You must specify at least one " |
| 253 |
"destination chain for each source chain.") |
| 254 |
colon_split = c.split(':') |
| 255 |
src_chain = colon_split.pop(0) |
| 256 |
sc_ = client.find_sfc_port_chain(src_chain, |
| 257 |
ignore_missing=False)['id'] |
| 258 |
for i in colon_split: |
| 259 |
comma_split = i.split(',') |
| 260 |
unique = set(comma_split) |
| 261 |
if len(unique) != len(comma_split): |
| 262 |
raise exceptions.CommandError( |
| 263 |
"Error: Duplicate " |
| 264 |
"destination chains from " |
| 265 |
"source chain {}".format(src_chain)) |
| 266 |
if sc_ in attrs['port_chains']: |
| 267 |
raise exceptions.CommandError( |
| 268 |
"Error: Source chain {} is in " |
| 269 |
"use already ".format(src_chain)) |
| 270 |
attrs['port_chains'][sc_] = [] |
| 271 |
_validate_destination_chains( |
| 272 |
comma_split, attrs, client, sc_) |