Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inventory-provider
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
geant-swd
dashboardv3
inventory-provider
Commits
f84930d4
Commit
f84930d4
authored
6 years ago
by
Robert Latta
Browse files
Options
Downloads
Patches
Plain Diff
Replaced code generated SQL with single statement. Added string conversion to port
parent
12a3a652
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
inventory_provider/opsdb.py
+174
-112
174 additions, 112 deletions
inventory_provider/opsdb.py
with
174 additions
and
112 deletions
inventory_provider/opsdb.py
+
174
−
112
View file @
f84930d4
...
@@ -2,104 +2,171 @@ import contextlib
...
@@ -2,104 +2,171 @@ import contextlib
import
mysql.connector
import
mysql.connector
equipment_location_query
=
"
SELECT
"
\
equipment_location_query
=
"""
SELECT
"
e.absid,
"
\
e.absid,
"
e.name AS equipment_name,
"
\
e.name AS equipment_name,
"
p.name AS pop_name,
"
\
p.name AS pop_name,
"
p.abbreviation AS pop_abbreviation,
"
\
p.abbreviation AS pop_abbreviation,
"
p.site_id AS pop_site_id,
"
\
p.site_id AS pop_site_id,
"
p.country,
"
\
p.country,
"
g.longitude,
"
\
g.longitude,
"
g.latitude
"
\
g.latitude
"
FROM
"
\
FROM
"
equipment e
"
\
equipment e
"
INNER JOIN pop p
"
\
INNER JOIN pop p
"
ON p.absid = e.PTR_pop
"
\
ON p.absid = e.PTR_pop
"
INNER JOIN geocoding g
"
\
INNER JOIN geocoding g
"
ON g.absid = p.PTR_geocoding
"
\
ON g.absid = p.PTR_geocoding
"
WHERE
"
\
WHERE
"
e.status !=
'
terminated
'
"
\
e.status !=
'
terminated
'
"
AND e.status !=
'
disposed
'"
AND e.status !=
'
disposed
'"""
circuit_hierarchy_query
=
"
SELECT
"
\
circuit_hierarchy_query
=
"""
SELECT
"
pc.name AS parent_circuit,
"
\
pc.name AS parent_circuit,
"
pc.absid AS parent_circuit_id,
"
\
pc.absid AS parent_circuit_id,
"
pc.status AS parent_circuit_status,
"
\
pc.status AS parent_circuit_status,
"
cc.name AS child_circuit,
"
\
cc.name AS child_circuit,
"
cc.absid AS child_circuit_id,
"
\
cc.absid AS child_circuit_id,
"
cc.status AS child_circuit_status,
"
\
cc.status AS child_circuit_status,
"
cg.segment_group AS segment_group
"
\
cg.segment_group AS segment_group
"
FROM circuit_glue cg
"
\
FROM circuit_glue cg
"
INNER JOIN circuit pc ON pc.absid = cg.PTR_circuit
"
\
INNER JOIN circuit pc ON pc.absid = cg.PTR_circuit
"
INNER JOIN circuit cc ON cc.absid = cg.PTR_component
"
INNER JOIN circuit cc ON cc.absid = cg.PTR_component
"""
retrieve_services_query_template
=
"
SELECT
"
\
retrieve_services_query
=
"""
SELECT *
"
c.absid AS id,
"
\
FROM (SELECT
"
c.name,
"
\
c.absid AS id,
"
c.status,
"
\
c.name,
"
c.circuit_type,
"
\
c.status,
"
c.service_type,
"
\
c.circuit_type,
"
events.short_descr AS project,
"
\
c.service_type,
"
e.name AS equipment,
"
\
events.short_descr AS project,
"
cc.{} AS port,
"
\
e.name AS equipment,
"
cc.{} AS logical_unit,
"
\
cc.port_a AS port,
"
LOWER(o.name) AS manufacturer,
"
\
cc.int_LU_a AS logical_unit,
"
LOWER(ec.card_id) AS card_id,
"
\
LOWER(o.name) AS manufacturer,
"
LOWER(IF(pp.interface_name IS NULL,
"
\
LOWER(ec.card_id) AS card_id,
"
''
,
"
\
LOWER(
"
pp.interface_name))
"
\
IF(pp.interface_name IS NULL,
"
AS interface_name
"
\
''
, pp.interface_name)) AS interface_name
"
FROM circuit c
"
\
FROM circuit c
"
INNER JOIN circuit_connections cc
"
\
INNER JOIN circuit_connections cc
"
ON cc.circ_absid = c.absid
"
\
ON cc.circ_absid = c.absid
"
INNER JOIN equipment e
"
\
INNER JOIN equipment e
"
ON e.absid = cc.{}
"
\
ON e.absid = cc.PTR_equip_a
"
LEFT JOIN events
"
\
LEFT JOIN events
"
ON events.absid = cc.PTR_project
"
\
ON events.absid = cc.PTR_project
"
INNER JOIN equipment_card ec
"
\
INNER JOIN equipment_card ec
"
ON ec.absid = cc.{}
"
\
ON ec.absid = cc.PTR_card_a
"
LEFT JOIN organisation o
"
\
LEFT JOIN organisation o
"
ON o.absid = ec.manufacturer
"
\
ON o.absid = ec.manufacturer
"
LEFT JOIN port_plugin pp
"
\
LEFT JOIN port_plugin pp
"
ON pp.PTR_card = cc.{}
"
\
ON pp.PTR_card = cc.PTR_card_a AND pp.port = cc.port_a
"
AND pp.port = cc.{}
"
\
WHERE c.status !=
'
terminated
'
AND is_circuit = 1
"
WHERE
"
\
UNION
"
c.status !=
'
terminated
'
"
\
SELECT
"
AND is_circuit = 1
"
c.absid AS id,
c.name,
order_services_outer_query
=
"
SELECT * FROM ({}) AS inner_query
"
\
c.status,
"
ORDER BY FIELD(status,
'
spare
'
,
'
planned
'
,
"
\
c.circuit_type,
"
'
ordered
'
,
'
installed
'
,
'
operational
'
)
"
c.service_type,
events.short_descr AS project,
e.name AS equipment,
connection_variants
=
[
cc.port_b AS port,
[
'
port_a
'
,
cc.int_LU_b AS logical_unit,
'
int_LU_a
'
,
LOWER(o.name) AS manufacturer,
'
PTR_equip_a
'
,
LOWER(ec.card_id) AS card_id,
'
PTR_card_a
'
,
LOWER(
'
PTR_card_a
'
,
IF(pp.interface_name IS NULL,
'
port_a
'
],
''
, pp.interface_name)) AS interface_name
[
'
port_b
'
,
FROM circuit c
'
int_LU_b
'
,
INNER JOIN circuit_connections cc
'
PTR_equip_b
'
,
ON cc.circ_absid = c.absid
'
PTR_card_b
'
,
INNER JOIN equipment e
'
PTR_card_b
'
,
ON e.absid = cc.PTR_equip_b
'
port_b
'
],
LEFT JOIN events
[
'
port_a_OUT
'
,
ON events.absid = cc.PTR_project
'
int_LU_a
'
,
INNER JOIN equipment_card ec
'
PTR_equip_a
'
,
ON ec.absid = cc.PTR_card_b
'
PTR_card_a_OUT
'
,
LEFT JOIN organisation o
'
PTR_card_a_OUT
'
,
ON o.absid = ec.manufacturer
'
port_a_OUT
'
],
LEFT JOIN port_plugin pp
[
'
port_b_OUT
'
,
ON pp.PTR_card = cc.PTR_card_b AND pp.port = cc.port_b
'
int_LU_b
'
,
WHERE c.status !=
'
terminated
'
AND is_circuit = 1
'
PTR_equip_b
'
,
UNION
'
PTR_card_b_OUT
'
,
SELECT
'
PTR_card_b_OUT
'
,
c.absid AS id,
'
port_b_OUT
'
]
c.name,
]
c.status,
c.circuit_type,
c.service_type,
events.short_descr AS project,
e.name AS equipment,
cc.port_a_OUT AS port,
cc.int_LU_a AS logical_unit,
LOWER(o.name) AS manufacturer,
LOWER(ec.card_id) AS card_id,
LOWER(
IF(pp.interface_name IS NULL,
''
, pp.interface_name)) AS interface_name
FROM circuit c
INNER JOIN circuit_connections cc
ON cc.circ_absid = c.absid
INNER JOIN equipment e
ON e.absid = cc.PTR_equip_a
LEFT JOIN events
ON events.absid = cc.PTR_project
INNER JOIN equipment_card ec
ON ec.absid = cc.PTR_card_a_OUT
LEFT JOIN organisation o
ON o.absid = ec.manufacturer
LEFT JOIN port_plugin pp
ON pp.PTR_card = cc.PTR_card_a_OUT
AND pp.port = cc.port_a_OUT
WHERE c.status !=
'
terminated
'
AND is_circuit = 1
UNION
SELECT
c.absid AS id,
c.name,
c.status,
c.circuit_type,
c.service_type,
events.short_descr AS project,
e.name AS equipment,
cc.port_b_OUT AS port,
cc.int_LU_b AS logical_unit,
LOWER(o.name) AS manufacturer,
LOWER(ec.card_id) AS card_id,
LOWER(
IF(pp.interface_name IS NULL,
''
, pp.interface_name)) AS interface_name
FROM circuit c
INNER JOIN circuit_connections cc
ON cc.circ_absid = c.absid
INNER JOIN equipment e
ON e.absid = cc.PTR_equip_b
LEFT JOIN events
ON events.absid = cc.PTR_project
INNER JOIN equipment_card ec
ON ec.absid = cc.PTR_card_b_OUT
LEFT JOIN organisation o
ON o.absid = ec.manufacturer
LEFT JOIN port_plugin pp
ON pp.PTR_card = cc.PTR_card_b_OUT
AND pp.port = cc.port_b_OUT
WHERE
c.status !=
'
terminated
'
AND is_circuit = 1)
AS inner_query
ORDER BY
FIELD(status,
'
spare
'
,
'
planned
'
,
'
ordered
'
,
'
installed
'
,
'
operational
'
)
"""
@contextlib.contextmanager
@contextlib.contextmanager
...
@@ -136,14 +203,19 @@ def _convert_to_dict(crs):
...
@@ -136,14 +203,19 @@ def _convert_to_dict(crs):
def
_infinera_field_update
(
record
):
def
_infinera_field_update
(
record
):
equipment_parts
=
record
[
"
equipment
"
].
rsplit
(
"
-
"
,
1
)
equipment_parts
=
record
[
"
equipment
"
].
rsplit
(
"
-
"
,
1
)
card_parts
=
record
[
"
card_id
"
].
split
(
"
-
"
,
1
)
card_parts
=
record
[
"
card_id
"
].
split
(
"
-
"
,
1
)
record
[
"
interface_name
"
]
=
""
record
[
"
equipment
"
]
=
equipment_parts
[
0
]
try
:
try
:
record
[
"
equipment
"
]
=
equipment_parts
[
0
]
record
[
"
interface_name
"
]
=
equipment_parts
[
1
]
+
"
-
"
record
[
"
interface_name
"
]
=
equipment_parts
[
1
]
+
"
-
"
+
card_parts
[
1
]
except
IndexError
:
except
IndexError
:
pass
# Nothing to see here
pass
# Nothing to see here
try
:
record
[
"
interface_name
"
]
+=
card_parts
[
1
]
except
IndexError
:
record
[
"
interface_name
"
]
+=
card_parts
[
0
]
if
record
[
"
port
"
]
is
not
None
and
record
[
"
port
"
]
!=
""
:
if
record
[
"
port
"
]
is
not
None
and
record
[
"
port
"
]
!=
""
:
record
[
"
interface_name
"
]
+=
"
-
"
+
record
[
"
port
"
]
record
[
"
interface_name
"
]
+=
"
-
"
+
record
[
"
port
"
]
record
[
"
interface_name
"
]
=
record
[
"
interface_name
"
]
\
record
[
"
interface_name
"
]
=
record
[
"
interface_name
"
]
\
.
replace
(
"
--
"
,
"
-
"
).
upper
()
.
replace
(
"
--
"
,
"
-
"
).
upper
()
return
record
return
record
...
@@ -153,7 +225,7 @@ def _juniper_field_update(record):
...
@@ -153,7 +225,7 @@ def _juniper_field_update(record):
record
[
"
interface_name
"
]
=
record
[
"
card_id
"
]
record
[
"
interface_name
"
]
=
record
[
"
card_id
"
]
if
record
[
"
port
"
]
is
not
None
and
record
[
"
port
"
]
!=
""
:
if
record
[
"
port
"
]
is
not
None
and
record
[
"
port
"
]
!=
""
:
separator
=
"
/
"
if
"
-
"
in
record
[
"
interface_name
"
]
else
""
separator
=
"
/
"
if
"
-
"
in
record
[
"
interface_name
"
]
else
""
record
[
"
interface_name
"
]
+=
separator
+
record
[
"
port
"
]
record
[
"
interface_name
"
]
+=
separator
+
str
(
record
[
"
port
"
]
)
if
record
[
"
logical_unit
"
]
is
not
None
and
record
[
"
logical_unit
"
]
!=
""
:
if
record
[
"
logical_unit
"
]
is
not
None
and
record
[
"
logical_unit
"
]
!=
""
:
record
[
"
interface_name
"
]
+=
"
.
"
+
str
(
record
[
"
logical_unit
"
])
record
[
"
interface_name
"
]
+=
"
.
"
+
str
(
record
[
"
logical_unit
"
])
return
record
return
record
...
@@ -165,7 +237,6 @@ def _update_fields(r):
...
@@ -165,7 +237,6 @@ def _update_fields(r):
def
get_circuits
(
db
):
def
get_circuits
(
db
):
retrieve_services_query
=
_generate_get_circuits_sql
()
with
cursor
(
db
)
as
crs
:
with
cursor
(
db
)
as
crs
:
crs
.
execute
(
retrieve_services_query
)
crs
.
execute
(
retrieve_services_query
)
r
=
_convert_to_dict
(
crs
)
r
=
_convert_to_dict
(
crs
)
...
@@ -173,15 +244,6 @@ def get_circuits(db):
...
@@ -173,15 +244,6 @@ def get_circuits(db):
return
r
return
r
def
_generate_get_circuits_sql
():
retrieve_services_inner_query
=
"
UNION
"
.
join
(
map
(
lambda
e
:
retrieve_services_query_template
.
format
(
*
e
),
connection_variants
))
retrieve_services_query
=
order_services_outer_query
.
\
format
(
retrieve_services_inner_query
)
return
retrieve_services_query
def
get_circuit_hierarchy
(
db
):
def
get_circuit_hierarchy
(
db
):
with
cursor
(
db
)
as
crs
:
with
cursor
(
db
)
as
crs
:
crs
.
execute
(
circuit_hierarchy_query
)
crs
.
execute
(
circuit_hierarchy_query
)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment