| 1 |
#!/usr/bin/env python |
| 2 |
# |
| 3 |
# Copyright 2014 Orange |
| 4 |
# |
| 5 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
# you may not use this file except in compliance with the License. |
| 7 |
# You may obtain 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, |
| 13 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
# See the License for the specific language governing permissions and |
| 15 |
# limitations under the License. |
| 16 |
|
| 17 |
import os |
| 18 |
import urllib.error |
| 19 |
import urllib.request |
| 20 |
|
| 21 |
import optparse |
| 22 |
from oslo_config import cfg |
| 23 |
from oslo_serialization import jsonutils |
| 24 |
|
| 25 |
from networking_bagpipe.bagpipe_bgp.api import config as api_config |
| 26 |
|
| 27 |
|
| 28 |
LOOKING_GLASS_BASE = "looking-glass" |
| 29 |
|
| 30 |
|
| 31 |
def request(options, server, args): |
| 32 |
target_url = "http://%s:%d/%s/%s" % (server, options.port, |
| 33 |
options.prefix, "/".join(args)) |
| 34 |
try: |
| 35 |
os.environ['NO_PROXY'] = server |
| 36 |
os.environ['no_proxy'] = server |
| 37 |
response = urllib.request.urlopen(target_url) |
| 38 |
|
| 39 |
if response.getcode() == 200: |
| 40 |
return jsonutils.load(response) |
| 41 |
except urllib.error.HTTPError as e: |
| 42 |
if e.code == 404: |
| 43 |
return {} |
| 44 |
print("Error requesting %s" % target_url) |
| 45 |
raise |
| 46 |
except Exception: |
| 47 |
print("Error requesting %s" % target_url) |
| 48 |
raise |
| 49 |
|
| 50 |
|
| 51 |
def normalize(string): |
| 52 |
return (string |
| 53 |
.replace("target:", "rt:") |
| 54 |
.replace(":", "_") |
| 55 |
.replace("-", "_") |
| 56 |
.replace(".", "_")) |
| 57 |
|
| 58 |
|
| 59 |
RT_TXT_STYLE = 'color="orange",fontcolor="orange"' |
| 60 |
RT_STYLE = 'color="orange",fontcolor="orange",arrowhead=onormal' |
| 61 |
RT_STYLE_REDIR_FROM_RT = ('color="orange",fontcolor="orange",style=dashed,' |
| 62 |
'arrowhead=diamond,arrowtail=oinv,headclip=false,' |
| 63 |
'dir=both') |
| 64 |
RT_STYLE_REDIR_TO_RT = ('color="orange",fontcolor="orange",style=dashed,' |
| 65 |
'arrowhead=onormal,tailclip=false,dir=both,' |
| 66 |
'arrowtail=oinv') |
| 67 |
RT_STYLE_FLOWSPEC = 'color="red",fontcolor="red",style=dotted' |
| 68 |
RT_STYLE_FLOWSPEC_INTER = ('color="red",fontcolor="red",style=dotted,' |
| 69 |
'arrowhead=none,dir=both,arrowtail=oinv') |
| 70 |
RT_STYLE_FLOWSPEC_ACTION = 'color="red",fontcolor="red",style=dashed' |
| 71 |
PORT_LINK_STYLE = 'style=dashed,dir=none,color="gray",weight=3' |
| 72 |
|
| 73 |
|
| 74 |
def get_all(options): |
| 75 |
rts = set() |
| 76 |
vpns = set() |
| 77 |
|
| 78 |
for server in options.servers: |
| 79 |
for vpn in request(options, server, ["vpns", "instances"]): |
| 80 |
vpn_id = vpn['id'] |
| 81 |
type = vpn['name'][:3] |
| 82 |
vpns.add((server, vpn_id, type)) |
| 83 |
for rt_i in request(options, server, ["vpns", "instances", vpn_id, |
| 84 |
"route_targets", "import"]): |
| 85 |
rts.add(normalize(rt_i)) |
| 86 |
for rt_e in request(options, server, ["vpns", "instances", vpn_id, |
| 87 |
"route_targets", "export"]): |
| 88 |
rts.add(normalize(rt_e)) |
| 89 |
|
| 90 |
readvertise = request(options, server, ["vpns", "instances", |
| 91 |
vpn_id, "readvertise"]) |
| 92 |
if readvertise: |
| 93 |
rts.add(normalize(readvertise['to'][0])) |
| 94 |
rts.add(normalize(readvertise['from'][0])) |
| 95 |
|
| 96 |
attract = readvertise.get('attract_traffic', None) |
| 97 |
if attract: |
| 98 |
rts |= {normalize(rt) |
| 99 |
for rt in attract['redirect_rts']} |
| 100 |
|
| 101 |
return (rts, vpns) |
| 102 |
|
| 103 |
|
| 104 |
def vpn_uid(server, vpn): |
| 105 |
return "vpn_{}__{}".format(normalize(server), normalize(vpn)) |
| 106 |
|
| 107 |
|
| 108 |
def vpn_short(vpn): |
| 109 |
if len(vpn) > 11: |
| 110 |
return vpn[:4] + ".." + vpn[-5:] |
| 111 |
else: |
| 112 |
return vpn |
| 113 |
|
| 114 |
|
| 115 |
def main(): |
| 116 |
api_config.register_config() |
| 117 |
try: |
| 118 |
cfg.CONF(args=[], |
| 119 |
project='bagpipe-impex2dot', |
| 120 |
default_config_files=['/etc/bagpipe-bgp/bgp.conf']) |
| 121 |
api_port = cfg.CONF.API.port |
| 122 |
except cfg.ConfigFilesNotFoundError: |
| 123 |
api_port = api_config.DEFAULT_PORT |
| 124 |
|
| 125 |
usage = """ %prog [options] |
| 126 |
|
| 127 |
Example: bagpipe-impex2dot --server s1 --server s2 | dot -Tpdf > impex.pdf |
| 128 |
""" |
| 129 |
parser = optparse.OptionParser(usage) |
| 130 |
|
| 131 |
parser.add_option( |
| 132 |
"--server", dest="servers", default=[], action="append", |
| 133 |
help="IP address of a BaGPipe BGP instances (default: localhost)") |
| 134 |
|
| 135 |
parser.add_option( |
| 136 |
"--port", dest="port", type="int", default=api_port, |
| 137 |
help="Port of BaGPipe BGP (optional, default: %default)") |
| 138 |
|
| 139 |
parser.add_option( |
| 140 |
"--prefix", dest="prefix", default=LOOKING_GLASS_BASE, |
| 141 |
help="Looking-glass URL Prefix (optional, default: %default)") |
| 142 |
|
| 143 |
(options, _) = parser.parse_args() |
| 144 |
|
| 145 |
if len(options.servers) == 0: |
| 146 |
options.servers = ["localhost"] |
| 147 |
|
| 148 |
ports = set() |
| 149 |
dests = set() |
| 150 |
print('digraph import_export {') |
| 151 |
|
| 152 |
print(' node [fontname="Helvetica"];') |
| 153 |
print(' edge [fontname="Helvetica"];') |
| 154 |
print(' nodesep=0.55;') |
| 155 |
|
| 156 |
(rts, vpns) = get_all(options) |
| 157 |
print(' subgraph rts {') |
| 158 |
print(' rank=same;') |
| 159 |
for rt in rts: |
| 160 |
label = rt.upper().replace('_', '\\n') |
| 161 |
print(' {} [shape="circle",label="{}",{}];'.format( |
| 162 |
rt, label, RT_TXT_STYLE)) |
| 163 |
print(' }') |
| 164 |
|
| 165 |
print(' subgraph ipvpns {') |
| 166 |
print(' rank=same;') |
| 167 |
for (server, vpn, _) in filter(lambda x: x[2] == 'VRF', vpns): |
| 168 |
server_spec = ("\\n[%s]" % server) if server != 'localhost' else '' |
| 169 |
print(' %s [label="{<0>VRF\\n%s%s|<readv>}",' |
| 170 |
'shape="record"];' % (vpn_uid(server, vpn), vpn_short(vpn), |
| 171 |
server_spec)) |
| 172 |
print(' }') |
| 173 |
|
| 174 |
print(' subgraph evpns {') |
| 175 |
for (server, vpn, _) in filter(lambda x: x[2] == 'EVI', vpns): |
| 176 |
server_spec = ("\\n[%s]" % server) if server != 'localhost' else '' |
| 177 |
print(' %s [label="{<0>EVI\\n%s%s}",' |
| 178 |
'shape="record"];' % (vpn_uid(server, vpn), vpn_short(vpn), |
| 179 |
server_spec)) |
| 180 |
print(' }') |
| 181 |
|
| 182 |
for (server, vpn, _) in vpns: |
| 183 |
print(' /* {}:{} */'.format(server, vpn)) |
| 184 |
uid = vpn_uid(server, vpn) |
| 185 |
|
| 186 |
for rt_i in request(options, server, ["vpns", "instances", vpn, |
| 187 |
"route_targets", "import"]): |
| 188 |
print(' {} -> {}:0 [{}];'.format(normalize(rt_i), uid, |
| 189 |
RT_STYLE)) |
| 190 |
|
| 191 |
for rt_e in request(options, server, ["vpns", "instances", vpn, |
| 192 |
"route_targets", "export"]): |
| 193 |
print(' {}:0 -> {} [{}];'.format(uid, normalize(rt_e), |
| 194 |
RT_STYLE)) |
| 195 |
|
| 196 |
readvertise = request(options, server, ["vpns", "instances", |
| 197 |
vpn, "readvertise"]) |
| 198 |
if readvertise: |
| 199 |
readv = "%s:readv" % vpn_uid(server, vpn) |
| 200 |
print(' %s -> %s [label="",%s];' % |
| 201 |
(normalize(readvertise['from'][0]), |
| 202 |
readv, |
| 203 |
RT_STYLE_REDIR_FROM_RT)) |
| 204 |
print(' %s -> %s [label="",%s];' % |
| 205 |
(readv, |
| 206 |
normalize(readvertise['to'][0]), |
| 207 |
RT_STYLE_REDIR_TO_RT)) |
| 208 |
|
| 209 |
attract = readvertise.get('attract_traffic', None) |
| 210 |
if attract: |
| 211 |
intermediate = "{}_{}_attract".format(normalize(server), |
| 212 |
normalize(vpn)) |
| 213 |
print(' %s [style=invis,height=0,width=0,' |
| 214 |
'fixedsize=true,rank=1]' % intermediate) |
| 215 |
|
| 216 |
redir_rt = attract['redirect_rts'][0] |
| 217 |
|
| 218 |
# readv -> intermediate |
| 219 |
print(' %s -> %s [label="flow",%s];' % |
| 220 |
(readv, intermediate, |
| 221 |
RT_STYLE_FLOWSPEC_INTER)) |
| 222 |
|
| 223 |
# intermediate -> flowspec route RT |
| 224 |
print(' %s -> %s [label="",weight=50,%s];' % |
| 225 |
(intermediate, normalize(redir_rt), |
| 226 |
RT_STYLE_FLOWSPEC)) |
| 227 |
|
| 228 |
# intermediate -> flowspec route redirect action RT |
| 229 |
print(' %s -> %s [label="action",%s,arrowhead=none' |
| 230 |
',weight=20];' % |
| 231 |
(intermediate, normalize(readvertise['to'][0]), |
| 232 |
RT_STYLE_FLOWSPEC_ACTION)) |
| 233 |
|
| 234 |
for port in request(options, server, ["vpns", "instances", |
| 235 |
vpn, "ports"]).keys(): |
| 236 |
print(' {} -> port_{}_{} [{},weight=5];'.format( |
| 237 |
uid, |
| 238 |
normalize(server), |
| 239 |
normalize(port), |
| 240 |
PORT_LINK_STYLE)) |
| 241 |
ports.add((server, normalize(port))) |
| 242 |
|
| 243 |
# possible link between an E-VPN and an IPVPN ? |
| 244 |
ipvpn = request(options, server, ["vpns", "instances", |
| 245 |
vpn, "gateway_port", "ipvpn"]) |
| 246 |
if ipvpn: |
| 247 |
ipvpn_id = ipvpn['external_instance_id'] |
| 248 |
print(' {} -> {} [weight=500];'.format( |
| 249 |
uid, |
| 250 |
vpn_uid(server, ipvpn_id))) |
| 251 |
|
| 252 |
for (server, port) in ports: |
| 253 |
print(' port_%s_%s [label="",style=invis,height=0,width=0,' |
| 254 |
'fixedsize=true];' % (normalize(server), |
| 255 |
normalize(port))) |
| 256 |
if port.startswith('to_'): |
| 257 |
dest = port.split('_')[1] |
| 258 |
print(' port_%s_%s -> dest_%s_%s [style=dashed,' |
| 259 |
'dir=none,color="gray"' |
| 260 |
',weight=5];' % |
| 261 |
(normalize(server), normalize(port), normalize(server), |
| 262 |
dest)) |
| 263 |
dests.add((server, dest)) |
| 264 |
|
| 265 |
for (server, dest) in dests: |
| 266 |
print(' dest_%s_%s [label="%s\\n[%s]",shape="square",' |
| 267 |
'color="gray"];' % (normalize(server), |
| 268 |
normalize(dest), dest, server)) |
| 269 |
|
| 270 |
print('}') |