| 1 |
from ncclient import manager |
| 2 |
from ncclient.xml_ import NCElement |
| 3 |
from lxml import etree |
| 4 |
import six |
| 5 |
|
| 6 |
""" |
| 7 |
These are Junos XML 'helper' definitions use for generic XML processing |
| 8 |
|
| 9 |
.DEL to delete an item |
| 10 |
.REN to rename an item, requires the use of NAME() |
| 11 |
|
| 12 |
.INSERT(<'before'|'after'>) to reorder an item, requires the use of NAME() |
| 13 |
.BEFORE to reorder an item before another, requires the use of NAME() |
| 14 |
.AFTER to reorder an item after another, requires the use of NAME() |
| 15 |
|
| 16 |
.NAME(name) to assign the name attribute |
| 17 |
|
| 18 |
""" |
| 19 |
|
| 20 |
DEL = {"delete": "delete"} # Junos XML resource delete |
| 21 |
REN = {"rename": "rename"} # Junos XML resource rename |
| 22 |
ACTIVATE = {"active": "active"} # activate resource |
| 23 |
DEACTIVATE = {"inactive": "inactive"} # deactivate resource |
| 24 |
REPLACE = {"replace": "replace"} # replace elements |
| 25 |
|
| 26 |
|
| 27 |
def NAME(name): |
| 28 |
return {"name": name} |
| 29 |
|
| 30 |
|
| 31 |
def INSERT(cmd): |
| 32 |
return {"insert": cmd} |
| 33 |
|
| 34 |
|
| 35 |
BEFORE = {"insert": "before"} |
| 36 |
AFTER = {"insert": "after"} |
| 37 |
|
| 38 |
# used with <get-configuration> to load only the object identifiers and |
| 39 |
# not all the subsequent configuration |
| 40 |
|
| 41 |
NAMES_ONLY = {"recurse": "false"} |
| 42 |
|
| 43 |
# for <get-configuration>, attributes to retrieve from apply-groups |
| 44 |
INHERIT = {"inherit": "inherit"} |
| 45 |
INHERIT_GROUPS = {"inherit": "inherit", "groups": "groups"} |
| 46 |
INHERIT_DEFAULTS = {"inherit": "defaults", "groups": "groups"} |
| 47 |
|
| 48 |
# XSLT for on-box commit script |
| 49 |
conf_xslt = """\ |
| 50 |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> |
| 51 |
<xsl:output method="xml" indent="yes"/> |
| 52 |
<xsl:strip-space elements="*"/> |
| 53 |
|
| 54 |
<xsl:param name="subSelectionXPath" /> |
| 55 |
|
| 56 |
<xsl:template match="/"> |
| 57 |
<xsl:apply-templates |
| 58 |
select="$subSelectionXPath/ancestor::*[position()=last()]"/> |
| 59 |
</xsl:template> |
| 60 |
|
| 61 |
<xsl:template match="*"> |
| 62 |
<xsl:choose> |
| 63 |
|
| 64 |
<xsl:when test="$subSelectionXPath/ancestor::* |
| 65 |
[generate-id() = generate-id(current())]"> |
| 66 |
<xsl:copy> |
| 67 |
<xsl:copy-of select="@*"/> |
| 68 |
|
| 69 |
<xsl:choose> |
| 70 |
<xsl:when test="generate-id(.)= |
| 71 |
generate-id($subSelectionXPath/ancestor::*[1])"> |
| 72 |
<xsl:copy-of select="$subSelectionXPath"/> |
| 73 |
</xsl:when> |
| 74 |
<xsl:otherwise> |
| 75 |
<xsl:apply-templates select="*"/> |
| 76 |
</xsl:otherwise> |
| 77 |
</xsl:choose> |
| 78 |
|
| 79 |
</xsl:copy> |
| 80 |
</xsl:when> |
| 81 |
<xsl:otherwise/> |
| 82 |
</xsl:choose> |
| 83 |
</xsl:template> |
| 84 |
</xsl:stylesheet>""" |
| 85 |
|
| 86 |
conf_xslt_root = etree.XML(conf_xslt) |
| 87 |
conf_transform = etree.XSLT(conf_xslt_root) |
| 88 |
|
| 89 |
|
| 90 |
normalize_xslt = """\ |
| 91 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 92 |
<xsl:output method="xml" indent="no"/> |
| 93 |
|
| 94 |
<xsl:template match="/*[local-name()='rpc-reply']/*[local-name()='output']"> |
| 95 |
<output> |
| 96 |
<xsl:value-of select="."/> |
| 97 |
</output> |
| 98 |
</xsl:template> |
| 99 |
|
| 100 |
<xsl:template match="/|comment()|processing-instruction()"> |
| 101 |
<xsl:copy> |
| 102 |
<xsl:apply-templates/> |
| 103 |
</xsl:copy> |
| 104 |
</xsl:template> |
| 105 |
|
| 106 |
<xsl:template match="*"> |
| 107 |
<xsl:element name="{local-name()}"> |
| 108 |
<xsl:apply-templates select="@*|node()"/> |
| 109 |
</xsl:element> |
| 110 |
</xsl:template> |
| 111 |
|
| 112 |
<xsl:template match="@*"> |
| 113 |
<xsl:attribute name="{local-name()}"> |
| 114 |
<xsl:value-of select="."/> |
| 115 |
</xsl:attribute> |
| 116 |
</xsl:template> |
| 117 |
|
| 118 |
<xsl:template match="text()"> |
| 119 |
<xsl:value-of select="normalize-space(.)"/> |
| 120 |
</xsl:template> |
| 121 |
</xsl:stylesheet>""" |
| 122 |
|
| 123 |
|
| 124 |
# XSLT to strip comments |
| 125 |
strip_comments_xslt = """\ |
| 126 |
<xsl:stylesheet version="1.0" |
| 127 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 128 |
<xsl:output omit-xml-declaration="yes" indent="yes"/> |
| 129 |
<xsl:strip-space elements="*"/> |
| 130 |
|
| 131 |
<xsl:template match="node()|@*" name="identity"> |
| 132 |
<xsl:copy> |
| 133 |
<xsl:apply-templates select="node()|@*"/> |
| 134 |
</xsl:copy> |
| 135 |
</xsl:template> |
| 136 |
<xsl:template match="comment()"/> |
| 137 |
</xsl:stylesheet>""" |
| 138 |
|
| 139 |
strip_xslt_root = etree.XML(strip_comments_xslt) |
| 140 |
strip_comments_transform = etree.XSLT(strip_xslt_root) |
| 141 |
|
| 142 |
# XSLT to strip <rpc-error> elements |
| 143 |
strip_rpc_error_xslt = """ |
| 144 |
<xsl:stylesheet version="1.0" |
| 145 |
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 146 |
<xsl:output omit-xml-declaration="yes" indent="yes"/> |
| 147 |
<xsl:strip-space elements="*"/> |
| 148 |
|
| 149 |
<xsl:template match="node()|@*"> |
| 150 |
<xsl:copy> |
| 151 |
<xsl:apply-templates select="node()|@*"/> |
| 152 |
</xsl:copy> |
| 153 |
</xsl:template> |
| 154 |
|
| 155 |
<xsl:template match="rpc-error"/> |
| 156 |
</xsl:stylesheet> |
| 157 |
""" |
| 158 |
|
| 159 |
strip_rpc_error_root = etree.XML(strip_rpc_error_xslt) |
| 160 |
strip_rpc_error_transform = etree.XSLT(strip_rpc_error_root) |
| 161 |
|
| 162 |
|
| 163 |
def remove_namespaces(xml): |
| 164 |
for elem in xml.iter(): |
| 165 |
if elem.tag is etree.Comment: |
| 166 |
continue |
| 167 |
i = elem.tag.find("}") |
| 168 |
if i > 0: |
| 169 |
elem.tag = elem.tag[i + 1 :] |
| 170 |
return xml |
| 171 |
|
| 172 |
|
| 173 |
def remove_namespaces_and_spaces(xml): |
| 174 |
for elem in xml.iter(): |
| 175 |
if elem.tag is etree.Comment: |
| 176 |
continue |
| 177 |
# Remove namespace from attributes |
| 178 |
for k, v in elem.attrib.items(): |
| 179 |
i = k.find("}") |
| 180 |
if i >= 0: |
| 181 |
del elem.attrib[k] |
| 182 |
k = k[i + 1 :] |
| 183 |
elem.set(k, v) |
| 184 |
# Remove namespace from tags |
| 185 |
i = elem.tag.find("}") |
| 186 |
if i >= 0: |
| 187 |
elem.tag = elem.tag[i + 1 :] |
| 188 |
# remove white spaces from text |
| 189 |
if elem.text: |
| 190 |
elem.text = elem.text.strip() |
| 191 |
return xml |
| 192 |
|
| 193 |
|
| 194 |
def rpc_error(rpc_xml): |
| 195 |
""" |
| 196 |
extract the various bits from an <rpc-error> element |
| 197 |
into a dictionary |
| 198 |
""" |
| 199 |
remove_namespaces(rpc_xml) |
| 200 |
|
| 201 |
if "rpc-reply" == rpc_xml.tag: |
| 202 |
rpc_xml = rpc_xml[0] |
| 203 |
|
| 204 |
def find_strip(x): |
| 205 |
ele = rpc_xml.find(x) |
| 206 |
return ele.text.strip() if ele is not None and ele.text is not None else None |
| 207 |
|
| 208 |
this_err = {} |
| 209 |
this_err["severity"] = find_strip("error-severity") |
| 210 |
this_err["source"] = find_strip("source-daemon") |
| 211 |
this_err["edit_path"] = find_strip("error-path") |
| 212 |
this_err["bad_element"] = find_strip("error-info/bad-element") |
| 213 |
this_err["message"] = find_strip("error-message") |
| 214 |
|
| 215 |
return this_err |
| 216 |
|
| 217 |
|
| 218 |
def cscript_conf(reply): |
| 219 |
try: |
| 220 |
device_params = {"name": "junos"} |
| 221 |
device_handler = manager.make_device_handler(device_params) |
| 222 |
transform_reply = device_handler.transform_reply() |
| 223 |
return NCElement(reply, transform_reply)._NCElement__doc |
| 224 |
except: |
| 225 |
return None |
| 226 |
|
| 227 |
|
| 228 |
# xslt to remove prefix like junos:ns |
| 229 |
strip_namespaces_prefix = six.b( |
| 230 |
"""<?xml version="1.0" encoding="UTF-8" ?> |
| 231 |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> |
| 232 |
<xsl:output method="xml" indent="no" omit-xml-declaration="no" /> |
| 233 |
|
| 234 |
<xsl:template match="/ |comment() |processing-instruction()"> |
| 235 |
<xsl:copy> |
| 236 |
<xsl:apply-templates select="/*" /> |
| 237 |
</xsl:copy> |
| 238 |
</xsl:template> |
| 239 |
|
| 240 |
<xsl:template match="*"> |
| 241 |
<xsl:element name="{local-name()}" namespace="{namespace-uri()}"> |
| 242 |
<xsl:apply-templates select="@*|node()" /> |
| 243 |
</xsl:element> |
| 244 |
</xsl:template> |
| 245 |
|
| 246 |
<xsl:template match="@*"> |
| 247 |
<xsl:attribute name="{local-name()}"> |
| 248 |
<xsl:value-of select="." /> |
| 249 |
</xsl:attribute> |
| 250 |
</xsl:template> |
| 251 |
</xsl:stylesheet>""" |
| 252 |
) |