| 1 |
# stdlib |
| 2 |
from copy import deepcopy |
| 3 |
|
| 4 |
# local |
| 5 |
from jnpr.junos.factory.cfgtable import CfgTable |
| 6 |
from jnpr.junos.factory.optable import OpTable |
| 7 |
from jnpr.junos.factory.cmdtable import CMDTable |
| 8 |
from jnpr.junos.factory.table import Table |
| 9 |
|
| 10 |
from jnpr.junos.factory.view import View |
| 11 |
from jnpr.junos.factory.cmdview import CMDView |
| 12 |
|
| 13 |
from jnpr.junos.utils.config import Config |
| 14 |
|
| 15 |
|
| 16 |
def FactoryCfgTable(table_name=None, data_dict={}): |
| 17 |
if table_name is None: |
| 18 |
table_name = "CfgTable" |
| 19 |
if "set" in data_dict.keys(): |
| 20 |
new_cls = type(table_name, (CfgTable, Config), {}) |
| 21 |
else: |
| 22 |
new_cls = type(table_name, (CfgTable,), {}) |
| 23 |
new_cls.DEFINE = deepcopy(data_dict) |
| 24 |
new_cls.__module__ = __name__.replace("factory_cls", "CfgTable") |
| 25 |
return new_cls |
| 26 |
|
| 27 |
|
| 28 |
def FactoryOpTable( |
| 29 |
cmd, |
| 30 |
args=None, |
| 31 |
args_key=None, |
| 32 |
item=None, |
| 33 |
key=OpTable.ITEM_NAME_XPATH, |
| 34 |
view=None, |
| 35 |
table_name=None, |
| 36 |
use_filter=True, |
| 37 |
): |
| 38 |
if table_name is None: |
| 39 |
table_name = "OpTable." + cmd |
| 40 |
new_cls = type(table_name, (OpTable,), {}) |
| 41 |
new_cls.GET_RPC = cmd |
| 42 |
new_cls.GET_ARGS = args or {} |
| 43 |
if args_key is not None: |
| 44 |
new_cls.GET_KEY = args_key |
| 45 |
new_cls.ITEM_XPATH = item |
| 46 |
new_cls.ITEM_NAME_XPATH = key |
| 47 |
new_cls.VIEW = view |
| 48 |
new_cls.USE_FILTER = use_filter |
| 49 |
new_cls.__module__ = __name__.replace("factory_cls", "OpTable") |
| 50 |
return new_cls |
| 51 |
|
| 52 |
|
| 53 |
def FactoryCMDTable( |
| 54 |
cmd, |
| 55 |
args=None, |
| 56 |
item=None, |
| 57 |
key_items=None, |
| 58 |
key="name", |
| 59 |
view=None, |
| 60 |
table_name=None, |
| 61 |
title=None, |
| 62 |
delimiter=None, |
| 63 |
eval=None, |
| 64 |
platform="juniper_junos", |
| 65 |
use_textfsm=False, |
| 66 |
**kwargs |
| 67 |
): |
| 68 |
new_cls = type(table_name, (CMDTable,), {}) |
| 69 |
new_cls.GET_CMD = cmd |
| 70 |
if "target" in kwargs: |
| 71 |
new_cls.TARGET = kwargs["target"] |
| 72 |
new_cls.KEY_ITEMS = key_items |
| 73 |
new_cls.CMD_ARGS = args or {} |
| 74 |
new_cls.ITEM = item |
| 75 |
new_cls.KEY = key |
| 76 |
new_cls.VIEW = view |
| 77 |
new_cls.TITLE = title |
| 78 |
new_cls.DELIMITER = delimiter |
| 79 |
new_cls.EVAL = eval |
| 80 |
new_cls.PLATFORM = platform |
| 81 |
new_cls.USE_TEXTFSM = use_textfsm |
| 82 |
new_cls.__module__ = __name__.replace("factory_cls", "CMDTable") |
| 83 |
return new_cls |
| 84 |
|
| 85 |
|
| 86 |
def FactoryCMDChildTable( |
| 87 |
title=None, |
| 88 |
regex=None, |
| 89 |
key="name", |
| 90 |
delimiter=None, |
| 91 |
table_name=None, |
| 92 |
view=None, |
| 93 |
key_items=None, |
| 94 |
item=None, |
| 95 |
eval=None, |
| 96 |
): |
| 97 |
new_cls = type(table_name, (CMDTable,), {}) |
| 98 |
new_cls.DELIMITER = delimiter |
| 99 |
new_cls.KEY = key |
| 100 |
new_cls.REGEX = regex |
| 101 |
new_cls.TITLE = title |
| 102 |
new_cls.VIEW = view |
| 103 |
new_cls.KEY_ITEMS = key_items |
| 104 |
new_cls.ITEM = item |
| 105 |
new_cls.EVAL = eval |
| 106 |
new_cls.__module__ = __name__.replace("factory_cls", "CMDTable") |
| 107 |
return new_cls |
| 108 |
|
| 109 |
|
| 110 |
def FactoryTable( |
| 111 |
item, key=Table.ITEM_NAME_XPATH, view=None, table_name=None, use_filter=True |
| 112 |
): |
| 113 |
if table_name is None: |
| 114 |
table_name = "Table." + item |
| 115 |
new_cls = type(table_name, (Table,), {}) |
| 116 |
new_cls.ITEM_XPATH = item |
| 117 |
new_cls.ITEM_NAME_XPATH = key |
| 118 |
new_cls.VIEW = view |
| 119 |
new_cls.USE_FILTER = use_filter |
| 120 |
new_cls.__module__ = __name__.replace("factory_cls", "Table") |
| 121 |
return new_cls |
| 122 |
|
| 123 |
|
| 124 |
def FactoryView(fields, **kvargs): |
| 125 |
""" |
| 126 |
:fields: |
| 127 |
dictionary of fields, structure of which is ~internal~ and should |
| 128 |
not be defined explicitly. use the RunstatMaker.Fields() mechanism to |
| 129 |
create theserather than hardcoding the dictionary structures; |
| 130 |
since they might change over time. |
| 131 |
|
| 132 |
:kvargs: |
| 133 |
'view_name' to name the class. this could be useful for debug |
| 134 |
or eventual callback mechanisms. |
| 135 |
|
| 136 |
'groups' is a dict of name/xpath assocaited to fields |
| 137 |
this technique would be used to extract fields from |
| 138 |
node-set elements like port <if-device-flags>. |
| 139 |
|
| 140 |
'extends' names the base View class to extend. using this |
| 141 |
technique you can add to existing defined Views. |
| 142 |
""" |
| 143 |
|
| 144 |
view_name = kvargs.get("view_name", "RunstatView") |
| 145 |
new_cls = type(view_name, (View,), {}) |
| 146 |
|
| 147 |
if "extends" in kvargs: |
| 148 |
base_cls = kvargs["extends"] |
| 149 |
new_cls.FIELDS = deepcopy(base_cls.FIELDS) |
| 150 |
new_cls.FIELDS.update(fields) |
| 151 |
if "groups" in kvargs: |
| 152 |
new_cls.GROUPS = deepcopy(base_cls.GROUPS) |
| 153 |
new_cls.GROUPS.update(kvargs["groups"]) |
| 154 |
else: |
| 155 |
new_cls.FIELDS = fields |
| 156 |
new_cls.GROUPS = kvargs["groups"] if "groups" in kvargs else None |
| 157 |
|
| 158 |
if "eval" in kvargs: |
| 159 |
new_cls.EVAL = kvargs["eval"] |
| 160 |
new_cls.FIELDS.update(kvargs["eval"]) |
| 161 |
|
| 162 |
new_cls.__module__ = __name__.replace("factory_cls", "View") |
| 163 |
return new_cls |
| 164 |
|
| 165 |
|
| 166 |
def FactoryCMDView(fields, **kvargs): |
| 167 |
""" |
| 168 |
:fields: |
| 169 |
dictionary of fields, structure of which is ~internal~ and should |
| 170 |
not be defined explicitly. use the RunstatMaker.Fields() mechanism to |
| 171 |
create theserather than hardcoding the dictionary structures; |
| 172 |
since they might change over time. |
| 173 |
|
| 174 |
:kvargs: |
| 175 |
'view_name' to name the class. this could be useful for debug |
| 176 |
or eventual callback mechanisms. |
| 177 |
|
| 178 |
'groups' is a dict of name/xpath assocaited to fields |
| 179 |
this technique would be used to extract fields from |
| 180 |
node-set elements like port <if-device-flags>. |
| 181 |
|
| 182 |
'extends' names the base View class to extend. using this |
| 183 |
technique you can add to existing defined Views. |
| 184 |
""" |
| 185 |
|
| 186 |
view_name = kvargs.get("view_name", "RunstatView") |
| 187 |
new_cls = type(view_name, (CMDView,), {}) |
| 188 |
|
| 189 |
if "columns" in kvargs: |
| 190 |
new_cls.COLUMNS = deepcopy(kvargs["columns"]) |
| 191 |
elif "title" in kvargs: |
| 192 |
new_cls.TITLE = deepcopy(kvargs["title"]) |
| 193 |
if "regex" in kvargs: |
| 194 |
new_cls.REGEX = deepcopy(kvargs["regex"]) |
| 195 |
if "exists" in kvargs: |
| 196 |
new_cls.EXISTS = deepcopy(kvargs["exists"]) |
| 197 |
if "filters" in kvargs: |
| 198 |
new_cls.FILTERS = deepcopy(kvargs["filters"]) |
| 199 |
if fields is not None: |
| 200 |
new_cls.FIELDS = fields |
| 201 |
if "eval" in kvargs: |
| 202 |
new_cls.EVAL = kvargs["eval"] |
| 203 |
# new_cls.FIELDS.update(kvargs['eval']) |
| 204 |
|
| 205 |
new_cls.__module__ = __name__.replace("factory_cls", "CMDView") |
| 206 |
return new_cls |