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
a50dc31e
Commit
a50dc31e
authored
6 years ago
by
Erik Reid
Browse files
Options
Downloads
Patches
Plain Diff
added validation to netconf input
parent
2ec8b093
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
inventory_provider/netconf.py
+109
-3
109 additions, 3 deletions
inventory_provider/netconf.py
test/test_netconf_data.py
+0
-1
0 additions, 1 deletion
test/test_netconf_data.py
with
109 additions
and
4 deletions
inventory_provider/netconf.py
+
109
−
3
View file @
a50dc31e
# import os
from
jnpr.junos
import
Device
import
logging
from
jnpr.junos
import
Device
from
lxml
import
etree
CONFIG_SCHEMA
=
"""
<?xml version=
"
1.1
"
encoding=
"
UTF-8
"
?>
<xs:schema xmlns:xs=
"
http://www.w3.org/2001/XMLSchema
"
>
<xs:complexType name=
"
generic-sequence
"
>
<xs:sequence>
<xs:any processContents=
"
lax
"
minOccurs=
"
0
"
maxOccurs=
"
unbounded
"
/>
</xs:sequence>
<xs:anyAttribute processContents=
"
skip
"
/>
</xs:complexType>
<!-- NOTE:
'
unit
'
content isn
'
t validated -->
<xs:complexType name=
"
juniper-interface
"
>
<xs:sequence>
<xs:choice minOccurs=
"
1
"
maxOccurs=
"
unbounded
"
>
<xs:element name=
"
name
"
minOccurs=
"
1
"
maxOccurs=
"
1
"
type=
"
xs:string
"
/>
<xs:element name=
"
description
"
minOccurs=
"
0
"
maxOccurs=
"
1
"
type=
"
xs:string
"
/>
<xs:any processContents=
"
lax
"
minOccurs=
"
0
"
maxOccurs=
"
unbounded
"
/>
</xs:choice>
</xs:sequence>
<xs:attribute name=
"
inactive
"
type=
"
xs:string
"
/>
</xs:complexType>
<xs:element name=
"
configuration
"
>
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs=
"
1
"
maxOccurs=
"
unbounded
"
>
<xs:element name=
"
version
"
minOccurs=
"
0
"
type=
"
xs:string
"
/>
<xs:element name=
"
groups
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
apply-groups
"
minOccurs=
"
0
"
type=
"
xs:string
"
/>
<xs:element name=
"
system
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
logical-systems
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
chassis
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
services
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
interfaces
"
minOccurs=
"
0
"
>
<xs:complexType>
<xs:sequence>
<xs:choice minOccurs=
"
1
"
maxOccurs=
"
unbounded
"
>
<xs:element name=
"
apply-groups
"
minOccurs=
"
0
"
type=
"
xs:string
"
/>
<xs:element name=
"
interface
"
minOccurs=
"
1
"
maxOccurs=
"
unbounded
"
type=
"
juniper-interface
"
/>
</xs:choice>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name=
"
snmp
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
forwarding-options
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
routing-options
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
protocols
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
policy-options
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
class-of-service
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
firewall
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
routing-instances
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
<xs:element name=
"
bridge-domains
"
minOccurs=
"
0
"
type=
"
generic-sequence
"
/>
</xs:choice>
</xs:sequence>
<xs:attribute name=
"
changed-seconds
"
type=
"
xs:string
"
/>
<xs:attribute name=
"
changed-localtime
"
type=
"
xs:string
"
/>
</xs:complexType>
</xs:element>
</xs:schema>
"""
UNIT_SCHEMA
=
"""
<?xml version=
"
1.1
"
encoding=
"
UTF-8
"
?>
<xs:schema xmlns:xs=
"
http://www.w3.org/2001/XMLSchema
"
>
<xs:complexType name=
"
generic-sequence
"
>
<xs:sequence>
<xs:any processContents=
"
lax
"
minOccurs=
"
0
"
maxOccurs=
"
unbounded
"
/>
</xs:sequence>
<xs:anyAttribute processContents=
"
skip
"
/>
</xs:complexType>
<xs:element name=
"
unit
"
>
<xs:complexType>
<xs:sequence>
<xs:element name=
"
name
"
minOccurs=
"
1
"
maxOccurs=
"
1
"
type=
"
xs:int
"
/>
<xs:element name=
"
description
"
minOccurs=
"
0
"
maxOccurs=
"
1
"
type=
"
xs:string
"
/>
<xs:any processContents=
"
lax
"
minOccurs=
"
0
"
maxOccurs=
"
unbounded
"
/>
</xs:sequence>
<xs:attribute name=
"
inactive
"
type=
"
xs:string
"
/>
</xs:complexType>
</xs:element>
</xs:schema>
"""
def
_rpc
(
hostname
,
ssh
):
dev
=
Device
(
...
...
@@ -15,7 +101,27 @@ def _rpc(hostname, ssh):
def
load_config
(
hostname
,
ssh_params
):
# data = dev.rpc.get_config(options={'format': 'json'})
return
_rpc
(
hostname
,
ssh_params
).
get_config
()
config
=
_rpc
(
hostname
,
ssh_params
).
get_config
()
def
_validate
(
schema
,
doc
):
if
schema
.
validate
(
doc
):
return
for
e
in
schema
.
error_log
:
logging
.
debug
(
"
%d.%d: %s
"
%
(
e
.
line
,
e
.
column
,
e
.
message
))
assert
False
schema_doc
=
etree
.
XML
(
CONFIG_SCHEMA
.
encode
(
'
utf-8
'
))
config_schema
=
etree
.
XMLSchema
(
schema_doc
)
_validate
(
config_schema
,
config
)
# validate interfaces/interface/unit elements ...
schema_doc
=
etree
.
XML
(
UNIT_SCHEMA
.
encode
(
'
utf-8
'
))
unit_schema
=
etree
.
XMLSchema
(
schema_doc
)
for
i
in
config
.
xpath
(
'
//configuration/interfaces/interface
'
):
for
u
in
i
.
xpath
(
'
./unit
'
):
_validate
(
unit_schema
,
u
)
return
config
def
load_interfaces
(
hostname
,
ssh_params
):
...
...
This diff is collapsed.
Click to expand it.
test/test_netconf_data.py
+
0
−
1
View file @
a50dc31e
...
...
@@ -28,7 +28,6 @@ def test_query_doc_and_validate(mocker, data_config):
'
inventory_provider.netconf.Device
'
,
MockedJunosDevice
)
etree
.
parse
(
SCHEMA_FILENAME
)
for
r
in
data_config
[
'
routers
'
]:
doc
=
netconf
.
load_config
(
r
[
'
hostname
'
],
data_config
[
'
ssh
'
])
...
...
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